Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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