Class: RubyProf::GraphHtmlPrinter

Inherits:
AbstractPrinter show all
Includes:
ERB::Util
Defined in:
lib/ruby-prof/graph_html_printer.rb

Overview

Generates graph profile reports as html. To use the graph html printer:

result = RubyProf.profile do
  [code to profile]
end

printer = RubyProf::GraphHtmlPrinter.new(result)
printer.print(STDOUT, :min_percent=>0)

The constructor takes two arguments. The first is a RubyProf::Result object generated from a profiling run. The second is the minimum %total (the methods total time divided by the overall total time) that a method must take for it to be printed out in the report. Use this parameter to eliminate methods that are not important to the overall profiling results.

Constant Summary collapse

PERCENTAGE_WIDTH =
8
TIME_WIDTH =
10
CALL_WIDTH =
20

Instance Method Summary collapse

Methods inherited from AbstractPrinter

#method_name, #min_percent, #print_file, #setup_options

Constructor Details

#initialize(result) ⇒ GraphHtmlPrinter

Create a GraphPrinter. Result is a RubyProf::Result object generated from a profiling run.



32
33
34
35
36
# File 'lib/ruby-prof/graph_html_printer.rb', line 32

def initialize(result)
  super(result)
  @thread_times = Hash.new
  calculate_thread_times
end

Instance Method Details

#calculate_thread_timesObject

These methods should be private but then ERB doesn’t work. Turn off RDOC though –



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ruby-prof/graph_html_printer.rb', line 69

def calculate_thread_times
  # Cache thread times since this is an expensive
  # operation with the required sorting
  @overall_threads_time = 0.0
  @thread_times = Hash.new
  @result.threads.each do |thread_id, methods|
    roots = methods.select{|m| m.root?}
    thread_total_time = sum(roots.map{|r| self.total_time(r.call_infos)})
    @overall_threads_time += thread_total_time
    @thread_times[thread_id] = thread_total_time
  end
end

Creates a link to a method. Note that we do not create links to methods which are under the min_perecent specified by the user, since they will not be printed out.



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

def create_link(thread_id, method)
  if self.total_percent(thread_id, method) < min_percent
    # Just return name
    h method.full_name
  else
    href = '#' + method_href(thread_id, method)
    "<a href=\"#{href}\">#{h method.full_name}</a>"
  end
end


114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ruby-prof/graph_html_printer.rb', line 114

def file_link(path, linenum)
  srcfile = File.expand_path(path)
  if srcfile =~ /\/ruby_runtime$/
    ""
  else
    if RUBY_PLATFORM =~ /darwin/
      "<a href=\"txmt://open?url=file://#{h srcfile}&line=#{linenum}\" title=\"#{h srcfile}:#{linenum}\">#{linenum}</a>"
    else
      "<a href=\"file://#{h srcfile}?line=#{linenum}\" title=\"#{h srcfile}:#{linenum}\">#{linenum}</a>"
    end
  end
end

#method_href(thread_id, method) ⇒ Object



110
111
112
# File 'lib/ruby-prof/graph_html_printer.rb', line 110

