Module: MicroBench

Defined in:
lib/micro_bench.rb,
lib/micro_bench/version.rb

Defined Under Namespace

Classes: Benchmark, Configurations

Constant Summary collapse

VERSION =
"0.3.0"

Class Method Summary collapse

Class Method Details

.configure(&block) ⇒ Object

Configure MicroBench.

Example usage:

MicroBench.configure do |config|
  config.formatter = proc { |duration| duration.ceil }
end


11
12
13
14
# File 'lib/micro_bench.rb', line 11

def configure(&block)
  block.call(configurations)
  nil
end

.duration(bench_id = nil) ⇒ Object

Give duration of the benchmark

Parameters:

bench_id

Identifier of the benchmark

Returns:

Formatted duration of the given benchmark, or nil if benchmark is unknown.

Example usage:

MicroBench.duration(:my_benchmark)


67
68
69
70
71
72
# File 'lib/micro_bench.rb', line 67

def duration(bench_id = nil)
  raw = raw_duration(bench_id)
  return nil if raw.nil?
  
  configurations.formatter.call(raw)
end

.raw_duration(bench_id = nil) ⇒ Object

Give raw duration of the benchmark

Parameters:

bench_id

Identifier of the benchmark

Returns:

Duration of the given benchmark, or nil if benchmark is unknown.

Example usage:

MicroBench.raw_duration(:my_benchmark)


86
87
88
# File 'lib/micro_bench.rb', line 86

def raw_duration(bench_id = nil)
  benchmarks[benchmark_key(bench_id)]&.duration
end

.start(bench_id = nil) ⇒ Object

Start a benchmark

Parameters:

bench_id

Identifier of the benchmark

Returns:

A boolean representing success

Example usage:

MicroBench.start(:my_benchmark)

Calling the method multiple times with the same bench_id will restart the benchmark for the given bench_id.



31
32
33
34
# File 'lib/micro_bench.rb', line 31

def start(bench_id = nil)
  benchmarks[benchmark_key(bench_id)] = MicroBench::Benchmark.new
  return true
end

.stop(bench_id = nil) ⇒ Object

Stop a benchmark

Parameters:

bench_id

Identifier of the benchmark

Returns:

A boolean representing success

Example usage:

MicroBench.stop(:my_benchmark)


48
49
50
51
52
53
# File 'lib/micro_bench.rb', line 48

def stop(bench_id = nil)
  key = benchmark_key(bench_id)
  return false unless benchmarks.key?(key)

  benchmarks[key].stop
end