怎么把单机的高并发做到最高,为什么 1 万的并发和 5 万的并发, CPU 占用没有变化?

13次阅读

共计 2030 个字符,预计需要花费 6 分钟才能阅读完成。

最近基于开源的高性能网络库 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

但发现有几个现象不是很理解:

1. wrk 同时开启的客户端越少,得到的 QPS 数据越好,以下是具体数据

`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
`

2. 不管以上那种压测的方式,HTTP 服务器的 CPU 占用都稳定在 240% 左右,一核 30% 左右,等于没有系统的 CPU 利用一点都不高,没有完全压榨机器的 CPU 性能,按道理并发数不同,服务器的压力应该也不同。

3. 当在压测服务器之间和 HTTP 服务器之间,用另一台 8 和的机器搭建 Nginx 做代理,然后 wrk 直接向 Nginx 请求,Nginx 再转发,发现没有直接法相 HTTP 服务器得到的 QPS 高,Nginx 的作用难道不是替后端服务抵挡高并发吗,怎么加了,效果变差了。Nginx 的配置也加入了:

worker_processes auto; worker_rlimit_nofile 30000; events {worker_connections 10240;}

正文完
 0