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

#method_href(thread, method) ⇒ Object



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

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

#method_location(method) ⇒ Object



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

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

#min_percentObject

Returns the min_percent of total 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



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

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

: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


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

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


101
102
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 101

def print_column_headers
end


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 104

def print_footer(thread)
  @output << <<~EOT
  
    * recursively called methods

    Columns are:

      %self     - The percentage of time spent in this method, derived from self_time/total_time.
      total     - The time spent in this method and its children.
      self      - The time spent in this method.
      wait      - The amount of time this method waited for other threads.
      child     - The time spent in this method's children.
      calls     - The number of times this method was called.
      name      - The name of the method.
      location  - The location of the method.

    The interpretation of method names is:

      * MyObject#test - An instance method "test" of the class "MyObject"
      * <Object:MyObject>#test - The <> characters indicate a method on a singleton class.
  EOT
end


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

def print_header(thread)
  @output << "Measure Mode: %s\n" % RubyProf.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


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

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


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

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

#setup_options(options = {}) ⇒ Object

:nodoc:



60
61
62
# File 'lib/ruby-prof/printers/abstract_printer.rb', line 60

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

#sort_methodObject

Returns how profile data should be sorted



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

def sort_method
  @options[:sort_method]
end

#time_formatObject

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



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

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