2011-12-13 59 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