全方位解读Ruby on Rails性能测试技巧

开发 开发工具
Ruby on Rails性能测试首先需要模仿大量的数据,才能真正掌握测试数据的真实性。在这篇文章中我们会对此做一个详细的介绍。

Ruby on Rails是一个非常实用WEB开发框架。不过它的性能到底如何呢?这需要我们进行详细的测试。那么如何进行Ruby on Rails性能测试呢?#t#

Ruby on Rails性能测试1.  要进行Ruby on Rails性能测试,我们首先要模仿大量的数据,我们现在知道,在test/fixtures/目录下的yml文件里添加我们的测试数据,在运行测试时,这些数据会被加载到数据库。但是一条两条数据还可以,数据多的情况下,一条一条在yml文件里写可不行,所以,我们先看看怎样在yml文件里造大量的数据。在fixtrue目录下创建一个子目录performance,在里面新建order.yml文件,把内容改成下面的样子:

 

  1. # Read about fixtures at 
    http://ar.rubyonrails.org/
    classes/Fixtures.html  
  2. <% for i in 1..100 %> 
  3. order_<%= i %>:  
  4. id: <%= i %> 
  5. name: Fred  
  6. email: fred@flintstones.com  
  7. address: 123 Rockpile Circle  
  8. pay_type: check  
  9. <% end %> 

 

然后再运行我们一个空测试,order_test.rb

depot>ruby test/unit/order_test.rb

到数据库里查看下表order,里面已经初始化了100条记录了。我们之所以要新建一个performance目录,是因为我们不想运行每个测试都要初始化100条记录,我们之前在测试model和controller的时候用的那个order.yml文件中的记录就够了。

Ruby on Rails性能测试2.  在test目录下也创建一个performance目录,然后创建一个order_test.rb文件,内容如下:

 

  1. require File.dirname(__FILE__)
     + '/../test_helper'  
  2. require 'store_controller'  
  3. class OrderTest < Test::Unit::TestCase 
  4. fixtures :products  
  5. HOW_MANY = 100 
  6. def setup  
  7. @controller = StoreController.new  
  8. @request = ActionController:
    :TestRequest.new  
  9. @response = ActionController
    ::TestResponse.new  
  10. get :add_to_cart, :id => 1  
  11. end  
  12. def teardown  
  13. Order.delete_all  
  14. end  
  15. def test_save_bulk_orders  
  16. elapsedSeconds = Benchmark::realtime do  
  17. Fixtures.create_fixtures
    (File.dirname(__FILE__) +  
  18. "/../fixtures/performance", "orders")  
  19. assert_equal(HOW_MANY, Order.find_all.size)  
  20. 1.upto(HOW_MANY) do |id|  
  21. order = Order.find(id)  
  22. get :save_order, :order => order.attributes  
  23. assert_redirected_to :action => 'index'  
  24. assert_equal("Thank you for your
     order.", flash[:notice])  
  25. end  
  26. end  
  27. assert elapsedSeconds < 3.0
    "Actually took #{elapsedSeconds} seconds"  
  28. end  
  29. end 

在这里,我们没有直接加载100个order,而是在test_save_bulk_orders方法中,先使用elapsedSeconds = Benchmark::realtime来计算测试花费的时间,再通过调用create_fixtures方法指定我们要加载order的yml文件,然后对每条加载的order,进行保存,在通过断言判断是否调用了index的Action,和Flash中的内容。***再判断elapsedSeconds是否小于3秒。

还有一点要注意,这里实际上对每个order进行了两次Save操作,一次是在加载yml文件的时候,一次是我们调用save_order的时候。

Ruby on Rails性能测试3.  如果我们不想在每个测试运行的时候都从yml文件里加载数据,那么我们可以通过self.use_transactional_fixtures来控制。例如:

 

  1. class OrderTest 

    < Test::Unit::TestCase 
  2.  fixtures :products  
  3.  self.use_transactional
    _fixtures = true  
  4.  HOW_MANY = 100 
  5.  ……  
  6. end  

 

Ruby on Rails性能测试4.  如果我们想知道某个方法或某句代码所花费的时间,可以通过rails的脚本script/profiler and script/benchmarker来查看,例如,我们注意到Product这个Model的search方法比较慢,为了避免盲目地进行优化,我们使用Profiler来告诉我们每句代码使用了多少时间,例如:

depot>ruby script/performance/profiler "Product.salable_items"

注意这里的script的路径,我在instantrails里的和书上的不一致,如果提示脚本找不到,那就在自己的本地目录找找看profiler文件放在什么地方。

Ruby on Rails性能测试5.  我们还可以使用benchmarker来比较两个方法所消耗的时间,例如:

ruby script/performance/benchmarker 10 "Product.salable_items" "Order.count_pending"

输出结果是:

user system totalreal
#1 0.078000  0.000000  0.078000 ( 0.078000)
#2 0.000000  0.000000  0.000000 ( 0.016000)

在这里,书上写的是两个方法之间用“”来分割,在我的机器上是使用一个空格来分割。

责任编辑:曹凯 来源: 新客网
相关推荐

2009-12-15 10:10:42

Ruby过程对象

2009-12-14 17:04:13

Ruby读写UNIX命

2009-12-15 10:48:54

Ruby局部变量

2009-12-14 15:30:43

安装Ruby on R

2010-09-25 14:39:29

Bruce Tate

2009-12-16 09:29:26

Ruby布尔表达式

2015-10-10 11:00:05

RubyRails性能

2015-10-14 17:27:18

性能

2010-01-04 14:06:35

Silverlight

2009-12-17 17:37:42

Ruby on Rai

2024-04-08 11:52:08

PromQL技术监控

2009-12-15 17:28:11

Ruby自动化脚本框架

2010-07-12 09:22:05

RubyRuby on rai

2010-01-27 13:52:15

Android多媒体框

2014-06-26 17:25:22

2011-10-26 09:28:28

红帽大数据Gluster

2009-12-09 13:32:08

PHP zend安装

2009-12-16 15:23:33

Ruby on rai

2009-12-21 13:06:05

WCF Address

2010-03-17 09:22:06

FlashSilverlight
点赞
收藏

51CTO技术栈公众号