Class: Tuttle::RubyProf::FastCallStackPrinter

Inherits:
RubyProf::CallStackPrinter
  • Object
show all
Defined in:
lib/tuttle/ruby_prof/fast_call_stack_printer.rb

Overview

prints a HTML visualization of the call tree

Instance Method Summary collapse

Instance Method Details

#applicationObject



136
137
138
# File 'lib/tuttle/ruby_prof/fast_call_stack_printer.rb', line 136

def application
  @application ||= @options.delete(:application) || $PROGRAM_NAME
end

#expansionObject



132
133
134
# File 'lib/tuttle/ruby_prof/fast_call_stack_printer.rb', line 132

def expansion
  @expansion ||= super
end

#method_full_name(method) ⇒ Object



124
125
126
# File 'lib/tuttle/ruby_prof/fast_call_stack_printer.rb', line 124

def method_full_name(method)
  @method_full_name_cache[method] ||= h(method.full_name)
end

#name(call_info) ⇒ Object



119
120
121
122
# File 'lib/tuttle/ruby_prof/fast_call_stack_printer.rb', line 119

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.

:print_file  - True or false. Specifies if a method's source
               file should be printed.  Default value if false.

: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.


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
# File 'lib/tuttle/ruby_prof/fast_call_stack_printer.rb', line 39

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

  print_header

  @overall_threads_time = @result.threads.inject(0) do |val, thread|
    val += thread.total_time
  end

  @method_full_name_cache = Hash.new.compare_by_identity

  @result.threads.each do |thread|
    @overall_time = thread.total_time
    thread_info = "Thread: #{thread.id}"
    thread_info << ", Fiber: #{thread.fiber_id}" unless thread.id == thread.fiber_id
    thread_info << " (#{"%4.2f%%" % ((@overall_time/@overall_threads_time)*100)} ~ #{@overall_time})"
    @output.print "<div class=\"thread\">#{thread_info}</div>"
    @output.print "<ul name=\"thread\">"
    thread.methods.each do |m|
      # $stderr.print m.dump
      next unless m.root?
      m.call_infos.each do |ci|
        next unless ci.root?
        print_stack ci, @overall_time
      end
    end
    @output.print "</ul>"
  end

  @method_full_name_cache = nil

  print_footer

end


149
150
151
152
153
154
155
156
157
158
# File 'lib/tuttle/ruby_prof/fast_call_stack_printer.rb', line 149

def print_css
  super
  @output.puts <<-CSS_OVERRIDE
<style type="text/css">
<!--
ul li { background-color: white; }
-->
</style>
CSS_OVERRIDE
end


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
# File 'lib/tuttle/ruby_prof/fast_call_stack_printer.rb', line 75

def print_stack(call_info, parent_time)
  total_time = call_info.total_time
  percent_total = (total_time/@overall_time)*100
  return unless percent_total > min_percent

  percent_parent = (total_time/parent_time)*100
  if percent_total < threshold
    @output.write "<li style=\"display:none;\">".freeze
  else
    @output.write "<li>".freeze
  end

  expanded = percent_total >= expansion
  kids = call_info.children

  toggle_href = if kids.empty? || kids.none?{|ci| (ci.total_time/@overall_time)*100 >= threshold}
                  "<a href=\"#\" class=\"toggle empty\" ></a>".freeze
                else
                  if expanded
                    "<a href=\"#\" class=\"toggle minus\" ></a>".freeze
                  else
                    "<a href=\"#\" class=\"toggle plus\" ></a>".freeze
                  end
                end
  @output.write toggle_href

  method = call_info.target
  @output.printf "<span>%4.2f%% (%4.2f%%) %s [%d calls, %d total]</span>\n".freeze,
                 percent_total, percent_parent,
                 method_full_name(method), call_info.called, method.called
  unless kids.empty?
    if expanded
      @output.write "<ul>".freeze
    else
      @output.write '<ul style="display:none">'.freeze
    end
    kids.sort_by!(&:total_time).reverse_each do |callinfo|
      print_stack callinfo, total_time
    end
    @output.write "</ul>".freeze
  end
  @output.write "</li>".freeze
end


140
141
142
143
144
145
146
147
# File 'lib/tuttle/ruby_prof/fast_call_stack_printer.rb', line 140

def print_title_bar
  @output.puts <<-"end_title_bar"
<div id="titlebar">
Call tree for application <b>#{h application} #{h arguments}</b><br/>
Generated on #{Time.now} with options #{h @options.inspect}<br/>
</div>
end_title_bar
end

#thresholdObject



128
129
130
# File 'lib/tuttle/ruby_prof/fast_call_stack_printer.rb', line 128

def threshold
  @threshold ||= super
end