Class: Easybench::Suite

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

Defined Under Namespace

Classes: DuplicateBenchmark

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iterations, &tests) ⇒ Suite

Returns a new instance of Suite.



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

def initialize(iterations, &tests)
  @iterations = iterations
  @tests      = tests
  @benchmarks = []      
end

Instance Attribute Details

#benchmarksObject (readonly)

Returns the value of attribute benchmarks.



46
47
48
# File 'lib/easybench.rb', line 46

def benchmarks
  @benchmarks
end

#testsObject (readonly)

Returns the value of attribute tests.



45
46
47
# File 'lib/easybench.rb', line 45

def tests
  @tests
end

Instance Method Details

#add_benchmark(name, options = {}, &block) ⇒ Object Also known as: report



54
55
56
57
58
59
60
# File 'lib/easybench.rb', line 54

def add_benchmark(name, options = {}, &block)
  if benchmark_defined?(name)
    raise DuplicateBenchmark, "A benchmark test called '#{name}' has already been defined"
  else
    @benchmarks << Easybench::Benchmark.new(name, options, &block)
  end
end

#run(runner = ::Benchmark) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/easybench.rb', line 64

def run(runner = ::Benchmark)
  @tests.call(self)

  runner.bm do |interface|
    @benchmarks.each do |benchmark|
      interface.report(benchmark.name(indent)) do
        (benchmark.iterations || @iterations).times(&benchmark.tests)
      end
    end
  end
end