.net Simple Httpserver
业务需求,需要自托管 http server, 把本地的一个文件夹host起来,本地访问。
不要求高性能, 只要功能正常。
c# 实现
java 实现
@Slf4j
public class SimpleHttpServer {
static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(50,100,
1, TimeUnit.MINUTES,
new LinkedBlockingQueue<>(5000));
@SneakyThrows
public static void main(String[] args) {
HttpServer server = HttpServer.create(new InetSocketAddress("localhost", 9988), 0);
server.createContext("/", new MyHttpHandler("/htmlDir"));
server.setExecutor(threadPool);
server.start();
log.info(" Server started on port 9988, http://localhost:9988/");
}
}
性能压测对比
> c# async http
wrk -t1 -c1 -d1s --latency http://localhost:59856/index2.html
Running 1s test @ http://localhost:59856/index2.html
1 threads and 1 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 4.14ms 10.91ms 60.13ms 89.39%
Req/Sec 5.94k 3.91k 9.29k 72.73%
Latency Distribution
50% 112.00us
75% 302.00us
90% 16.53ms
99% 51.67ms
6489 requests in 1.10s, 38.08MB read
Requests/sec: 5888.40
Transfer/sec: 34.56MB
> simple http server (java built-in http server)
wrk -c1 -t1 -d1s http://localhost:9988/index2.html --latency
Running 1s test @ http://localhost:9988/index2.html
1 threads and 1 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 130.76us 460.33us 7.18ms 96.36%
Req/Sec 16.75k 4.41k 21.08k 90.91%
Latency Distribution
50% 52.00us
75% 62.00us
90% 89.00us
99% 2.30ms
18351 requests in 1.10s, 106.35MB read
Requests/sec: 16688.63
Transfer/sec: 96.72MB
> nginx
wrk -c1 -t1 -d1s http://localhost/index2.html --latency
Running 1s test @ http://localhost/index2.html
1 threads and 1 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 167.40us 1.27ms 17.59ms 98.55%
Req/Sec 33.99k 2.83k 36.53k 81.82%
Latency Distribution
50% 26.00us
75% 29.00us
90% 35.00us
99% 6.48ms
37129 requests in 1.10s, 207.67MB read
Requests/sec: 33764.96
Transfer/sec: 188.86MB
总结
- nginx 果然厉害,静态页面,QPS 3万多, 非常稳且低时延
- c# 基于 task 的 .net http server QPS 6000 也不差了,进程内host Dir足够了
- jdk 内置的 基于线程池的 http server 也很不错,QPS 1万6
- 但是 nginx 平均 0.167ms vs 4.38 ms, 160 us vs 4380 us, ~~ 27倍