diff --git a/spec/performance/shop_controller_spec.rb b/spec/performance/shop_controller_spec.rb index 6794c70da5..bfe49cd178 100644 --- a/spec/performance/shop_controller_spec.rb +++ b/spec/performance/shop_controller_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a309037ccb..155b617860 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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' diff --git a/spec/support/performance_helper.rb b/spec/support/performance_helper.rb new file mode 100644 index 0000000000..a2d4fe3630 --- /dev/null +++ b/spec/support/performance_helper.rb @@ -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