Class: BenchmarkInterface::BenchmarkSet

Inherits:
Object
  • Object
show all
Defined in:
lib/benchmark-interface/benchmark-set.rb

Constant Summary collapse

@@current =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBenchmarkSet

Returns a new instance of BenchmarkSet.



14
15
16
17
18
19
# File 'lib/benchmark-interface/benchmark-set.rb', line 14

def initialize
  @benchmarks = []
  @counter = 0
  @@current = self
  @iterations = 1
end

Instance Attribute Details

#iterationsObject (readonly)

Returns the value of attribute iterations.



12
13
14
# File 'lib/benchmark-interface/benchmark-set.rb', line 12

def iterations
  @iterations
end

Class Method Details

.currentObject



80
81
82
# File 'lib/benchmark-interface/benchmark-set.rb', line 80

def self.current
  @@current
end

Instance Method Details

#benchmark(name) ⇒ Object



74
75
76
# File 'lib/benchmark-interface/benchmark-set.rb', line 74

def benchmark(name)
  benchmarks([name]).first
end

#benchmarks(names = nil) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/benchmark-interface/benchmark-set.rb', line 66

def benchmarks(names=nil)
  if names
    @benchmarks.select { |b| names.include?(b.name) }
  else
    @benchmarks
  end
end

#implicit_nameObject



38
39
40
41
42
# File 'lib/benchmark-interface/benchmark-set.rb', line 38

def implicit_name
  file = File.basename(@path, '.rb')
  @counter += 1
  "#{file}:#{@counter}"
end

#load_benchmarks(path) ⇒ Object



21
22
23
24
25
# File 'lib/benchmark-interface/benchmark-set.rb', line 21

def load_benchmarks(path)
  @path = path
  @@current = self
  load(path)
end

#load_mri_benchmarks(path, options) ⇒ Object



27
28
29
30
31
# File 'lib/benchmark-interface/benchmark-set.rb', line 27

def load_mri_benchmarks(path, options)
  @path = path
  @@current = self
  Frontends::MRI.load_mri path, options
end

#prepareObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/benchmark-interface/benchmark-set.rb', line 44

def prepare
  # Don't give benchmarks line numbers if there's only one

  if @benchmarks.size == 1
    @benchmarks.first.remove_line_numbers
  end

  # Give benchmarks iterations if needed

  if @benchmarks.any?(&:needs_iterating?)
    iterations = @benchmarks.map(&:iterations_for_one_second).min

    puts "This benchmark set contains blocks that want a number of iterations - running all iterations #{iterations} times"

    @benchmarks.each do |b|
      b.iterate iterations
    end

    @iterations = iterations
  end
end

#register(name, code) ⇒ Object



33
34
35
36
# File 'lib/benchmark-interface/benchmark-set.rb', line 33

def register(name, code)
  name = implicit_name unless name
  @benchmarks.push Benchmark.new(name, code)
end