PHP 性能最好的应该就是原生代码了吧,真的么?MixPHP 是基于 Swoole 扩展的高性能 PHP 框架,今天我来做个对比测试,一行代码 VS 六千多行代码。
环境
虚拟机: 4 核,1G
使用 ab 工具压测,命令: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>
只有一行代码,输出一个 Hello World。
<?php
echo 'Hello World';
MixPHP
默认控制器代码如下,输出一个 Hello World。
public function actionIndex()
{
return 'Hello World';
}
开始测试
首先测试原生 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)
然后测试 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)
结论
是不是测试结果让你惊讶,一行代码 VS 六千多行代码,竟然是 MixPHP 框架并发性能更强。
我来分析下原因:MixPHP 框架的核心组件全部都是常驻内存的,也就是说,六千多行代码在每次请求时,实际上只执行了其中很小的一部分代码,当然也还是要比一行代码多很多的,但是为何性能上还是要更强,这个就要归功于 swoole_http_server 了,在并发性能上他是优于 Apache 的,所以才使得 MixPHP 的性能可以超越原生 PHP。
不信?你也来测试一下吧,https://github.com/mixstart/mixphp
ITEM | DESC |
---|---|
原生 PHP | 1147.96 QPS |
MixPHP | 1296.35 QPS |
网友评论