欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP框架性能测试报告

程序员文章站 2024-04-02 11:27:28
作为一个php开发者,而且是初创企业团队的技术开发者,选择开发框架是个很艰难的事情。 用thinkphp的话,招聘一个刚从培训机构出来的开发者就可以上手了,但是性能和后期...

作为一个php开发者,而且是初创企业团队的技术开发者,选择开发框架是个很艰难的事情。

用thinkphp的话,招聘一个刚从培训机构出来的开发者就可以上手了,但是性能和后期代码解耦是个让人头疼的事情。不过很多第三方功能不需要自己写,众多大牛已经给铺好路了。

用laravel的话,传说写起来很爽扩展性也够,但是学习成本有点高,总不能给初级开发者半个月的时间去学习框架吧。而且据说江湖人士透漏,laravel性能不怎么样,文档也并不是特别丰富。

用yii的话,语法有点啰嗦,前后端代码分离有点小麻烦,不过性能是非常好的,而且已经有众多国内大公司在使用了,出现意外可以快速的找到大牛答疑解惑。

不如做个简单的性能评测,选个性能出色的总不会出错

性能测试
测试时间: 2016年05月06日
测试工具: siege (因为mac系统的ab总是会出现 pr_socket_recv: connection reset by peer (54) 的错误,所以选择了siege)
测试机器: macpro core i5 处理器/8gb内存/256gb ssd闪存
测试环境: apache php5.6
框架版本: thinkphp 3.2.3 laravel 5.2 yii2.0.5
测试原则: 每次测试循环5次,取中等数据
补充说明:所有项目都是让在子目录中

结果说明

transactions:        2119 hits 处理请求总数量
availability:        96.85 % 可用性
elapsed time:        9.74 secs 运行时间
data transferred:      1.31 mb 数据传输量
response time:        0.60 secs 响应时间
transaction rate:     217.56 trans/sec 每秒处理效率
throughput:        0.13 mb/sec 每秒处理数据
concurrency:       130.28 并发
successful transactions:    2162 成功的请求
failed transactions:       69 失败的请求
longest transaction:      2.85 最长的单个请求
shortest transaction:      0.01 最短的单个请求

开始测试

增加一个控制器,并在控制器中输入10000次helloworld

并发50循环10次:

首先向我们走来的是thinkphp,看上去还行

transactions:         500 hits
availability:       100.00 %
elapsed time:        2.81 secs
data transferred:      52.45 mb
response time:        0.26 secs
transaction rate:     177.94 trans/sec
throughput:        18.67 mb/sec
concurrency:        47.10
successful transactions:     500
failed transactions:        0
longest transaction:      0.48
shortest transaction:      0.03

然后是laravle,嗯,略微有些失望

transactions:         500 hits
availability:       100.00 %
elapsed time:        13.33 secs
data transferred:      52.45 mb
response time:        1.27 secs
transaction rate:      37.51 trans/sec
throughput:        3.93 mb/sec
concurrency:        47.55
successful transactions:     500
failed transactions:        0
longest transaction:      3.64
shortest transaction:      0.07

最后赛前比较看好的yii

transactions:         500 hits
availability:       100.00 %
elapsed time:        4.84 secs
data transferred:      52.45 mb
response time:        0.46 secs
transaction rate:     103.31 trans/sec
throughput:        10.84 mb/sec
concurrency:        47.65
successful transactions:     500
failed transactions:        0
longest transaction:      0.88
shortest transaction:      0.04

并发200循环10次:

首先是thinkphp

transactions:        1977 hits
availability:        98.85 %
elapsed time:        10.03 secs
data transferred:     207.40 mb
response time:        0.95 secs
transaction rate:     197.11 trans/sec
throughput:        20.68 mb/sec
concurrency:       187.68
successful transactions:    1977
failed transactions:       23
longest transaction:      1.22
shortest transaction:      0.02

然后是laravel

transactions:        1890 hits
availability:        94.50 %
elapsed time:        51.85 secs
data transferred:     198.27 mb
response time:        4.88 secs
transaction rate:      36.45 trans/sec
throughput:        3.82 mb/sec
concurrency:       178.00
successful transactions:    1890
failed transactions:       110
longest transaction:      26.01
shortest transaction:      0.07

最后是yii

transactions:        1996 hits
availability:        99.80 %
elapsed time:        18.95 secs
data transferred:     209.39 mb
response time:        1.79 secs
transaction rate:     105.33 trans/sec
throughput:        11.05 mb/sec
concurrency:       188.57
successful transactions:    1996
failed transactions:        4
longest transaction:      3.29
shortest transaction:      0.10

没想到在没有优化的情况下,thinkphp的速度是最快的,yii略微多一些,laravel步履蹒跚的跑完了测试。结果是有些出人意料的,不过上述的测试只代表了开发环境,下面会对框架进行优化,模拟线上环境。

优化框架

thinkphp:

app_debug改为false


laravel:

app_debug改为false
php artisan route:cache
php artisan optimize
php artisan config:cache
composer dumpautoload -o

yii:

yii_debug改为false
composer dumpautoload -o
并发200循环10次:

thinkphp

transactions:        1655 hits
availability:        82.75 %
elapsed time:        8.21 secs
data transferred:     173.62 mb
response time:        0.69 secs
transaction rate:     201.58 trans/sec
throughput:        21.15 mb/sec
concurrency:       139.29
successful transactions:    1655
failed transactions:       345
longest transaction:      7.83
shortest transaction:      0.00

laravel:

transactions:        1520 hits
availability:        76.00 %
elapsed time:        34.95 secs
data transferred:     159.45 mb
response time:        3.15 secs
transaction rate:      43.49 trans/sec
throughput:        4.56 mb/sec
concurrency:       136.84
successful transactions:    1520
failed transactions:       480
longest transaction:      19.18
shortest transaction:      0.00

yii:

transactions:        1704 hits
availability:        85.20 %
elapsed time:        15.16 secs
data transferred:     178.76 mb
response time:        1.46 secs
transaction rate:     112.40 trans/sec
throughput:        11.79 mb/sec
concurrency:       164.21
successful transactions:    1704
failed transactions:       296
longest transaction:      9.04
shortest transaction:      0.00

比较奇怪的一点,当框架关掉调试模式后都出现了较多失败的情况。不过明显的看到,laravel进行简单的优化后,性能大大的提升了,但还是低于其他两个框架。

结论

在测试的过程中,其他我的心情是沮丧的,感觉自己的三观世界已经崩塌了,崩塌至渣。

thinkphp的性能比yii高出一倍,比laravel高出近四倍
yii的性能比较中庸,但是在测试中会明显发现请求失败的情况比其他两个框架要少
laravel依旧优雅,不过性能堪忧,用网友的话形容比较好 学之者生用之者死

最后

我的项目最后使用了自建框架进行开发,得益于composer的强大,开发的过程非常顺利,最后附上自有框架在200并发10次循环的数据,框架开源但是更新比较忙,欢迎在github上搜索ppphp,和我一起搭建框架。

transactions:        1672 hits
availability:        83.60 %
elapsed time:        6.18 secs
data transferred:     175.40 mb
response time:        0.57 secs
transaction rate:     270.55 trans/sec
throughput:        28.38 mb/sec
concurrency:       153.16
successful transactions:    1672
failed transactions:       328
longest transaction:      4.57
shortest transaction:      0.01