身為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