Class: Benchmark::Bencher

Inherits:
Object
  • Object
show all
Defined in:
lib/better-benchmark/bencher.rb

Constant Summary collapse

DATA_FILE =
'bbench-run-time'

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Bencher

Returns a new instance of Bencher.

Parameters:

  • argv (Array)

    The command line arguments passed to the bencher script



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
# File 'lib/better-benchmark/bencher.rb', line 11

def initialize( argv )
  @iterations = 10
  @executable = 'ruby'

  while argv.any?
    arg = argv.shift
    case arg
    when '-d'
      @data_dir = argv.shift
      begin
        if ! File.stat( @data_dir ).directory?
          $stderr.puts "#{@data_dir} is not a directory."
          exit 3
        end
      rescue Errno::ENOENT
        $stderr.puts "#{@data_dir} does not exist."
        exit 4
      end
    when '-e'
      @executable = argv.shift
    when '-i'
      @iterations = argv.shift.to_i
    when '-p'
      @max_p = argv.shift
    when '-r'
      if @r1.nil?
        @r1 = argv.shift
      else
        @r2 = argv.shift
      end
    when '-w'
      @test_working_copy = true
    when '--'
      @executable_args = argv.dup
      argv.clear
    end
  end

  if ( ! @test_working_copy && ( @r1.nil? || @r2.nil? ) ) || @executable_args.nil?
    print_usage
    exit 2
  end
end

Instance Method Details

#one_runObject



55
56
57
# File 'lib/better-benchmark/bencher.rb', line 55

def one_run
  system "#{@executable} #{ @executable_args.join(' ') }"  or exit $?.to_i
end


5
6
7
# File 'lib/better-benchmark/bencher.rb', line 5

def print_usage
  puts "#{$0} [-i <iterations>] [-w] [-r <revision 1> -r <revision 2>] [-p <max p-value>] [-d <data tmp dir>] [-e <executable/interpreter>] -- <executable's args...>"
end

#runObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/better-benchmark/bencher.rb', line 70

def run
  times1 = []
  times2 = []

  @iterations.times do
    if @test_working_copy
      system "git stash -q"  or exit $?.to_i
    else
      system "git checkout -q #{@r1}"  or exit $?.to_i
    end
    times1 << time_one_run

    if @test_working_copy
      system "git stash pop -q"  or exit $?.to_i
    else
      system "git checkout -q #{@r2}"  or exit $?.to_i
    end
    times2 << time_one_run
  end

  ::Benchmark.report_on(
    ::Benchmark.compare_times( times1, times2, @max_p )
  )
end

#time_one_runObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/better-benchmark/bencher.rb', line 59

def time_one_run
  if @data_dir
    one_run
    File.read( "#{@data_dir}/#{DATA_FILE}" ).to_f
  else
    t0 = Time.now
    one_run
    Time.now.to_f - t0.to_f
  end
end