Module: RubyProf::Test

Defined in:
lib/ruby-prof/profile_test.rb

Constant Summary collapse

PROFILE_OPTIONS =
{
:measure_modes => [RubyProf::PROCESS_TIME],
:count => 10,
:printers => [RubyProf::FlatPrinter, RubyProf::GraphHtmlPrinter],
:min_percent => 0.05,
:output_dir => Dir.pwd }

Instance Method Summary collapse

Instance Method Details

#format_profile_total(total, measure_mode) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ruby-prof/profile_test.rb', line 97

def format_profile_total(total, measure_mode)
  case measure_mode
    when RubyProf::PROCESS_TIME, RubyProf::WALL_TIME
      "%.2f seconds" % total
    when RubyProf::MEMORY
      "%.2f kilobytes" % total
    when RubyProf::ALLOCATIONS
      "%d allocations" % total
    else
      "%.2f #{measure_mode}"
  end
end

#measure_mode_name(measure_mode) ⇒ Object



137
138
139
140
141
142
143
144
145
# File 'lib/ruby-prof/profile_test.rb', line 137

def measure_mode_name(measure_mode)
  case measure_mode
    when RubyProf::PROCESS_TIME; 'process_time'
    when RubyProf::WALL_TIME; 'wall_time'
    when RubyProf::MEMORY; 'memory'
    when RubyProf::ALLOCATIONS; 'allocations'
    else "measure#{measure_mode}"
  end
end

#report_filename(printer, measure_mode) ⇒ Object

The report filename is test_name + measure_mode + report_type



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ruby-prof/profile_test.rb', line 124

def report_filename(printer, measure_mode)
  suffix =
    case printer
      when RubyProf::FlatPrinter; 'flat.txt'
      when RubyProf::GraphPrinter; 'graph.txt'
      when RubyProf::GraphHtmlPrinter; 'graph.html'
      when RubyProf::CallTreePrinter; 'tree.txt'
      else printer.to_s.downcase
    end

  "#{PROFILE_OPTIONS[:output_dir]}/#{method_name}_#{measure_mode_name(measure_mode)}_#{suffix}"
end

#report_profile(data, measure_mode) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ruby-prof/profile_test.rb', line 110

def report_profile(data, measure_mode)
  PROFILE_OPTIONS[:printers].each do |printer_klass|
    printer = printer_klass.new(data)

    # Open the file
    file_name = report_filename(printer, measure_mode)

    File.open(file_name, 'wb') do |file|
      printer.print(file, PROFILE_OPTIONS)
    end
  end
end

#run(result) {|self.class::STARTED, name| ... } ⇒ Object

Yields:

  • (self.class::STARTED, name)


13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby-prof/profile_test.rb', line 13

def run(result)
  return if @method_name.to_s == "default_test"

  yield(self.class::STARTED, name)
  @_result = result
  run_warmup
  PROFILE_OPTIONS[:measure_modes].each do |measure_mode|
    data = run_profile(measure_mode)
    report_profile(data, measure_mode)
    result.add_run
  end
  yield(self.class::FINISHED, name)
end

#run_profile(measure_mode) ⇒ Object



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
95
# File 'lib/ruby-prof/profile_test.rb', line 65

def run_profile(measure_mode)
  RubyProf.measure_mode = measure_mode

  print '  '
  PROFILE_OPTIONS[:count].times do |i|
    run_test do
      begin
        print '.'
        $stdout.flush
        GC.disable

        RubyProf.resume do
          __send__(@method_name)
        end
      ensure
        GC.enable
      end
    end
  end

  data = RubyProf.stop
  bench = data.threads.values.inject(0) do |total, method_infos|
    top = method_infos.sort.last
    total += top.total_time
    total
  end

  puts "\n  #{measure_mode_name(measure_mode)}: #{format_profile_total(bench, measure_mode)}\n"

  data
end

#run_setupObject



57
58
59
# File 'lib/ruby-prof/profile_test.rb', line 57

def run_setup
  setup
end

#run_teardownObject



61
62
63
# File 'lib/ruby-prof/profile_test.rb', line 61

def run_teardown
  teardown
end

#run_testObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby-prof/profile_test.rb', line 27

def run_test
  begin
    run_setup
    yield
  rescue ::Test::Unit::AssertionFailedError => e
    add_failure(e.message, e.backtrace)
  rescue StandardError, ScriptError
    add_error($!)
  ensure
    begin
      run_teardown
    rescue ::Test::Unit::AssertionFailedError => e
      add_failure(e.message, e.backtrace)
    rescue StandardError, ScriptError
      add_error($!)
    end
  end
end

#run_warmupObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/ruby-prof/profile_test.rb', line 46

def run_warmup
  print "\n#{self.class.name}##{method_name}"

  run_test do
    bench = Benchmark.realtime do
      __send__(@method_name)
    end
    puts " (%.2fs warmup)" % bench
  end
end