好几年没有做过性能比照了,因为越来越感觉性能并没有那么的重要(绝对于生态),明天有工夫简略测试一下,因为 Mix v2.1 开始就全副切换为单过程协程模式,因而本次次要测试的是 CoHttpServer 。
环境
- CPU: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
- CPU(s): 12
- Mem: 15G
- Linux version 3.10.0-957.10.1.el7.x86_64
PHP 7.3.12 + Swoole 4.4.14
代码中应用的单过程 CoHttpServer ,因而须要利用端口复用 (须要 Linux >= 3.10),开启 12 个过程
- 代码
<?php \Swoole\Process::daemon();$scheduler = new \Swoole\Coroutine\Scheduler;$scheduler->set([ 'hook_flags' => SWOOLE_HOOK_ALL,]);$scheduler->add(function () { $server = new \Swoole\Coroutine\Http\Server('0.0.0.0', 8888, false, true); $server->handle('/', function($request, $response){ $response->end('hello, world!'); }); $server->start();});$scheduler->start();
- 开启的过程
[nobody@tmp]$ ps -ef | grep test.phpnobody 1917 1 0 20:22 ? 00:00:02 /usr/local/php-7.3.12/bin/php test.phpnobody 1923 1 0 20:22 ? 00:00:02 /usr/local/php-7.3.12/bin/php test.phpnobody 1929 1 0 20:22 ? 00:00:02 /usr/local/php-7.3.12/bin/php test.phpnobody 1934 1 0 20:22 ? 00:00:02 /usr/local/php-7.3.12/bin/php test.phpnobody 2154 1 0 20:22 ? 00:00:02 /usr/local/php-7.3.12/bin/php test.phpnobody 2166 1 0 20:22 ? 00:00:02 /usr/local/php-7.3.12/bin/php test.phpnobody 2173 1 0 20:22 ? 00:00:02 /usr/local/php-7.3.12/bin/php test.phpnobody 2181 1 0 20:22 ? 00:00:02 /usr/local/php-7.3.12/bin/php test.phpnobody 2187 1 0 20:22 ? 00:00:02 /usr/local/php-7.3.12/bin/php test.phpnobody 2194 1 0 20:22 ? 00:00:02 /usr/local/php-7.3.12/bin/php test.phpnobody 2200 1 0 20:22 ? 00:00:02 /usr/local/php-7.3.12/bin/php test.phpnobody 2205 1 0 20:22 ? 00:00:02 /usr/local/php-7.3.12/bin/php test.php
- 测试:多跑几次,根本稳固在
127441.95
左右。
[nobody@~]$ ab -n 100000 -c 1000 -k http://127.0.0.1:8888/This is ApacheBench, Version 2.3 <$Revision: 1430300 $>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 10000 requestsCompleted 20000 requestsCompleted 30000 requestsCompleted 40000 requestsCompleted 50000 requestsCompleted 60000 requestsCompleted 70000 requestsCompleted 80000 requestsCompleted 90000 requestsCompleted 100000 requestsFinished 100000 requestsServer Software: swoole-http-serverServer Hostname: 127.0.0.1Server Port: 8888Document Path: /Document Length: 13 bytesConcurrency Level: 1000Time taken for tests: 0.785 secondsComplete requests: 100000Failed requests: 0Write errors: 0Keep-Alive requests: 100000Total transferred: 16600000 bytesHTML transferred: 1300000 bytesRequests per second: 127441.95 [#/sec] (mean)Time per request: 7.847 [ms] (mean)Time per request: 0.008 [ms] (mean, across all concurrent requests)Transfer rate: 20659.53 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 0 2.4 0 47Processing: 2 7 0.5 7 47Waiting: 0 7 0.4 7 14Total: 2 8 2.6 7 58Percentage of the requests served within a certain time (ms) 50% 7 66% 7 75% 7 80% 7 90% 8 95% 8 98% 8 99% 18 100% 58 (longest request)
Go 1.13.4
Golang 默认应用全副 CPU 核,因而只需开启一个过程即可。
- 代码
package mainimport ( "fmt" "net/http")func main() { http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { writer.Write([]byte("hello, world!")) }) err := http.ListenAndServe(":9999", nil) if err != nil{ fmt.Println(err) }}
- 开启的过程
[nobody@~]$ ps -ef | grep gotestnobody 4409 1859 0 20:25 pts/31 00:00:06 ./gotest_linux
- 测试:多跑几次,根本稳固在
121575.23
左右,比 Swoole 略微差点,但十分靠近,PHP 是动静语言借助 Swoole 做到这个性能的确是十分夸大了。
[nobody@~]$ ab -n 100000 -c 1000 -k http://127.0.0.1:9999/This is ApacheBench, Version 2.3 <$Revision: 1430300 $>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 10000 requestsCompleted 20000 requestsCompleted 30000 requestsCompleted 40000 requestsCompleted 50000 requestsCompleted 60000 requestsCompleted 70000 requestsCompleted 80000 requestsCompleted 90000 requestsCompleted 100000 requestsFinished 100000 requestsServer Software:Server Hostname: 127.0.0.1Server Port: 9999Document Path: /Document Length: 13 bytesConcurrency Level: 1000Time taken for tests: 0.823 secondsComplete requests: 100000Failed requests: 0Write errors: 0Keep-Alive requests: 100000Total transferred: 15400000 bytesHTML transferred: 1300000 bytesRequests per second: 121575.23 [#/sec] (mean)Time per request: 8.225 [ms] (mean)Time per request: 0.008 [ms] (mean, across all concurrent requests)Transfer rate: 18283.77 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 0 2.4 0 46Processing: 2 8 1.1 7 46Waiting: 0 8 1.1 7 30Total: 2 8 2.7 7 56Percentage of the requests served within a certain time (ms) 50% 7 66% 8 75% 9 80% 9 90% 9 95% 9 98% 10 99% 18 100% 56 (longest request)
MixPHP V2.2
接下来咱们看一下跑在 PHP7.3 + Swoole4.4 下的 Mix V2.2 能跑多少。
- 开启的过程
[nobody@tmp]$ ps -ef | grep mix.phpnobody 24783 1 0 20:51 ? 00:00:00 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 24801 1 0 20:51 ? 00:00:00 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 24821 1 0 20:51 ? 00:00:00 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 24839 1 0 20:51 ? 00:00:00 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 24856 1 0 20:51 ? 00:00:00 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 24873 1 0 20:51 ? 00:00:00 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 24891 1 0 20:51 ? 00:00:00 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 24908 1 0 20:51 ? 00:00:00 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 24927 1 0 20:51 ? 00:00:00 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 24946 1 0 20:51 ? 00:00:00 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 24963 1 0 20:51 ? 00:00:00 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 24981 1 0 20:51 ? 00:00:00 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -d
- 测试:多跑几次,根本稳固在
110050.47
左右,比原生 Swoole 升高了13.6%
,整个框架的代码只升高了这个比例,还是蛮能够的。
[nobody@~]$ ab -n 100000 -c 1000 -k http://127.0.0.1:9501/This is ApacheBench, Version 2.3 <$Revision: 1430300 $>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 10000 requestsCompleted 20000 requestsCompleted 30000 requestsCompleted 40000 requestsCompleted 50000 requestsCompleted 60000 requestsCompleted 70000 requestsCompleted 80000 requestsCompleted 90000 requestsCompleted 100000 requestsFinished 100000 requestsServer Software: swoole-http-serverServer Hostname: 127.0.0.1Server Port: 9501Document Path: /Document Length: 13 bytesConcurrency Level: 1000Time taken for tests: 0.909 secondsComplete requests: 100000Failed requests: 0Write errors: 0Keep-Alive requests: 100000Total transferred: 18100000 bytesHTML transferred: 1300000 bytesRequests per second: 110050.47 [#/sec] (mean)Time per request: 9.087 [ms] (mean)Time per request: 0.009 [ms] (mean, across all concurrent requests)Transfer rate: 19452.28 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 0 2.5 0 45Processing: 2 9 1.6 9 46Waiting: 0 9 1.6 9 25Total: 2 9 3.2 9 58Percentage of the requests served within a certain time (ms) 50% 9 66% 9 75% 9 80% 9 90% 9 95% 10 98% 17 99% 23 100% 58 (longest request)
我尝试缩小过程测试:
- 当缩小到 5 个过程时达到最高性能
[nobody@tmp]$ ps -ef | grep mix.phpnobody 24946 1 0 20:51 ? 00:00:15 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 24963 1 0 20:51 ? 00:00:15 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 24981 1 0 20:51 ? 00:00:15 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 27471 1 22 21:35 ? 00:00:05 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -dnobody 27522 1 18 21:35 ? 00:00:03 /usr/local/php-7.3.12/bin/php mix/bin/mix.php web -r -d
- 测试:多跑几次,根本稳固在
114070.87
左右,比 12 个过程的时候还高一点,证实再多开过程曾经无奈晋升性能了,12 核的 CPU 为何会这样呢?
[nobody@~]$ ab -n 100000 -c 1000 -k http://127.0.0.1:9501/This is ApacheBench, Version 2.3 <$Revision: 1430300 $>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 10000 requestsCompleted 20000 requestsCompleted 30000 requestsCompleted 40000 requestsCompleted 50000 requestsCompleted 60000 requestsCompleted 70000 requestsCompleted 80000 requestsCompleted 90000 requestsCompleted 100000 requestsFinished 100000 requestsServer Software: swoole-http-serverServer Hostname: 127.0.0.1Server Port: 9501Document Path: /Document Length: 13 bytesConcurrency Level: 1000Time taken for tests: 0.877 secondsComplete requests: 100000Failed requests: 0Write errors: 0Keep-Alive requests: 100000Total transferred: 18100000 bytesHTML transferred: 1300000 bytesRequests per second: 114070.87 [#/sec] (mean)Time per request: 8.766 [ms] (mean)Time per request: 0.009 [ms] (mean, across all concurrent requests)Transfer rate: 20162.92 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 0 2.0 0 33Processing: 0 8 3.0 8 33Waiting: 0 8 3.0 8 24Total: 0 9 3.7 8 43Percentage of the requests served within a certain time (ms) 50% 8 66% 9 75% 10 80% 11 90% 12 95% 13 98% 18 99% 21 100% 43 (longest request)
总结一下:
语言框架 | 过程数 | 数值 |
---|---|---|
PHP 7.3.12 + Swoole 4.4.14 | 12 | 127441.95 |
Go 1.13.4 | 1 | 121575.23 |
PHP 7.3.12 + Swoole 4.4.14 + MixPHP V2.2 | 12 | 110050.47 |
PHP 7.3.12 + Swoole 4.4.14 + MixPHP V2.2 | 5 | 114070.87 |