PHP 性能最好的應(yīng)該就是原生代碼了吧,真的么貌夕?MixPHP 是基于 Swoole 擴(kuò)展的高性能 PHP 框架律歼,今天我來(lái)做個(gè)對(duì)比測(cè)試,一行代碼 VS 六千多行代碼啡专。
環(huán)境
虛擬機(jī): 4 核险毁,1G
使用 ab 工具壓測(cè),命令:ab -n 10000 -c 300 URL
原生 PHP
Apache worker模式们童,mpm配置如下:
<IfModule worker.c>
ServerLimit 50
ThreadLimit 200
StartServers 5
MaxClients 5000
MinSpareThreads 25
MaxSpareThreads 500
ThreadsPerChild 100
MaxRequestsPerChild 0
</IfModule>
只有一行代碼畔况,輸出一個(gè) Hello World。
<?php
echo 'Hello World';
MixPHP
默認(rèn)控制器代碼如下慧库,輸出一個(gè) Hello World跷跪。
public function actionIndex()
{
return 'Hello World';
}
開(kāi)始測(cè)試
首先測(cè)試原生 PHP,QPS: 1147.96
C:\Server\apache24vc11\bin>ab -n 10000 -c 300 http://www.p.com/
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.p.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: Apache/2.2.32
Server Hostname: www.p.com
Server Port: 80
Document Path: /
Document Length: 9 bytes
Concurrency Level: 300
Time taken for tests: 8.711 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2010000 bytes
HTML transferred: 90000 bytes
Requests per second: 1147.96 [#/sec] (mean)
Time per request: 261.333 [ms] (mean)
Time per request: 0.871 [ms] (mean, across all concurrent requests)
Transfer rate: 225.33 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.5 1 32
Processing: 10 254 23.0 257 298
Waiting: 4 142 71.9 142 278
Total: 10 255 23.0 258 299
Percentage of the requests served within a certain time (ms)
50% 258
66% 260
75% 262
80% 263
90% 267
95% 272
98% 279
99% 288
100% 299 (longest request)
然后測(cè)試 MixPHP齐板,QPS: 1296.35
C:\Server\apache24vc11\bin>ab -n 10000 -c 300 http://www.v.com:9501/
This is ApacheBench, Version 2.3 <$Revision: 1757674 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.v.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: swoole-http-server
Server Hostname: www.v.com
Server Port: 9501
Document Path: /
Document Length: 12 bytes
Concurrency Level: 300
Time taken for tests: 7.714 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 1600000 bytes
HTML transferred: 120000 bytes
Requests per second: 1296.35 [#/sec] (mean)
Time per request: 231.419 [ms] (mean)
Time per request: 0.771 [ms] (mean, across all concurrent requests)
Transfer rate: 202.55 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 1 0.4 1 5
Processing: 10 227 23.0 231 261
Waiting: 7 138 61.8 142 250
Total: 11 228 23.0 231 262
Percentage of the requests served within a certain time (ms)
50% 231
66% 235
75% 236
80% 237
90% 241
95% 243
98% 245
99% 247
100% 262 (longest request)
結(jié)論
是不是測(cè)試結(jié)果讓你驚訝吵瞻,一行代碼 VS 六千多行代碼,竟然是 MixPHP 框架并發(fā)性能更強(qiáng)甘磨。
我來(lái)分析下原因:MixPHP 框架的核心組件全部都是常駐內(nèi)存的橡羞,也就是說(shuō),六千多行代碼在每次請(qǐng)求時(shí)济舆,實(shí)際上只執(zhí)行了其中很小的一部分代碼卿泽,當(dāng)然也還是要比一行代碼多很多的,但是為何性能上還是要更強(qiáng)滋觉,這個(gè)就要?dú)w功于 swoole_http_server 了签夭,在并發(fā)性能上他是優(yōu)于 Apache 的,所以才使得 MixPHP 的性能可以超越原生 PHP椎瘟。
不信?你也來(lái)測(cè)試一下吧侄旬,https://github.com/mixstart/mixphp
ITEM | DESC |
---|---|
原生 PHP | 1147.96 QPS |
MixPHP | 1296.35 QPS |