最近基于开源的高性能网络库 muduo/libhv ,分别搭建一个简单的 HTTP 服务器,就一个接口/get ,然后返回 hello world. HTTP 服务器单独部署在 8 核的 ubuntu 机器上,代码中开了 8 个工作线程。
在另一台 12 核的 ubuntu 机器上用 wrk 对 HTTP 服务器进行压测。两台机器都分别用ulimit -n 65536
开启了最大的连接数。
HTTP 服务器还做了一些内核参数的优化,在/etc/sysctl.conf 里面加入了如下内容:
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.ip_local_port_range = 10000 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog = 32768
net.core.somaxconn = 32768
net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_wmem = 8192 436600 873200
net.ipv4.tcp_rmem = 32768 436600 873200
net.ipv4.tcp_mem = 94500000 91500000 92700000
net.ipv4.tcp_max_orphans = 3276800
net.ipv4.tcp_fin_timeout = 30
`alex@alex-System-Product-Name:~/code/wrk$ wrk -t12 -c1000 -d100s
12 threads and 1000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 41.01ms 37.09ms 542.37ms 88.01%
Req/Sec 2.38k 1.11k 8.33k 62.50%
2838582 requests in 1.67m, 319.44MB read
Requests/sec: 28358.09
Transfer/sec: 3.19MB
alex@alex-System-Product-Name:~/code/wrk$ wrk -t12 -c10000 -d100s
12 threads and 10000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 364.67ms 115.01ms 1.49s 81.21%
Req/Sec 2.04k 0.98k 7.46k 61.89%
2430692 requests in 1.67m, 273.53MB read
Socket errors: connect 0, read 0, write 0, timeout 93
Requests/sec: 24292.02
Transfer/sec: 2.73MB
alex@alex-System-Product-Name:~/code/wrk$ wrk -t36 -c50000 -d60s
36 threads and 50000 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 910.52ms 276.46ms 2.00s 83.81%
Req/Sec 836.26 0.90k 13.56k 86.67%
1489770 requests in 1.00m, 167.65MB read
Socket errors: connect 21740, read 0, write 0, timeout 16523
Requests/sec: 24790.08
Transfer/sec: 2.79MB
`
worker_processes auto; worker_rlimit_nofile 30000; events { worker_connections 10240; }
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.