def method_href(thread_id, method)
  h(method.full_name.gsub(/[><#\.\?=:]/,"_") + "_" + thread_id.to_s)
end

Print a graph html report to the provided output.

output - Any IO oject, including STDOUT or a file. The default value is STDOUT.

options - Hash of print options. See #setup_options

for more information.


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

def print(output = STDOUT, options = {})
  @output = output
  setup_options(options)

  filename = options[:filename]
  template = filename ? File.read(filename).untaint : (options[:template] || self.template)
  _erbout = @output
  erb = ERB.new(template, nil, nil)
  erb.filename = filename
  @output << erb.result(binding)
end

#self_percent(method) ⇒ Object



91
92
93
94
# File 'lib/ruby-prof/graph_html_printer.rb', line 91

def self_percent(method)
  overall_time = self.thread_time(method.thread_id)
  (method.self_time/overall_time) * 100
end

#sum(a) ⇒ Object



62
63
64
# File 'lib/ruby-prof/graph_html_printer.rb', line 62

def sum(a)
  a.inject(0.0){|s,t| s+=t}
end

#templateObject



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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/ruby-prof/graph_html_printer.rb', line 127

def template
'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <style media="all" type="text/css">
table {
  border-collapse: collapse;
  border: 1px solid #CCC;
  font-family: Verdana, Arial, Helvetica, sans-serif;
  font-size: 9pt;
  line-height: normal;
  width: 100%;
}

th {
  text-align: center;
  border-top: 1px solid #FB7A31;
  border-bottom: 1px solid #FB7A31;
  background: #FFC;
  padding: 0.3em;
  border-left: 1px solid silver;
}

tr.break td {
  border: 0;
  border-top: 1px solid #FB7A31;
  padding: 0;
  margin: 0;
}

tr.method td {
  font-weight: bold;
}

td {
  padding: 0.3em;
}

td:first-child {
  width: 190px;
  }

td {
  border-left: 1px solid #CCC;
  text-align: center;
}

.method_name {
  text-align: left;
}
  </style>
  </head>
  <body>
<h1>Profile Report</h1>
<!-- Threads Table -->
<table>
  <tr>
    <th>Thread ID</th>
    <th>Total Time</th>
  </tr>
  <% for thread_id in @result.threads.keys.sort %>
  <tr>
    <td><a href="#<%= thread_id %>"><%= thread_id %></a></td>
    <td><%= thread_time(thread_id) %></td>
  </tr>
  <% end %>
</table>

<!-- Methods Tables -->
<% for thread_id in @result.threads.keys.sort
     methods = @result.threads[thread_id]
     total_time = thread_time(thread_id) %>
  <h2><a name="<%= thread_id %>">Thread <%= thread_id %></a></h2>

  <table>
    <tr>
      <th><%= sprintf("%#{PERCENTAGE_WIDTH}s", "%Total") %></th>
      <th><%= sprintf("%#{PERCENTAGE_WIDTH}s", "%Self") %></th>
      <th><%= sprintf("%#{TIME_WIDTH}s", "Total") %></th>
      <th><%= sprintf("%#{TIME_WIDTH}s", "Self") %></th>
      <th><%= sprintf("%#{TIME_WIDTH}s", "Wait") %></th>
      <th><%= sprintf("%#{TIME_WIDTH+2}s", "Child") %></th>
      <th><%= sprintf("%#{CALL_WIDTH}s", "Calls") %></th>
      <th class="method_name">Name</th>
      <th>Line</th>
    </tr>

    <% min_time = @options[:min_time] || (@options[:nonzero] ? 0.005 : nil)
       methods.sort.reverse_each do |method|
        total_percentage = (method.total_time/total_time) * 100
        next if total_percentage < min_percent
        next if min_time && method.total_time < min_time
        self_percentage = (method.self_time/total_time) * 100 %>

        <!-- Parents -->
        <% for caller in method.aggregate_parents.sort_by(&:total_time)
             next unless caller.parent
             next if min_time && caller.total_time < min_time  %>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td><%= sprintf("%#{TIME_WIDTH}.2f", caller.total_time) %></td>
            <td><%= sprintf("%#{TIME_WIDTH}.2f", caller.self_time) %></td>
            <td><%= sprintf("%#{TIME_WIDTH}.2f", caller.wait_time) %></td>
            <td><%= sprintf("%#{TIME_WIDTH}.2f", caller.children_time) %></td>
            <% called = "#{caller.called}/#{method.called}" %>
            <td><%= sprintf("%#{CALL_WIDTH}s", called) %></td>
            <td class="method_name"><%= create_link(thread_id, caller.parent.target) %></td>
            <td><%= file_link(caller.parent.target.source_file, caller.line) %></td>
          </tr>
        <% end %>

        <tr class="method">
          <td><%= sprintf("%#{PERCENTAGE_WIDTH-1}.2f\%", total_percentage) %></td>
          <td><%= sprintf("%#{PERCENTAGE_WIDTH-1}.2f\%", self_percentage) %></td>
          <td><%= sprintf("%#{TIME_WIDTH}.2f", method.total_time) %></td>
          <td><%= sprintf("%#{TIME_WIDTH}.2f", method.self_time) %></td>
          <td><%= sprintf("%#{TIME_WIDTH}.2f", method.wait_time) %></td>
          <td><%= sprintf("%#{TIME_WIDTH}.2f", method.children_time) %></td>
          <td><%= sprintf("%#{CALL_WIDTH}i", method.called) %></td>
          <td class="method_name"><a name="<%= method_href(thread_id, method) %>"><%= h method.full_name %></a></td>
          <td><%= file_link(method.source_file, method.line) %></td>
        </tr>

        <!-- Children -->
        <% for callee in method.aggregate_children.sort_by(&:total_time).reverse %>
        <%   next if min_time && callee.total_time < min_time  %>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td><%= sprintf("%#{TIME_WIDTH}.2f", callee.total_time) %></td>
            <td><%= sprintf("%#{TIME_WIDTH}.2f", callee.self_time) %></td>
            <td><%= sprintf("%#{TIME_WIDTH}.2f", callee.wait_time) %></td>
            <td><%= sprintf("%#{TIME_WIDTH}.2f", callee.children_time) %></td>
            <% called = "#{callee.called}/#{callee.target.called}" %>
            <td><%= sprintf("%#{CALL_WIDTH}s", called) %></td>
            <td class="method_name"><%= create_link(thread_id, callee.target) %></td>
            <td><%= file_link(method.source_file, callee.line) %></td>
          </tr>
        <% end %>
        <!-- Create divider row -->
        <tr class="break"><td colspan="9"></td></tr>
    <% end %>
  </table>
<% end %>
  </body>
</html>'
end

#thread_time(thread_id) ⇒ Object



82
83
84
# File 'lib/ruby-prof/graph_html_printer.rb', line 82

def thread_time(thread_id)
  @thread_times[thread_id]
end

#total_percent(thread_id, method) ⇒ Object



86
87
88
89
# File 'lib/ruby-prof/graph_html_printer.rb', line 86

def total_percent(thread_id, method)
  overall_time = self.thread_time(thread_id)
  (method.total_time/overall_time) * 100
end

#total_time(call_infos) ⇒ Object



58
59
60
# File 'lib/ruby-prof/graph_html_printer.rb', line 58

def total_time(call_infos)
  sum(call_infos.map{|ci| ci.total_time})
end