Files
openfoodnetwork/spec/support/performance_helper.rb
2017-06-09 10:32:57 +10:00

32 lines
864 B
Ruby

module OpenFoodNetwork
module PerformanceHelper
def multi_benchmark(num_samples, cache_key_patterns: [])
results = (0..num_samples).map do |i|
ActiveRecord::Base.connection.query_cache.clear
delete_cache_keys(cache_key_patterns)
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
# Looks for matching keys and deletes them
# Blindly running `Rails.cache.clear` is harmful since it alters application
# state outside executing spec example
#
# @param cache_key_patterns [Array<String>]
def delete_cache_keys(cache_key_patterns)
cache_key_patterns.each do |pattern|
Rails.cache.delete_matched(pattern)
end
end
end
end