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 Attribute Summary collapse

Attributes inherited from AbstractPrinter

#filter_by, #max_percent, #min_percent, #sort_method

Instance Method Summary collapse

Methods inherited from AbstractPrinter

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

Constructor Details

This class inherits a constructor from RubyProf::AbstractPrinter

Instance Attribute Details

#applicationObject (readonly)

Returns the value of attribute application.



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

def application
  @application
end

#expansionObject (readonly)

Returns the value of attribute expansion.



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

def expansion
  @expansion
end

#thresholdObject (readonly)

Returns the value of attribute threshold.



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

def threshold
  @threshold
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

#argumentsObject



153
154
155
# File 'lib/ruby-prof/printers/call_stack_printer.rb', line 153

def arguments
  ARGV.join(' ')
end

#base64_imageObject



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

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


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

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


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

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_tree) ⇒ Object



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

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

Specify print options.

output - Any IO object, including STDOUT or a file.

Keyword arguments:

title:       - a String to override the default "ruby-prof call stack"
               title of the report.

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

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 override the name of the application,
               as it appears on the report.

Also accepts min_percent:, max_percent:, filter_by:, and sort_method: from AbstractPrinter.



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

def print(output = STDOUT, title: "ruby-prof call stack", threshold: 1.0,
          expansion: 10.0, application: $PROGRAM_NAME,
          min_percent: 0, max_percent: 100, filter_by: :self_time, sort_method: nil, **)
  @min_percent = min_percent
  @max_percent = max_percent
  @filter_by = filter_by
  @sort_method = sort_method
  @title = title
  @threshold = threshold
  @expansion = expansion
  @application = application
  output << ERB.new(self.template).result(binding)
end


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

def print_stack(output, visited, call_tree, parent_time)
  total_time = call_tree.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)
  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_tree)
    output << "<a href=\"#\" class=\"toggle empty\" ></a>" << "\n"
    output << "<span>%s %s</span>" % [link(call_tree.target, true), graph_link(call_tree)] << "\n"
  else
    visited << call_tree

    if call_tree.children.empty?
      output << "<a href=\"#\" class=\"toggle empty\" ></a>" << "\n"
    else
      visible_children = call_tree.children.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_tree.target, false), graph_link(call_tree)] << "\n"

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

    visited.delete(call_tree)
  end
  output << '</li>' << "\n"
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



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

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

#total_time(call_trees) ⇒ Object



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

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