自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

橫向壓力測試:Ruby on Rails PK CakePHP

開發(fā) 后端
文章介紹了PHP開發(fā)中常用框架CakePHP與著名的MVC框架Ruby on Rails的橫向壓測,壓力測試的目標(biāo)集中在Ruby On Rails和CakePHP的效率。

Ruby on Rails以優(yōu)雅的MVC架構(gòu)聞名,這個架構(gòu)如此誘人和美麗,而CakePHP則是PHP開發(fā)中常用的框架之一。如果你不想束縛于傳統(tǒng)的PHP的磚頭式開發(fā),那么你可以嘗試轉(zhuǎn)向MVC架構(gòu),不過Rails的性能和部署問題一直讓人擔(dān)心。

兩者對比的話題在網(wǎng)上眾說紛紜,很少見到客觀而有說服力的論證和充分模擬實(shí)際環(huán)境下的壓力測評。作為架構(gòu)選型的重要決定,我們既不能人云亦云,更不可憑空臆想,一定要有充分的測試數(shù)據(jù)才能幫助做出正確的決定。

心動不如行動,立刻著手安排了仿真環(huán)境測試。第一步是設(shè)計測試方案:

壓力測試的目標(biāo)集中在Ruby On Rails和CakePHP的效率,所以采用同樣的Nginx生產(chǎn)環(huán)境,但避開所有數(shù)據(jù)庫操作以避免瓶頸轉(zhuǎn)嫁到數(shù)據(jù)庫影響結(jié)果。

代碼的主要部分都是通過輸出128000個4位的十進(jìn)制隨機(jī)數(shù),來模擬總計約500KB的頁面數(shù)據(jù)輸出。調(diào)用的指令都很基本,對腳本測試來說很公平。

不過既然是虛擬高壓力測試,實(shí)際環(huán)境中數(shù)據(jù)庫讀寫等操作的時間開銷應(yīng)該有一個仿真替代,所以通過Sleep 200ms來仿真具有高度數(shù)據(jù)壓力的服務(wù)端。當(dāng)然我們都知道Sleep是沒有真實(shí)的cpu開銷的,所以不會影響測試結(jié)果的公平。

測試工具使用經(jīng)典的ApacheBench。先后測試10并發(fā)100請求(-c 10 -n 100) 的中等壓力,和200并發(fā)5000請求(-c 200 -n 5000)高壓測試。

環(huán)境

  1. OS: FreeBSD 8.1  
  2. CPU: Intel 4核心 Core 2  
  3. RAM: 4GB 內(nèi)存  
  4. PHP環(huán)境:nginx+php-fpm(5.3.3)+APC  
  5. Rails環(huán)境:nginx+passenger+Ruby(1.8.7) on Rails(3.0.0)  
  6. 所有軟件均使用ports安裝 

fpm的優(yōu)化配置:

  1. pm.max_children = 1000 
  2. pm.start_servers = 20 
  3. pm.min_spare_servers = 5 
  4. pm.max_spare_servers = 1000 

passenger的優(yōu)化配置(nginx.conf):

passenger_max_pool_size  300;//4GB內(nèi)存最大的允許值,再追加便無法啟動passenger

通過Rails腳本創(chuàng)建Test App:

rails new dummy

Ruby on Rails 代碼:

  1. // app/controller/test_controller.rb  
  2. class TestController < ApplicationController 
  3.   def index  
  4.     sleep(0.2)  
  5.   end  
  6. end// app/views/test/index.html.rb  
  7. <% 128000.times do %><%=rand(8999)+1000%><% end %> 

PHP代碼:

  1. // vsruby.php  
  2. php   
  3.  
  4. usleep(200000);  
  5. echo "<html><head>head><body>";  
  6. for($i = 0; $i < 128000;$i++)  
  7. {  
  8.   echo mt_rand(8999,9999);  
  9. }  
  10.  
  11. echo "body>html>"; 

