mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-01 06:41:41 +00:00
Extract multi-sample benchmarking into a helper method
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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'
|
||||
|
||||
20
spec/support/performance_helper.rb
Normal file
20
spec/support/performance_helper.rb
Normal 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
|
||||
Reference in New Issue
Block a user