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

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

全方位解读Ruby on Rails性能测试技巧(ruby rails入门)  Ruby Rails性能测试 第1张

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

  1. #Readaboutfixturesat
    http://ar.Rubyonrails.org/
    classes/Fixtures.html
  2. <%foriin1..100%>
  3. order_<%=i%>:
  4. id:<%=i%>
  5. name:Fred
  6. email:fred@flintstones.com
  7. address:123RockpileCircle
  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. requireFile.dirname(__FILE__)
    +'/../test_helper'
  2. require'store_controller'
  3. classOrderTest<Test::Unit::TestCase
  4. fixtures:products
  5. HOW_MANY=100
  6. defsetup
  7. @controller=StoreController.new
  8. @request=ActionController:
    :TestRequest.new
  9. @response=ActionController
    ::TestResponse.new
  10. get:add_to_cart,:id=>1
  11. end
  12. defteardown
  13. Order.delete_all
  14. end
  15. deftest_save_bulk_orders
  16. elapsedSeconds=Benchmark::realtimedo
  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("Thankyouforyour
    order.",flash[:notice])
  25. end
  26. end
  27. assertelapsedSeconds<3.0,
    "Actuallytook#{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. classOrderTest

    <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)

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

转载请说明出处
知优网 » 全方位解读Ruby on Rails性能测试技巧(ruby on rails入门)

发表评论

您需要后才能发表评论