Class: RubyProf::AbstractPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-prof/printers/abstract_printer.rb

Overview

This is the base class for all Printers. It is never used directly.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ AbstractPrinter

Create a new printer.

result should be the output generated from a profiling run



15
16
17
18
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 15

def initialize(result)
  @result = result
  @output = nil
end

Class Method Details

.needs_dir?Boolean

:stopdoc:

Returns:

  • (Boolean)


7
8
9
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 7

def self.needs_dir?
  false
end

Instance Method Details

#filter_byObject

Returns the method to filter methods by (when using min_percent and max_percent)



31
32
33
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 31

def filter_by
  @options[:filter_by] || :self_time
end

#max_percentObject

Returns the max_percent of time a method can take to be included in a profiling report



26
27
28
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 26

def max_percent
  @options[:max_percent] || 100
end

#method_href(thread, method) ⇒ Object



80
81
82
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 80

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

#method_location(method) ⇒ Object



74
75
76
77
78
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 74

def method_location(method)
  if method.source_file
    "#{method.source_file}:#{method.line}"
  end
end

#min_percentObject

Returns the min_percent of time a method must take to be included in a profiling report



21
22
23
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 21

def min_percent
  @options[:min_percent] || 0
end

#open_asset(file) ⇒ Object



84
85
86
87
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 84

def open_asset(file)
  path = File.join(File.expand_path('../../assets', __FILE__), file)
  File.open(path, 'rb').read
end

Prints a report to the provided output.

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

options - Hash of print options. Note that each printer can define its own set of options.

:min_percent - Number 0 to 100 that specifies 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.

:sort_method - Specifies method used for sorting method infos.
               Available values are :total_time, :self_time,
               :wait_time, :children_time
               Default value is :total_time


63
64
65
66
67
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 63

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


111
112
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 111

def print_column_headers
end


114
115
116
117
118
119
120
121
122
123
124
125
126
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
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 114

def print_footer(thread)
  metric_data = {
    0 => { label: "time", prefix: "", suffix: "spent" },
    1 => { label: "time", prefix: "", suffix: "spent" },
    2 => { label: "allocations", prefix: "number of ", suffix: "made" },
    3 => { label: "memory", prefix: "", suffix: "used" }
  }

  metric = metric_data[@result.measure_mode]

  metric_label = metric[:label]
  metric_suffix = metric[:suffix]
  metric_prefix = metric[:prefix]

  metric1 = "#{metric_label} #{metric_suffix}"
  metric2 = "#{metric_prefix}#{metric1}"
  metric3 = metric_label

  # Output the formatted text
  @output << "\n    * recursively called methods\n\n    Columns are:\n\n      %self     - The percentage of \#{metric1} by this method relative to the total \#{metric3} in the entire program.\n      total     - The total \#{metric2} by this method and its children.\n      self      - The \#{metric2} by this method.\n      wait      - The time this method spent waiting for other threads.\n      child     - The \#{metric2} by this method's children.\n      calls     - The number of times this method was called.\n      name      - The name of the method.\n      location  - The location of the method.\n\n    The interpretation of method names is:\n\n      * MyObject#test - An instance method \"test\" of the class \"MyObject\"\n      * <Object:MyObject>#test - The <> characters indicate a method on a singleton class.\n\n  EOT\nend\n"


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

def print_header(thread)
  @output << "Measure Mode: %s\n" % @result.measure_mode_string
  @output << "Thread ID: %d\n" % thread.id
  @output << "Fiber ID: %d\n" % thread.fiber_id unless thread.id == thread.fiber_id
  @output << "Total: %0.6f\n" % thread.total_time
  @output << "Sort by: #{sort_method}\n"
  @output << "\n"
  print_column_headers
end


95
96
97
98
99
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 95

def print_thread(thread)
  print_header(thread)
  print_methods(thread)
  print_footer(thread)
end


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

def print_threads
  @result.threads.each do |thread|
    print_thread(thread)
  end
end

#setup_options(options = {}) ⇒ Object

:nodoc:



70
71
72
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 70

def setup_options(options = {})
  @options = options
end

#sort_methodObject

Returns how profile data should be sorted



41
42
43
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 41

def sort_method
  @options[:sort_method]
end

#time_formatObject

Returns the time format used to show when a profile was run



36
37
38
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 36

def time_format
  '%A, %B %-d at %l:%M:%S %p (%Z)'
end