From c932d20ef5f2b4f6200f3ab063e1b687206e724f Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Thu, 5 Nov 2015 12:01:52 +1100 Subject: [PATCH] Extract multi-sample benchmarking into a helper method --- spec/performance/shop_controller_spec.rb | 16 +++------------- spec/spec_helper.rb | 1 + spec/support/performance_helper.rb | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 spec/support/performance_helper.rb 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