zoukankan      html  css  js  c++  java
  • Ruby on rails开发从头来(windows)(二十九) 性能测试

    Rails所针对的是Web项目,必须要考虑大访问量的情况,所以我们来看看在Rails怎样进行性能测试。

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

    # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html

    <% for i in 1..100 %>

    order_<%= i %>:

        id: <%= i %>

        name: Fred

        email: fred@flintstones.com

        address: 123 Rockpile Circle

        pay_type: check

    <% end %>

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

    depot>ruby test/unit/order_test.rb

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

    2.       test目录下也创建一个performance目录,然后创建一个order_test.rb文件,内容如下:

    require File.dirname(__FILE__) + '/../test_helper'

    require 'store_controller'

    class OrderTest < Test::Unit::TestCase

      fixtures :products

      HOW_MANY = 100

      def setup

        @controller = StoreController.new

        @request = ActionController::TestRequest.new

        @response = ActionController::TestResponse.new

        get :add_to_cart, :id => 1

      end

     

      def teardown

        Order.delete_all

      end

     

      def test_save_bulk_orders

        elapsedSeconds = Benchmark::realtime do

          Fixtures.create_fixtures(File.dirname(__FILE__) +

          "/../fixtures/performance", "orders")

          assert_equal(HOW_MANY, Order.find_all.size)

          1.upto(HOW_MANY) do |id|

            order = Order.find(id)

            get :save_order, :order => order.attributes

            assert_redirected_to :action => 'index'

            assert_equal("Thank you for your order.", flash[:notice])

          end

        end

        assert elapsedSeconds < 3.0, "Actually took #{elapsedSeconds} seconds"

      end

    end

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

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

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

    class OrderTest < Test::Unit::TestCase

      fixtures :products

      self.use_transactional_fixtures  = true

      HOW_MANY = 100

      ……

    end

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

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

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

    5.       我们还可以使用benchmarker来比较两个方法所消耗的时间,例如:

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

    输出结果是:

                user     system      total        real

    #1      0.078000   0.000000   0.078000 (  0.078000)

    #2      0.000000   0.000000   0.000000 (  0.016000)

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

  • 相关阅读:
    86. Partition List
    2. Add Two Numbers
    55. Jump Game
    70. Climbing Stairs
    53. Maximum Subarray
    64. Minimum Path Sum
    122. Best Time to Buy and Sell Stock II
    以场景为中心的产品设计方法
    那些产品经理犯过最大的错
    Axure教程:如何使用动态面板?动态面板功能详解
  • 原文地址:https://www.cnblogs.com/dahuzizyd/p/ruby_rails_dotnet_study_29.html
Copyright © 2011-2022 走看看