Class: RubyProf::GraphHtmlPrinterEnhanced

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

Overview

This is mostly from ruby_forge, with some optimization changes.

Constant Summary collapse

MIN_TIME =
0.01
MIN_THREAD_TIME =
0.0
PERCENTAGE_WIDTH =
8
TIME_WIDTH =
10
CALL_WIDTH =
20

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ GraphHtmlPrinterEnhanced

Create a GraphPrinter. Result is a RubyProf::Result

object generated from a profiling run.



37
38
39
40
41
# File 'lib/ruby-prof/graph_html_printer_enhanced.rb', line 37

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 –



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby-prof/graph_html_printer_enhanced.rb', line 63

def calculate_thread_times
  # Cache thread times since this is an expensive
  # operation with the required sorting      
  @result.threads.each do |thread_id, methods|
    top = methods.sort.last
    
    thread_time = 0.01
    thread_time = top.total_time if top.total_time > 0

    @thread_times[thread_id] = thread_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.



103
104
105
106
107
108
109
110
111
# File 'lib/ruby-prof/graph_html_printer_enhanced.rb', line 103

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

#method_href(thread_id, method) ⇒ Object



113
114
115
# File 'lib/ruby-prof/graph_html_printer_enhanced.rb', line 113

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.


51
52
53
54
55
56
57
58
# File 'lib/ruby-prof/graph_html_printer_enhanced.rb', line 51

def print(output = STDOUT, options = {})
  @output = output
  setup_options(options)
  
  _erbout = @output
  erb = ERB.new(template, nil, nil)
  @output << erb.result(binding)
end

#select_methods(methods) ⇒ Object



80
81
82
83
# File 'lib/ruby-prof/graph_html_printer_enhanced.rb', line 80

def select_methods(methods)
  return [] unless methods
  methods.select {|method| method.total_time >= MIN_TIME }
end

#select_threads(threads) ⇒ Object



85
86
87
# File 'lib/ruby-prof/graph_html_printer_enhanced.rb', line 85

def select_threads(threads)
  threads.select {|thread_id, methods| thread_time(thread_id) >= MIN_THREAD_TIME }
end

#self_percent(method) ⇒ Object



94
95
96
97
# File 'lib/ruby-prof/graph_html_printer_enhanced.rb', line 94

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

#templateObject



117
118
119
# File 'lib/ruby-prof/graph_html_printer_enhanced.rb', line 117

def template
  return IO.read(File.dirname(__FILE__) + "/template.rhtml")
end

#thread_time(thread_id) ⇒ Object



76
77
78
# File 'lib/ruby-prof/graph_html_printer_enhanced.rb', line 76

def thread_time(thread_id)
  @thread_times[thread_id]
end

#total_percent(thread_id, method) ⇒ Object



89
90
91
92
# File 'lib/ruby-prof/graph_html_printer_enhanced.rb', line 89

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