File: printers_test.rb

package info (click to toggle)
ruby-prof 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,092 kB
  • sloc: ruby: 10,664; ansic: 2,946; makefile: 7
file content (178 lines) | stat: -rw-r--r-- 5,496 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env ruby
# encoding: UTF-8

require File.expand_path('../test_helper', __FILE__)
require 'fileutils'
require 'stringio'
require 'tmpdir'
require_relative 'prime'

# --  Tests ----
class PrintersTest < TestCase
  def setup
    super
    # WALL_TIME, PROCESS_TIME, ALLOCATIONS and MEMORY as types of measuremen
    measure_modes = {wall_time: RubyProf::WALL_TIME, process_time: RubyProf::PROCESS_TIME, allocations: RubyProf::ALLOCATIONS, memory: RubyProf::MEMORY}

    @results = {}

    measure_modes.each do |key, value|
      @results[key] = RubyProf::Profile.profile(measure_mode: value) do
        run_primes(1000, 5000)
      end
    end
  end

  def test_printers
    output = ENV['SHOW_RUBY_PROF_PRINTER_OUTPUT'] == "1" ? STDOUT : StringIO.new('')

    printer = RubyProf::CallStackPrinter.new(@results[:wall_time])
    printer.print(output)

    printer = RubyProf::CallTreePrinter.new(@results[:wall_time])
    printer.print(:path => Dir.tmpdir)

    printer = RubyProf::FlatPrinter.new(@results[:wall_time])
    printer.print(output)

    printer = RubyProf::GraphHtmlPrinter.new(@results[:wall_time])
    printer.print(output)

    printer = RubyProf::GraphPrinter.new(@results[:wall_time])
    printer.print(output)
  end

  def test_print_to_files
    printer = RubyProf::DotPrinter.new(@results[:wall_time])
    File.open("#{Dir.tmpdir}/graph.dot", "w") {|f| printer.print(f)}

    printer = RubyProf::CallStackPrinter.new(@results[:wall_time])
    File.open("#{Dir.tmpdir}/stack.html", "w") {|f| printer.print(f, :application => "primes")}

    printer = RubyProf::MultiPrinter.new(@results[:wall_time])
    printer.print(:path => Dir.tmpdir, :profile => "multi", :application => "primes")

    ['graph.dot', 'multi.flat.txt', 'multi.graph.html', "multi.callgrind.out.#{$$}", 'multi.stack.html', 'stack.html'].each do |file_name|
      file_path = File.join(Dir.tmpdir, file_name)
      refute(File.empty?(file_path))
    end
  end

  def test_refuses_io_objects
    p = RubyProf::MultiPrinter.new(@results[:wall_time])
    begin
      p.print(STDOUT)
      flunk "should have raised an ArgumentError"
    rescue ArgumentError => e
      assert_match(/IO/, e.to_s)
    end
  end

  def test_refuses_non_hashes
    p = RubyProf::MultiPrinter.new (@results[:wall_time])
    begin
      p.print([])
      flunk "should have raised an ArgumentError"
    rescue ArgumentError => e
      assert_match(/hash/, e.to_s)
    end
  end

  def test_flat_string
    output = helper_test_flat_string(RubyProf::FlatPrinter)
    assert_match(/prime.rb/, output)
  end

  def helper_test_flat_string(klass)
    output = ''

    printer = klass.new(@results[:wall_time])
    printer.print(output)

    assert_match(/Thread ID: -?\d+/i, output)
    assert_match(/Fiber ID: -?\d+/i, output)
    assert_match(/Total: \d+\.\d+/i, output)
    assert_match(/Object#run_primes/i, output)
    output
  end

  def test_graph_html_string
    output = ''
    printer = RubyProf::GraphHtmlPrinter.new(@results[:wall_time])
    printer.print(output)

    assert_match(/<!DOCTYPE html>/i, output)
    assert_match( %r{<th>Total</th>}i, output)
    assert_match(/Object#run_primes/i, output)
  end

  def test_graph_string
    output = ''
    printer = RubyProf::GraphPrinter.new(@results[:wall_time])
    printer.print(output)

    assert_match(/Thread ID: -?\d+/i, output)
    assert_match(/Fiber ID: -?\d+/i, output)
    assert_match(/Total Time: \d+\.\d+/i, output)
    assert_match(/Object#run_primes/i, output)
  end

  def do_nothing
    start = Time.now
    while(Time.now == start)
    end
  end

  def test_all_with_small_percentiles
    result = RubyProf::Profile.profile do
      sleep 2
      do_nothing
    end

    # RubyProf::CallTreePrinter doesn't "do" a min_percent
    # RubyProf::FlatPrinter only outputs if self time > percent...
    for klass in [RubyProf::GraphPrinter, RubyProf::GraphHtmlPrinter]
      printer = klass.new(result)
      out = ''
      printer.print(out, :min_percent => 0.00000001)
      assert_match(/do_nothing/, out)
    end
  end

  def test_print_footer
    results_keys = [:wall_time, :process_time, :allocations, :memory]
    expected_matches = [
      "The percentage of time spent by this method relative to the total time in the entire program.",
      "The total time spent by this method and its children.",
      "The time spent by this method.",
      "The time spent by this method's children.",
      "The percentage of allocations made by this method relative to the total allocations in the entire program.",
      "The total number of allocations made by this method and its children.",
      "The number of allocations made by this method.",
      "The number of allocations made by this method's children.",
      "The percentage of memory used by this method relative to the total memory in the entire program.",
      "The total memory used by this method and its children.",
      "The memory used by this method.",
      "The memory used by this method's children."
    ]

    results_keys.each do |key|
      output = ''
      printer = RubyProf::GraphPrinter.new(@results[key])
      printer.print(output)

      case key
      when :wall_time, :process_time
        matches = expected_matches[0..3]
      when :allocations
        matches = expected_matches[4..7]
      when :memory
        matches = expected_matches[8..11]
      end

      matches.each do |pattern|
        assert_match(pattern, output)
      end
    end
  end
end