CakePHP代碼:

  1. // CakePHP  
  2. // app/controller/test_controller.php  
  3. php 
  4.   class TestController extends AppController {  
  5.    var $name = 'Test';  
  6.    function index()  
  7.    {  
  8.     usleep(200000);  
  9.    }  
  10.   } // CakePHP  
  11. // app/views/test/index.ctp  
  12. php 
  13. for($i = 0; $i < 128000;$i++)  
  14. {  
  15.   echo mt_rand(8999,9999);  
  16. }  
  17. ?> 

#p#

10并發(fā)100個請求:

  1. // Ruby on Rails  
  2. // CPU usage: 100%  
  3. Server Software:        nginx/0.8.52  
  4. Server Hostname:        127.0.0.1  
  5. Server Port:            80  
  6.  
  7. Document Path:          /test/  
  8. Document Length:        512731 bytes  
  9.  
  10. Concurrency Level:      10  
  11. Time taken for tests:   40.939 seconds  
  12. Complete requests:      100  
  13. Failed requests:        0  
  14. Write errors:           0  
  15. Total transferred:      51334500 bytes  
  16. HTML transferred:       51273100 bytes  
  17. Requests per second:    2.44 [#/sec] (mean)  
  18. Time per request:       4093.898 [ms] (mean)  
  19. Time per request:       409.390 [ms] (mean, across all concurrent requests)  
  20. Transfer rate:          1224.54 [Kbytes/sec] received  
  21.  
  22. Connection Times (ms)  
  23.               min  mean[+/-sd] median   max  
  24. Connect:        0    0   0.0      0       0  
  25. Processing:  1231 4036 3167.1   3149   16396  
  26. Waiting:     1203 2428 2533.7   1625   15683  
  27. Total:       1231 4036 3167.1   3150   16396  
  28.  
  29. Percentage of the requests served within a certain time (ms)  
  30.   50%   3150  
  31.   66%   3353  
  32.   75%   3679  
  33.   80%   3893  
  34.   90%  12307  
  35.   95%  12307  
  36.   98%  16108  
  37.   99%  16396  
  38.  100%  16396 (longest request)//php  
  39. //CPU usage: 20-30%  
  40.  
  41. Server Software:        nginx/0.8.52  
  42. Server Hostname:        127.0.0.1  
  43. Server Port:            80  
  44.  
  45. Document Path:          /php/  
  46. Document Length:        512039 bytes  
  47.  
  48. Concurrency Level:      10  
  49. Time taken for tests:   4.144 seconds  
  50. Complete requests:      100  
  51. Failed requests:        0  
  52. Write errors:           0  
  53. Total transferred:      51218600 bytes  
  54. HTML transferred:       51203900 bytes  
  55. Requests per second:    24.13 [#/sec] (mean)  
  56. Time per request:       414.389 [ms] (mean)  
  57. Time per request:       41.439 [ms] (mean, across all concurrent requests)  
  58. Transfer rate:          12070.36 [Kbytes/sec] received  
  59.  
  60. Connection Times (ms)  
  61.               min  mean[+/-sd] median   max  
  62. Connect:        0    0   0.1      0       0  
  63. Processing:   400  405  14.0    403     502  
  64. Waiting:      201  205   3.1    204     218  
  65. Total:        400  405  14.0    403     502  
  66.  
  67. Percentage of the requests served within a certain time (ms)  
  68.   50%    403  
  69.   66%    404  
  70.   75%    405  
  71.   80%    405  
  72.   90%    408  
  73.   95%    409  
  74.   98%    501  
  75.   99%    502  
  76.  100%    502 (longest request)// CakePHP  
  77.  
  78. Server Software:        nginx/0.8.52  
  79. Server Hostname:        127.0.0.1  
  80. Server Port:            80  
  81.  
  82. Document Path:          /cakephp/  
  83. Document Length:        512652 bytes  
  84.  
  85. Concurrency Level:      10  
  86. Time taken for tests:   4.036 seconds  
  87. Complete requests:      100  
  88. Failed requests:        0  
  89. Write errors:           0  
  90. Total transferred:      51291900 bytes  
  91. HTML transferred:       51265200 bytes  
  92. Requests per second:    24.78 [#/sec] (mean)  
  93. Time per request:       403.553 [ms] (mean)  
  94. Time per request:       40.355 [ms] (mean, across all concurrent requests)  
  95. Transfer rate:          12412.20 [Kbytes/sec] received  
  96.  
  97. Connection Times (ms)  
  98.               min  mean[+/-sd] median   max  
  99. Connect:        0    0   0.7      0       6  
  100. Processing:   302  399 119.1    363     775  
  101. Waiting:      275  370 119.9    340     764  
  102. Total:        302  400 119.1    364     775  
  103.  
  104. Percentage of the requests served within a certain time (ms)  
  105.   50%    364  
  106.   66%    372  
  107.   75%    378  
  108.   80%    381  
  109.   90%    725  
  110.   95%    755  
  111.   98%    775  
  112.   99%    775  
  113.  100%    775 (longest request) 

5000個請求,200并發(fā)數(shù):

  1. // php  
  2. Server Software:        nginx/0.8.52  
  3. Server Hostname:        127.0.0.1  
  4. Server Port:            80  
  5.  
  6. Document Path:          /php/  
  7. Document Length:        512039 bytes  
  8.  
  9. Concurrency Level:      200  
  10. Time taken for tests:   82.243 seconds  
  11. Complete requests:      5000  
  12. Failed requests:        0  
  13. Write errors:           0  
  14. Total transferred:      2560930000 bytes  
  15. HTML transferred:       2560195000 bytes  
  16. Requests per second:    60.80 [#/sec] (mean)  
  17. Time per request:       3289.722 [ms] (mean)  
  18. Time per request:       16.449 [ms] (mean, across all concurrent requests)  
  19. Transfer rate:          30408.75 [Kbytes/sec] received  
  20.  
  21. Connection Times (ms)  
  22.               min  mean[+/-sd] median   max  
  23. Connect:        0    1   1.6      0      20  
  24. Processing:   405 3258 4830.3   2675   56787  
  25. Waiting:      202 1048 1324.8    344   53432  
  26. Total:        405 3259 4830.3   2676   56787  
  27.  
  28. Percentage of the requests served within a certain time (ms)  
  29.   50%   2676  
  30.   66%   3081  
  31.   75%   3361  
  32.   80%   3535  
  33.   90%   3828  
  34.   95%   4262  
  35.   98%   5709  
  36.   99%  31863  
  37.  100%  56787 (longest request) // CakePHP  
  38.  
  39. Server Software:        nginx/0.8.52  
  40. Server Hostname:        127.0.0.1  
  41. Server Port:            80  
  42.  
  43. Document Path:          /cakephp/  
  44. Document Length:        512652 bytes  
  45.  
  46. Concurrency Level:      200  
  47. Time taken for tests:   99.652 seconds  
  48. Complete requests:      5000  
  49. Failed requests:        0  
  50. Write errors:           0  
  51. Total transferred:      2565102923 bytes  
  52. HTML transferred:       2563767656 bytes  
  53. Requests per second:    50.17 [#/sec] (mean)  
  54. Time per request:       3986.073 [ms] (mean)  
  55. Time per request:       19.930 [ms] (mean, across all concurrent requests)  
  56. Transfer rate:          25137.36 [Kbytes/sec] received  
  57.  
  58. Connection Times (ms)  
  59.               min  mean[+/-sd] median   max  
  60. Connect:        0    4  57.8      0    1663  
  61. Processing:   367 3969 1825.7   3857   10630  
  62. Waiting:      280 1543 731.9   1297    3953  
  63. Total:        472 3973 1824.8   3860   10630  
  64.  
  65. Percentage of the requests served within a certain time (ms)  
  66.   50%   3860  
  67.   66%   4466  
  68.   75%   5065  
  69.   80%   5426  
  70.   90%   6482  
  71.   95%   7337  
  72.   98%   8599  
  73.   99%   8847  
  74.  100%  10630 (longest request)  
  75. // Rails  
  76. //約10分鐘后,服務(wù)器進(jìn)入假死狀態(tài)。 

#p#

Rails與CakePHP對比

備注:

因?yàn)椴惶嘈舝uby的性能會有這樣大的差距,懷疑是否ruby的rand()效率格外的低造成問題,我在測試完成又將rand()去掉,改為直接輸出數(shù)字,但腳本執(zhí)行時間并沒有明顯縮短。所以應(yīng)該說是 ruby 對循環(huán)或數(shù)據(jù)輸出的處理效率不佳導(dǎo)致。

結(jié)論

坦白說,幾個ab測試跑下來,ruby的成績?nèi)绱酥?,不?0并發(fā)的100請求就已經(jīng)用滿了服務(wù)器資源,更甚至沒有能通過200并發(fā)5000請求的高壓測試,這把我自己也嚇了一跳。想到堅持在使用Rails的twitter,心中的敬佩油然而生。不知道那是什么樣的硬件或軟件優(yōu)化,才可以用Ruby來支撐那樣巨大的訪問量。

客觀的從純性能的角度出發(fā),在生產(chǎn)環(huán)境中,Ruby/Rails還是只適合Small Business。對與壓力較高的服務(wù)或應(yīng)用,就必須投入大量額外的硬件資源才能維持。本次測試中,Ruby On Rails與CakePHP的性能差距達(dá)到10倍之多,實(shí)在讓我不敢考慮把Rails用在生產(chǎn)環(huán)境中。另一方面,PHP依托龐大的社區(qū),多年來積累了眾多的優(yōu)化手段,其性能領(lǐng)先也有它的道理。在PHP架構(gòu)之上的MVC候選人CakePHP,性能雖然相對于傳統(tǒng)php的代碼書寫方法略有損失,但這個損失不到10%。所以對于在考慮MVC架構(gòu)的php用戶來說,CakePHP的性能完全在可以接受的范圍內(nèi)。

原文鏈接:http://blog.splayer.org/index.php/2010/10/performance-penalty-in-mvc-web-deployment/

所有測試源代碼和nginx/php-fpm配置文件下載:http://blog.splayer.org/wp-content/uploads/2010/10/FBSD-SandBox.zip

【編輯推薦】

  1. Ruby On Rails開發(fā)教程
  2. 在Nginx上運(yùn)行Ruby on Rails
  3. Ruby on Rails性能優(yōu)化七劍
  4. 詳細(xì)剖析Ruby on Rails配置文件

 

責(zé)任編輯:王曉東 來源: 射手科技官方博客
相關(guān)推薦

2009-08-27 10:21:22

Ruby on Rai

2009-12-16 17:07:27

Ruby on Rai

2009-08-06 09:13:36

Ruby on Rai

2015-10-10 11:00:05

RubyRails性能

2009-12-16 16:37:59

Ruby on Rai

2015-10-14 17:27:18

性能

2009-12-17 14:29:50

Ruby on Rai

2009-12-14 15:30:43

安裝Ruby on R

2010-10-09 08:58:03

NginxRuby on Rai

2009-09-29 17:04:29

2009-12-16 16:24:00

Ruby on Rai

2010-07-12 09:22:05

RubyRuby on rai

2013-03-28 12:42:02

RubyRails

2009-12-16 15:23:33

Ruby on rai

2009-12-16 15:41:10

Ruby on Rai

2009-12-16 17:37:31

Ruby on Rai

2009-12-17 17:37:42

Ruby on Rai

2010-09-25 14:39:29

Bruce Tate

2009-12-18 15:16:49

Ruby on Rai

2009-12-14 15:37:35

Ruby on Rai
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號