2011-12-13 31 views
8

我们在rails项目中使用RSpec进行单元测试。我想在RSpec中设置一些性能测试,但要以不破坏“常规”功能和固件的方式进行。如何设置RSpec进行性能测试'侧面'

理想情况下,我可以用某种方式标记我的性能规格,以便它们不会默认运行。 然后,当我指定明确运行这些规格时,它将加载一组不同的灯具(使用更大,更像'制作'的数据集进行性能测试是有意义的)。

这可能吗?它似乎应该是。

有没有人设置过这样的东西?你是怎么做的?

回答

20

我设法得到什么,我通过以下寻找:

# Exclude :performance tagged specs by default 
config.filter_run_excluding :performance => true 

# When we're running a performance test load the test fixures: 
config.before(:all, :performance => true) do 
    # load performance fixtures 
    require 'active_record/fixtures' 
    ActiveRecord::Fixtures.reset_cache 
    ActiveRecord::Fixtures.create_fixtures('spec/perf_fixtures', File.basename("products.yml", '.*')) 
    ActiveRecord::Fixtures.create_fixtures('spec/perf_fixtures', File.basename("ingredients.yml", '.*')) 
end 

# define an rspec helper for takes_less_than 
require 'benchmark' 
RSpec::Matchers.define :take_less_than do |n| 
    chain :seconds do; end 
    match do |block| 
    @elapsed = Benchmark.realtime do 
     block.call 
    end 
    @elapsed <= n 
    end 
end 

# example of a performance test 
describe Api::ProductsController, "API Products controller", :performance do 
    it "should fetch all the products reasonably quickly" do 
    expect do 
     get :index, :format => :json 
    end.to take_less_than(60).seconds 
    end 
end 

但我倾向于Marnen的观点,这不是真正的性能测试最好的主意同意。

0

如果你想进行性能测试,为什么不运行New Relic或者生产数据快照?我想,你并不需要不同的规格。

+0

主要是我想知道自动执行性能测试的可行性。因此,引入了显着的负面性能影响的代码更改会“跳闸”测试,而不是断言某个功能可以在少于30秒内运行。 –

+0

你可以做到这一点,但我不确定它的实际用处是多么的有用,特别是因为RSpec最好不测试面向用户的东西,而且性能是面向用户的。我想你可以使用Cucumber而不是RSpec来进行性能测试,但我倾向于认为这最好留给New Relic。 –

+1

这就是所谓的分析,而不是性能测试。性能测试应确保在新代码投入生产之前,一段代码在给定的上下文内运行所需的时间。 – aledalgrande