Class: RubyProf::CallStackPrinter

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

Overview

Prints a HTML visualization of the call tree.

To use the printer:

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

printer = RubyProf::CallStackPrinter.new(result)
printer.print(STDOUT)

Instance Method Summary collapse

Methods inherited from AbstractPrinter

#initialize, #method_location, #min_percent, needs_dir?, #open_asset, #print_column_headers, #print_footer, #print_header, #print_thread, #print_threads, #sort_method, #time_format

Constructor Details

This class inherits a constructor from RubyProf::AbstractPrinter

Instance Method Details

#applicationObject



151
152
153
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 151

def application
  @options[:application] || $PROGRAM_NAME
end

#argumentsObject



155
156
157
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 155

def arguments
  ARGV.join(' ')
end

#base64_imageObject



171
172
173
174
175
176
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 171

def base64_image
  @data ||= begin
    file = open_asset('call_stack_printer.png')
    Base64.encode64(file).gsub(/\n/, '')
  end
end

#color(p) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 138

def color(p)
  case i = p.to_i
  when 0..5
    "01"
  when 5..10
    "05"
  when 100
    "9"
  else
    "#{i/10}"
  end
end

#dump(ci) ⇒ Object



134
135
136
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 134

def dump(ci)
  $stderr.printf "%s/%d t:%f s:%f w:%f  \n", ci, ci.object_id, ci.total_time, ci.self_time, ci.wait_time
end

#expansionObject



167
168
169
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 167

def expansion
  @options[:expansion] || 10.0
end


115
116
117
118
119
120
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 115

def graph_link(call_info)
  total_calls = call_info.target.called
  href = "#{method_href(call_info.target)}"
  totals = total_calls.to_s
  "[#{call_info.called} calls, #{totals} total]"
end


105
106
107
108
109
110
111
112
113
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 105

def link(method, recursive)
  method_name = "#{recursive ? '*' : ''}#{method.full_name}"
  if method.source_file.nil?
    h method_name
  else
    file = File.expand_path(method.source_file)
   "<a href=\"file://#{file}##{method.line}\">#{h method_name}</a>"
  end
end

#method_href(method) ⇒ Object



122
123
124
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 122

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

#name(call_info) ⇒ Object



100
101
102
103
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 100

def name(call_info)
  method = call_info.target
  method.full_name
end

Specify print options.

options - Hash table

:min_percent - Number 0 to 100 that specifes the minimum
               %self (the methods self time divided by the
               overall total time) that a method must take
               for it to be printed out in the report.
               Default value is 0.

:threshold   - a float from 0 to 100 that sets the threshold of
               results displayed.
               Default value is 1.0

:title       - a String to overide the default "ruby-prof call tree"
               title of the report.

:expansion   - a float from 0 to 100 that sets the threshold of
               results that are expanded, if the percent_total
               exceeds it.
               Default value is 10.0

:application - a String to overide the name of the application,
               as it appears on the report.


46
47
48
49
50
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 46

def print(output = STDOUT, options = {})
  setup_options(options)
  output << @erb.result(binding)
  a = 1
end


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
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 58

def print_stack(output, visited, call_info, parent_time)
  total_time = call_info.total_time
  percent_parent = (total_time/parent_time)*100
  percent_total = (total_time/@overall_time)*100
  return unless percent_total > min_percent
  color = self.color(percent_total)
  kids = call_info.target.callees
  visible = percent_total >= threshold
  expanded = percent_total >= expansion
  display = visible ? "block" : "none"

  output << "<li class=\"color#{color}\" style=\"display:#{display}\">" << "\n"

  if visited.include?(call_info)
    output << "<a href=\"#\" class=\"toggle empty\" ></a>" << "\n"
    output << "<span>%s %s</span>" % [link(call_info.target, true), graph_link(call_info)] << "\n"
  else
    visited << call_info

    if kids.empty?
      output << "<a href=\"#\" class=\"toggle empty\" ></a>" << "\n"
    else
      visible_children = kids.any?{|ci| (ci.total_time/@overall_time)*100 >= threshold}
      image = visible_children ? (expanded ? "minus" : "plus") : "empty"
      output << "<a href=\"#\" class=\"toggle #{image}\" ></a>" << "\n"
    end
    output << "<span>%4.2f%% (%4.2f%%) %s %s</span>" % [percent_total, percent_parent,
                                                        link(call_info.target, false), graph_link(call_info)] << "\n"

    unless kids.empty?
      output <<  (expanded ? '<ul>' : '<ul style="display:none">')  << "\n"
      kids.sort_by{|c| -c.total_time}.each do |child_call_info|
        print_stack(output, visited, child_call_info, total_time)
      end
      output << '</ul>' << "\n"
    end

    visited.delete(call_info)
  end
  output << '</li>' << "\n"
end

#setup_options(options) ⇒ Object

:enddoc:



53
54
55
56
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 53

def setup_options(options)
  super(options)
  @erb = ERB.new(self.template)
end

#sum(a) ⇒ Object



130
131
132
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 130

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

#templateObject



178
179
180
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 178

def template
  open_asset('call_stack_printer.html.erb')
end

#thresholdObject



163
164
165
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 163

def threshold
  @options[:threshold] || 1.0
end

#titleObject



159
160
161
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 159

def title
  @title ||= @options.delete(:title) || "ruby-prof call tree"
end

#total_time(call_infos) ⇒ Object



126
127
128
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 126

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