机器:Macbook Pro CPU:8核 内存:16G SSD硬盘
测试工具:ab
测试并发:一共发送1000个请求 ,一共100个客户端 。
PS:本来想把并发数弄大一些,奈何只要调大了就会报错:apr_socket_recv: Connection reset by peer (54),按照网上的资料说是ab的bug,把Apache重新安装就行,然后用新安装的ab替换旧的ab,然并卵。。 依然这个错误,所以就暂时先这样吧。
测试命令:
nginx:ab -n1000 -c100 -k http://localhost/test/index.php
workerman: ab -n1000 -c100 -k http://127.0.0.1:9999/
swoole:ab -n1000 -c100 -k http://127.0.0.1:9501/
nginx对应的 代码
1 2 3 |
<?php echo 'hello world'; |
workerman代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php use Workerman\Worker; require_once './Workerman/Autoloader.php'; $worker = new Worker('http://0.0.0.0:9999'); $worker->onMessage = function($connection, $request) { $connection->send("hello world"); }; Worker::runAll(); |
swoole代码
1 2 3 4 5 6 7 |
<?php $http = new Swoole\Http\Server("127.0.0.1", 9501); $http->on('request', function ($request, $response) { $response->end("Hello world"); }); $http->start(); |
三个文件的代码都是最简单的输出hello world。
测试结果
Nginx结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
命令:ab -n1000 -c100 -k http://localhost/test/index.php This is ApacheBench, Version 2.3 <$Revision: 1843412 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking localhost (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: nginx/1.17.0 Server Hostname: localhost Server Port: 80 Document Path: /test/index.php Document Length: 11 bytes Concurrency Level: 100 Time taken for tests: 0.141 seconds Complete requests: 1000 Failed requests: 0 Keep-Alive requests: 0 Total transferred: 174000 bytes HTML transferred: 11000 bytes Requests per second: 7084.76 [#/sec] (mean) Time per request: 14.115 [ms] (mean) Time per request: 0.141 [ms] (mean, across all concurrent requests) Transfer rate: 1203.86 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.9 0 4 Processing: 1 13 2.0 13 17 Waiting: 1 13 2.0 13 17 Total: 5 13 1.7 13 18 Percentage of the requests served within a certain time (ms) 50% 13 66% 14 75% 14 80% 15 90% 16 95% 16 98% 17 99% 18 100% 18 (longest request) |
Workerman结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
命令:ab -n1000 -c100 -k http://127.0.0.1:9999/ This is ApacheBench, Version 2.3 <$Revision: 1843412 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: workerman/3.5.20 Server Hostname: 127.0.0.1 Server Port: 9999 Document Path: / Document Length: 11 bytes Concurrency Level: 100 Time taken for tests: 0.029 seconds Complete requests: 1000 Failed requests: 0 Keep-Alive requests: 1000 Total transferred: 139000 bytes HTML transferred: 11000 bytes Requests per second: 34917.42 [#/sec] (mean) Time per request: 2.864 [ms] (mean) Time per request: 0.029 [ms] (mean, across all concurrent requests) Transfer rate: 4739.77 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.6 0 3 Processing: 0 2 2.8 2 22 Waiting: 0 2 2.8 2 22 Total: 0 2 2.9 2 22 Percentage of the requests served within a certain time (ms) 50% 2 66% 2 75% 2 80% 2 90% 3 95% 4 98% 17 99% 22 100% 22 (longest request) |
Swoole结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
命令:ab -n1000 -c100 -k http://127.0.0.1:9501/ This is ApacheBench, Version 2.3 <$Revision: 1843412 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking 127.0.0.1 (be patient) Completed 100 requests Completed 200 requests Completed 300 requests Completed 400 requests Completed 500 requests Completed 600 requests Completed 700 requests Completed 800 requests Completed 900 requests Completed 1000 requests Finished 1000 requests Server Software: swoole-http-server Server Hostname: 127.0.0.1 Server Port: 9501 Document Path: / Document Length: 11 bytes Concurrency Level: 100 Time taken for tests: 0.015 seconds Complete requests: 1000 Failed requests: 0 Keep-Alive requests: 1000 Total transferred: 164000 bytes HTML transferred: 11000 bytes Requests per second: 65257.11 [#/sec] (mean) Time per request: 1.532 [ms] (mean) Time per request: 0.015 [ms] (mean, across all concurrent requests) Transfer rate: 10451.33 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 0 0.8 0 4 Processing: 0 1 0.4 1 4 Waiting: 0 1 0.4 1 2 Total: 0 1 0.9 1 5 Percentage of the requests served within a certain time (ms) 50% 1 66% 1 75% 2 80% 2 90% 2 95% 4 98% 4 99% 5 100% 5 (longest request) |
结果分析
每秒处理请求数 Requests per second:
nginx:7084.76
workerman:34917.24
swoole:65257.11
90%的客户端请求处理时间:
nginx:16毫秒
workerman:3毫秒
swoole:2毫秒
从这个结果可以看出来swoole的并发能力大概是nginx的9倍,是workerman的2倍(仅是当前测试环境,生产环境会有各种复杂的场景),由此可见swoole用来做web服务器比nginx和workerman性能高很多,稳定性就不清楚怎么样了。
疑问:nginx和workerman比swoole性能低的原因是什么?性能都消耗在哪里了?