Extract multi-sample benchmarking into a helper method

This commit is contained in:
Rohan Mitchell
2015-11-05 12:01:52 +11:00
parent e20d8d3e95
commit c932d20ef5
3 changed files with 24 additions and 13 deletions

View File

@@ -28,20 +28,10 @@ describe ShopController, type: :controller, performance: true do
end
it "returns products via json" do
results = []
4.times do |i|
ActiveRecord::Base.connection.query_cache.clear
Rails.cache.clear
result = Benchmark.measure do
xhr :get, :products
response.should be_success
end
results << result.total if i > 0
puts result
results = multi_benchmark(3) do
xhr :get, :products
response.should be_success
end
puts (results.sum / results.count * 1000).round 0
end
end
end

View File

@@ -109,6 +109,7 @@ RSpec.configure do |config|
config.include OpenFoodNetwork::HtmlHelper
config.include ActionView::Helpers::DateHelper
config.include OpenFoodNetwork::DelayedJobHelper
config.include OpenFoodNetwork::PerformanceHelper
# FactoryGirl
require 'factory_girl_rails'

View File

@@ -0,0 +1,20 @@
module OpenFoodNetwork
module PerformanceHelper
def multi_benchmark(num_samples)
results = (0..num_samples).map do |i|
ActiveRecord::Base.connection.query_cache.clear
Rails.cache.clear
result = Benchmark.measure { yield }
puts result
result.total
end.drop(1) # Do not return the first sample
puts (results.sum / results.count * 1000).round 0
results
end
end
end