JAVA实现一个单机9万QPS的网络服务端

背景

之前有通过java实现一个模拟redis的服务端,本期将以上次的网络模型为基础,再添加并发处理,实现一个支持http的服务端,并使用ab(Apache Benchmark)做性能的测试。

描述

如果要理解流程需要对http协议有所了解。整个代码来说,以epoll(本地linux)的事件通知网络模型为基础,分别处理不同的网络事件,同时将这些处理流程再放到多线程里,期望获得较高的执行效率。

代码

http_server

package server;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

public class HttpChannelServer extends SelectorServer {

    public void handle(SelectionKey key) throws IOException {
        switch (key.interestOps()) {
            case SelectionKey.OP_ACCEPT:
                //                System.out.println("accept");
                handleAccept(key);
                break;
            case SelectionKey.OP_READ:
                //                System.out.println("read");
                handleRead(key);
                break;
            case SelectionKey.OP_WRITE:
                //                System.out.println("write");
                handleWrite(key);
                break;
            default:
                break;
        }
    }

    private static void handleAccept(SelectionKey key) throws IOException {
        SocketChannel channel = ((ServerSocketChannel) key.channel()).accept();
        if (null == channel) {
            return;
        }
        System.out.println(channel.getRemoteAddress().toString());
        channel.configureBlocking(false);
        channel.register(key.selector(), SelectionKey.OP_READ);
    }

    private static void handleWrite(SelectionKey key) throws IOException {
        SocketChannel channel = (SocketChannel) key.channel();
        channel.configureBlocking(false);
        channel.write(ByteBuffer.wrap(httpBody().getBytes(StandardCharsets.UTF_8)));
//        channel.close();
        channel.register(key.selector(), SelectionKey.OP_READ);
    }

    private static void handleRead(SelectionKey key) throws IOException {
        SocketChannel channel = (SocketChannel) key.channel();
        ByteBuffer buffer = ByteBuffer.allocateDirect(2048);
        while (channel.read(buffer) > 0) {
            printString(buffer);
        }
        channel.register(key.selector(), SelectionKey.OP_WRITE);
    }

    public static String printString(ByteBuffer byteBuffer) {
        byteBuffer.flip();
        byte[] bytes = new byte[byteBuffer.limit() - byteBuffer.position()];
        byteBuffer.get(bytes);
        String s = new String(bytes, StandardCharsets.UTF_8);
        //        System.out.println(s);
        return s;
    }

    public static String httpBody() {
        return "HTTP/1.1 200 OK\n"
                + "Server: Mini-jedis\n"
                + "Last-Modified: Sun, 26 Sep 2029 22:04:35 GMT\n"
                + "Accept-Ranges: bytes\n"
                + "Connection: keep-alive\r"
                + "Content-Type: text/html\n"
                + "\n"
                + "Hello world!";
    }
}

  1. 网络模型
package server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.util.Iterator;

public abstract class SelectorServer {

    public void start() {
        ServerSocketChannel serverChannel = null;
        try {
            serverChannel = ServerSocketChannel.open();
            serverChannel.bind(new InetSocketAddress("127.0.0.1", 6379));
            serverChannel.configureBlocking(false);

            for (int i = 0; i < 6; i++) {
                ServerSocketChannel s = serverChannel;
                new Thread(() -> {
                    try {
                        runner(s);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }).start();
            }
            //主线程等待执行
            Thread.currentThread().join();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(-1);
        }finally {
            if (null != serverChannel) {
                try {
                    serverChannel.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void runner(ServerSocketChannel serverChannel) throws IOException {
        Selector selector = Selector.open();
        serverChannel.register(selector, SelectionKey.OP_ACCEPT);
        while (true) {
            selector.select();
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                handle(iterator.next());
                iterator.remove();
            }
        }
    }

    protected abstract void handle(SelectionKey key) throws IOException;
}
  1. 执行入口
import server.HttpChannelServer;

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello World!");
        new HttpChannelServer().start();
    }
}

结果

如下是开了4个线程的结果,达到9.3万每秒的请求速率。 ab开启10个并发,执行1000万请求,耗时106.525秒。

Benchmarking 127.0.0.1 (be patient)
Completed 1000000 requests
Completed 2000000 requests
Completed 3000000 requests
Completed 4000000 requests
Completed 5000000 requests
Completed 6000000 requests
Completed 7000000 requests
Completed 8000000 requests
Completed 9000000 requests
Completed 10000000 requests
Finished 10000000 requests


Server Software:        Mini-Server
Server Hostname:        127.0.0.1
Server Port:            6379

Document Path:          /
Document Length:        12 bytes

Concurrency Level:      10
Time taken for tests:   106.525 seconds
Complete requests:      10000000
Failed requests:        0
Keep-Alive requests:    10000000
Total transferred:      1610000000 bytes
HTML transferred:       120000000 bytes
Requests per second:    93875.08 [#/sec] (mean)
Time per request:       0.107 [ms] (mean)
Time per request:       0.011 [ms] (mean, across all concurrent requests)
Transfer rate:          14759.66 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0    0   0.5      0     182
Waiting:        0    0   0.5      0     182
Total:          0    0   0.5      0     182

然而,通过分别修改为单线程,10个线程,6个线程之后,结果表明增加线程并不能有效增加执行效率。原因有待分析。其中4个线程对应9.3W,6个线程8.9W,10个线程最差6.8W,1个线程9.0W

说明

  1. 在尝试增加线程数之后,增加到6个,反而出现了下降。如下是结果,大概在每秒8.9W请求
$ab -c 10 -n 10000000 -k http://127.0.0.1:6379/
Benchmarking 127.0.0.1 (be patient)
Completed 1000000 requests
Completed 2000000 requests
Completed 3000000 requests
Completed 4000000 requests
Completed 5000000 requests
Completed 6000000 requests
Completed 7000000 requests
Completed 8000000 requests
Completed 9000000 requests
Completed 10000000 requests
Finished 10000000 requests


Server Software:        Mini-Server
Server Hostname:        127.0.0.1
Server Port:            6379

Document Path:          /
Document Length:        12 bytes

Concurrency Level:      10
Time taken for tests:   111.889 seconds
Complete requests:      10000000
Failed requests:        0
Keep-Alive requests:    10000000
Total transferred:      1610000000 bytes
HTML transferred:       120000000 bytes
Requests per second:    89374.39 [#/sec] (mean)
Time per request:       0.112 [ms] (mean)
Time per request:       0.011 [ms] (mean, across all concurrent requests)
Transfer rate:          14052.03 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0    0   0.7      0     174
Waiting:        0    0   0.7      0     174
Total:          0    0   0.7      0     174

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      0
  95%      0
  98%      1
  99%      1
 100%    174 (longest request)
  1. 暂时只支持使用ab命令测试, wrk本地出现“断开的管道错误”,原因是wrk和ab处理连接的机制有区别,需要抓包分析。

推荐阅读更多精彩内容