使用 apache 基准测试 100K 请求 20K 并发用户:
$ ab -n 100000 -c 20000 http://localhost:8080/mrs/ping
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
apr_socket_recv: Connection reset by peer (104) <<< HOW to overcome??
下面是 Undertow(版本 1.2.6 + xnio-api 3.3.1)PingServer:
public class UndertowPingServer {
private static Logger log = Logger.getLogger(UndertowPingServer.class);
public static void main(String[] args) throws ServletException {
PathHandler path = Handlers.path()
.addPrefixPath("/mrs/ping", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(
Headers.CONTENT_TYPE, "text/plain");
exchange.getResponseSender().send("Server Time:" + new Date().toString() + "\n\n");
}
});
Undertow.Builder builder = Undertow.builder()
.setHandler(path)
.addHttpListener(8080, "0.0.0.0")
.setBufferSize(1024 * 16)
//this seems slightly faster in some configurations
.setIoThreads(Runtime.getRuntime().availableProcessors() * 2)
.setSocketOption(Options.BACKLOG, 500000)
.setWorkerThreads(2000)
//don't send a keep-alive header for HTTP/1.1 requests, as it is not required
.setServerOption(UndertowOptions.ALWAYS_SET_KEEP_ALIVE, false);
Undertow server = builder.build();
server.start();
log.info("micro-service running!");
}
}
所有需要的 linux 内核套接字和通过 sysctl 进行的线程设置都已经完成。这就是为什么它可以毫无问题地对 20k 用户执行第一个 90K 请求的原因。