PHP框架性能测试报告

作为一个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,看上去还行

  1. Transactions: 500 hits
  2. Availability: 100.00 %
  3. Elapsed time: 2.81 secs
  4. Data transferred: 52.45 MB
  5. Response time: 0.26 secs
  6. Transaction rate: 177.94 trans/sec
  7. Throughput: 18.67 MB/sec
  8. Concurrency: 47.10
  9. Successful transactions: 500
  10. Failed transactions: 0
  11. Longest transaction: 0.48
  12. Shortest transaction: 0.03

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

  1. Transactions: 500 hits
  2. Availability: 100.00 %
  3. Elapsed time: 13.33 secs
  4. Data transferred: 52.45 MB
  5. Response time: 1.27 secs
  6. Transaction rate: 37.51 trans/sec
  7. Throughput: 3.93 MB/sec
  8. Concurrency: 47.55
  9. Successful transactions: 500
  10. Failed transactions: 0
  11. Longest transaction: 3.64
  12. Shortest transaction: 0.07

最后赛前比较看好的Yii

  1. Transactions: 500 hits
  2. Availability: 100.00 %
  3. Elapsed time: 4.84 secs
  4. Data transferred: 52.45 MB
  5. Response time: 0.46 secs
  6. Transaction rate: 103.31 trans/sec
  7. Throughput: 10.84 MB/sec
  8. Concurrency: 47.65
  9. Successful transactions: 500
  10. Failed transactions: 0
  11. Longest transaction: 0.88
  12. Shortest transaction: 0.04

并发200循环10次:

首先是ThinkPHP

  1. Transactions: 1977 hits
  2. Availability: 98.85 %
  3. Elapsed time: 10.03 secs
  4. Data transferred: 207.40 MB
  5. Response time: 0.95 secs
  6. Transaction rate: 197.11 trans/sec
  7. Throughput: 20.68 MB/sec
  8. Concurrency: 187.68
  9. Successful transactions: 1977
  10. Failed transactions: 23
  11. Longest transaction: 1.22
  12. Shortest transaction: 0.02

然后是Laravel

  1. Transactions: 1890 hits
  2. Availability: 94.50 %
  3. Elapsed time: 51.85 secs
  4. Data transferred: 198.27 MB
  5. Response time: 4.88 secs
  6. Transaction rate: 36.45 trans/sec
  7. Throughput: 3.82 MB/sec
  8. Concurrency: 178.00
  9. Successful transactions: 1890
  10. Failed transactions: 110
  11. Longest transaction: 26.01
  12. Shortest transaction: 0.07

最后是Yii

  1. Transactions: 1996 hits
  2. Availability: 99.80 %
  3. Elapsed time: 18.95 secs
  4. Data transferred: 209.39 MB
  5. Response time: 1.79 secs
  6. Transaction rate: 105.33 trans/sec
  7. Throughput: 11.05 MB/sec
  8. Concurrency: 188.57
  9. Successful transactions: 1996
  10. Failed transactions: 4
  11. Longest transaction: 3.29
  12. Shortest transaction: 0.10

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

优化框架

ThinkPHP:

APP_DEBUG改为false

Laravel:

  1. APP_DEBUG改为false
  2. php artisan route:cache
  3. php artisan optimize
  4. php artisan config:cache
  5. composer dumpautoload -o

Yii:

YII_DEBUG改为false

composer dumpautoload -o

并发200循环10次:

ThinkPHP

  1. Transactions: 1655 hits
  2. Availability: 82.75 %
  3. Elapsed time: 8.21 secs
  4. Data transferred: 173.62 MB
  5. Response time: 0.69 secs
  6. Transaction rate: 201.58 trans/sec
  7. Throughput: 21.15 MB/sec
  8. Concurrency: 139.29
  9. Successful transactions: 1655
  10. Failed transactions: 345
  11. Longest transaction: 7.83
  12. Shortest transaction: 0.00

Laravel:

  1. Transactions: 1520 hits
  2. Availability: 76.00 %
  3. Elapsed time: 34.95 secs
  4. Data transferred: 159.45 MB
  5. Response time: 3.15 secs
  6. Transaction rate: 43.49 trans/sec
  7. Throughput: 4.56 MB/sec
  8. Concurrency: 136.84
  9. Successful transactions: 1520
  10. Failed transactions: 480
  11. Longest transaction: 19.18
  12. Shortest transaction: 0.00

Yii:

  1. Transactions: 1704 hits
  2. Availability: 85.20 %
  3. Elapsed time: 15.16 secs
  4. Data transferred: 178.76 MB
  5. Response time: 1.46 secs
  6. Transaction rate: 112.40 trans/sec
  7. Throughput: 11.79 MB/sec
  8. Concurrency: 164.21
  9. Successful transactions: 1704
  10. Failed transactions: 296
  11. Longest transaction: 9.04
  12. Shortest transaction: 0.00

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

结论

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

ThinkPHP的性能比Yii高出一倍,比Laravel高出近四倍

Yii的性能比较中庸,但是在测试中会明显发现请求失败的情况比其他两个框架要少

Laravel依旧优雅,不过性能堪忧,用网友的话形容比较好 学之者生用之者死

最后

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

  1. Transactions: 1672 hits
  2. Availability: 83.60 %
  3. Elapsed time: 6.18 secs
  4. Data transferred: 175.40 MB
  5. Response time: 0.57 secs
  6. Transaction rate: 270.55 trans/sec
  7. Throughput: 28.38 MB/sec
  8. Concurrency: 153.16
  9. Successful transactions: 1672
  10. Failed transactions: 328
  11. Longest transaction: 4.57
  12. Shortest transaction: 0.01