Class: Bench::Implementation

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

Direct Known Subclasses

BinaryImplementation, RbenvImplementation

Constant Summary collapse

BEFORE_WARMUP_TIME =
30
WARMUP_WINDOW_SIZE =
20
WARMED_UP_RELATIVE_RANGE =
0.1
MAX_WARMUP =
100
MAX_WARMUP_TIME =
4 * 60
SAMPLES_COUNT =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Implementation

Returns a new instance of Implementation.



22
23
24
# File 'lib/bench9000/implementation.rb', line 22

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/bench9000/implementation.rb', line 20

def name
  @name
end

Instance Method Details

#measure(flags, benchmark) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/bench9000/implementation.rb', line 26

def measure(flags, benchmark)
  command = "bash -c \"#{command(benchmark)}\""

  puts command if flags.has_key? "--show-commands"

  warming_up = true
  warmup_window = []
  warmup_samples = []
  samples = []

  overall_time = Time.now

  IO.popen command, "r+" do |subprocess|
    while true
      line = subprocess.gets

      if line.nil? || line == "error"
        return :failed
      end

      unless line.match(/\d+\.\d+/)
        STDERR.puts line
        next
      end

      time = line.to_f
      puts time if flags.has_key? "--show-samples"

      elapsed_time = Time.now - overall_time

      if elapsed_time < BEFORE_WARMUP_TIME
        warmup_samples.push time

        subprocess.puts "continue"
      elsif warming_up
        warmup_samples.push time

        warmup_window.shift if warmup_window.size == WARMUP_WINDOW_SIZE
        warmup_window.push time
        window_relative_range = Stats.range(warmup_window) / Stats.mean(warmup_window)

        if warmup_window.size == WARMUP_WINDOW_SIZE && window_relative_range < WARMED_UP_RELATIVE_RANGE
          warming_up = false
          warmup_samples = warmup_samples.reverse.drop(WARMUP_WINDOW_SIZE).reverse
          samples = warmup_window
        elsif warmup_samples.size > MAX_WARMUP || elapsed_time > MAX_WARMUP_TIME
          puts "warning: #{@name} #{benchmark} never warmed up!"
          warming_up = false
        end

        subprocess.puts "continue"
      else
        samples.push time

        if samples.size < SAMPLES_COUNT
          subprocess.puts "continue"
        else
          subprocess.puts "stop"
          break
        end
      end
    end
  end

  raise "not enough warmup samples" if warmup_samples.nil?
  raise "not enough samples" if samples.nil? || samples.size < SAMPLES_COUNT

  Measurement.new(warmup_samples, samples)
end

#to_sObject



96
97
98
# File 'lib/bench9000/implementation.rb', line 96

def to_s
  @name
end