Class: Benchable::Benchmark

Inherits:
Object
  • Object
show all
Defined in:
lib/benchable/benchmark.rb

Overview

Main class to perform benchmarks.

Use the method Benchable.bench to declare a benchmark.

Constant Summary collapse

DEFAULT_WIDTH =
20
BENCHMARK_TYPES =
%i[bm bmbm ips memory].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(benchmark_type, **options) ⇒ Benchmark

Returns a new instance of Benchmark.

Raises:



15
16
17
18
19
20
# 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

Class Method Details

.bench(name, &block) ⇒ Object



26
27
28
# File 'lib/benchable/benchmark.rb', line 26

def self.bench(name, &block)
  define_method(method_name_for(name), &block)
end

.setup(&block) ⇒ Object



22
23
24
# File 'lib/benchable/benchmark.rb', line 22

def self.setup(&block)
  define_method(:setup, &block)
end

Instance Method Details

#casesObject



38
39
40
# File 'lib/benchable/benchmark.rb', line 38

def cases
  public_methods.grep(/\Abench_/)
end

#runObject



33
34
35
36
# File 'lib/benchable/benchmark.rb', line 33

def run
  setup
  run_benchmark
end

#setupObject



30
31
# File 'lib/benchable/benchmark.rb', line 30

def setup
end