golang和nginx的简单性能对比
说明
测试都是ab做的,中等并发量,统一采用10000并发,100000个请求。都是本机请求本机,避免公司内网IDS的干扰。
机器是一台双核CPU的DELL:Intel(R) Pentium(R) CPU G2030 @ 3.00GHz。配4G内存。
第一组数据是ab测试nginx,nginx的配置如下:
worker_processes 4;
pid /run/nginx.pid;
worker_rlimit_nofile 30000;
events {
worker_connections 20000;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
...
}
第二组是ab测试golang,返回固定是个OK。
第三组是ab测试golang,返回某个目录或文件。
err := http.ListenAndServe(":8080", http.FileServer(http.Dir("/home/shell/photo")))
nginx
Concurrency Level: 10000
Time taken for tests: 5.720 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 172100000 bytes
HTML transferred: 160000000 bytes
Requests per second: 17482.47 [#/sec] (mean)
Time per request: 572.001 [ms] (mean)
Time per request: 0.057 [ms] (mean, across all concurrent requests)
Transfer rate: 29382.16 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 320 581.1 146 3262
Processing: 1 197 136.6 198 1886
Waiting: 1 162 120.6 154 1811
Total: 1 517 604.1 371 3558
Percentage of the requests served within a certain time (ms)
50% 371
66% 455
75% 515
80% 587
90% 1167
95% 1378
98% 3375
99% 3402
100% 3558 (longest request)
golang with string
Concurrency Level: 10000
Time taken for tests: 5.147 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 11800000 bytes
HTML transferred: 200000 bytes
Requests per second: 19429.37 [#/sec] (mean)
Time per request: 514.685 [ms] (mean)
Time per request: 0.051 [ms] (mean, across all concurrent requests)
Transfer rate: 2238.93 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 293 659.6 5 3020
Processing: 1 37 112.3 10 1644
Waiting: 1 34 111.9 9 1642
Total: 3 329 700.4 16 4653
Percentage of the requests served within a certain time (ms)
50% 16
66% 25
75% 248
80% 1003
90% 1032
95% 1431
98% 3026
99% 3042
100% 4653 (longest request)
golang with file
Concurrency Level: 10000
Time taken for tests: 8.122 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 100000
Total transferred: 72200000 bytes
HTML transferred: 53500000 bytes
Requests per second: 12312.87 [#/sec] (mean)
Time per request: 812.158 [ms] (mean)
Time per request: 0.081 [ms] (mean, across all concurrent requests)
Transfer rate: 8681.54 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 29 333.4 0 7009
Processing: 0 495 98.1 524 1918
Waiting: 0 495 98.1 524 1918
Total: 0 524 353.0 525 8086
Percentage of the requests served within a certain time (ms)
50% 525
66% 531
75% 535
80% 537
90% 543
95% 550
98% 558
99% 563
100% 8086 (longest request)
分析
从rps来看,三者都达到了10Krps的级别以上,而且差距很小。golang在没有逻辑的情况下比nginx还要快11%,但是加入逻辑后反而落后30%(这不奇怪)。三者差距都在50%以内,基本属于同一个数量级。
如果不考虑golang本身的内存管理问题,我觉得可以用golang替代nginx+lua的方案了。至少代码好写很多。