class Benchable::Benchmark
Main class to perform benchmarks.
Use the method Benchable.bench
to declare a benchmark.
Constants
- BENCHMARK_TYPES
- DEFAULT_WIDTH
Attributes
benchmark_type[R]
options[R]
Public Class Methods
bench(name, &block)
click to toggle source
# File lib/benchable/benchmark.rb, line 26 def self.bench(name, &block) define_method(method_name_for(name), &block) end
new(benchmark_type, **options)
click to toggle source
# File lib/benchable/benchmark.rb, line 15 def initialize(benchmark_type, **options) @benchmark_type = benchmark_type @options = options raise Error, "Invalid benchmark type '#{benchmark_type}'" unless valid_benchmark_type? end
setup(&block)
click to toggle source
# File lib/benchable/benchmark.rb, line 22 def self.setup(&block) define_method(:setup, &block) end
Private Class Methods
method_name_for(name)
click to toggle source
# File lib/benchable/benchmark.rb, line 42 def self.method_name_for(name) "bench_#{name.to_s.tr(" ", "_").downcase}" end
Public Instance Methods
cases()
click to toggle source
# File lib/benchable/benchmark.rb, line 38 def cases public_methods.grep(/\Abench_/) end
run()
click to toggle source
# File lib/benchable/benchmark.rb, line 33 def run setup run_benchmark end
setup()
click to toggle source
# File lib/benchable/benchmark.rb, line 30 def setup end
Private Instance Methods
benchmark(&block)
click to toggle source
# File lib/benchable/benchmark.rb, line 72 def benchmark(&block) if benchmark_type == :memory ::Benchmark.public_send(benchmark_type, **benchmark_args, &block) else ::Benchmark.public_send(benchmark_type, *benchmark_args, &block) end end
benchmark_args()
click to toggle source
# File lib/benchable/benchmark.rb, line 80 def benchmark_args if benchmark_type == :memory options.slice(:quiet) else options[:width] || DEFAULT_WIDTH end end
name_for(benchmark_case)
click to toggle source
# File lib/benchable/benchmark.rb, line 68 def name_for(benchmark_case) benchmark_case.to_s.gsub("bench_", "").tr("_", " ").capitalize end
run_benchmark()
click to toggle source
# File lib/benchable/benchmark.rb, line 54 def run_benchmark benchmark do |b| b.config(options) if benchmark_type == :ips cases.each do |benchmark_case| b.report(name_for(benchmark_case)) do method(benchmark_case).call end end b.compare! if b.respond_to? :compare! end end
valid_benchmark_type?()
click to toggle source
# File lib/benchable/benchmark.rb, line 50 def valid_benchmark_type? BENCHMARK_TYPES.include? benchmark_type end