Skip to content

Instantly share code, notes, and snippets.

@jorgemanrubia
Last active May 1, 2020 09:22
Show Gist options
  • Save jorgemanrubia/464a4a4f0a8be576902ab95a7606bd86 to your computer and use it in GitHub Desktop.
Save jorgemanrubia/464a4a4f0a8be576902ab95a7606bd86 to your computer and use it in GitHub Desktop.
Minitest helper to compare code with benchmark-ips
require 'benchmark/ips'
module PerformanceHelpers
BENCHMARK_DURATION = 1
BENCHMARK_WARMUP = 1
BASELINE_LABEL = "Baseline"
CODE_TO_TEST_LABEL = "Code"
# Usage:
#
# baseline = -> { <some baseline code> }
#
# assert_slower_by_at_most 2, baseline: baseline do
# <the code you want to compare against the baseline>
# end
def assert_slower_by_at_most(threshold_factor, baseline:, baseline_label: BASELINE_LABEL, code_to_test_label: CODE_TO_TEST_LABEL, &block_to_test)
result = Benchmark.ips do |x|
x.config(time: BENCHMARK_DURATION, warmup: BENCHMARK_WARMUP)
x.report(code_to_test_label, &block_to_test)
x.report(baseline_label, &baseline)
x.compare!
end
baseline_result = result.entries.find { |entry| entry.label == baseline_label }
code_to_test_result = result.entries.find { |entry| entry.label == code_to_test_label }
times_slower = baseline_result.ips / code_to_test_result.ips
assert times_slower >= 1, "Actually, '#{code_to_test_label}' was faster by a factor of #{1 / times_slower}"
assert times_slower < threshold_factor, "Expecting #{threshold_factor} times slower at most, but got #{times_slower} times slower"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment