class Rack::Profiler

Set the profile=process_time query parameter to download a calltree profile of the request.

Pass the :printer option to pick a different result format. Note that some printers (such as CallTreePrinter) have broken the `AbstractPrinter` API, and thus will not work. Bug reports to `ruby-prof`, please, not us.

You can cause every request to be run multiple times by passing the `:times` option to the `use Rack::Profiler` call. You can also run a given request multiple times, by setting the `profiler_runs` query parameter in the request URL.

Constants

CONTENT_TYPES
DEFAULT_PRINTER
MODES

Public Class Methods

new(app, options = {}) click to toggle source

Accepts a :printer => [:call_stack|:call_tree|:graph_html|:graph|:flat] option defaulting to :call_stack.

   # File lib/rack/contrib/profiler.rb
33 def initialize(app, options = {})
34   @app = app
35   @printer = parse_printer(options[:printer] || DEFAULT_PRINTER)
36   @times = (options[:times] || 1).to_i
37 end

Public Instance Methods

call(env) click to toggle source
   # File lib/rack/contrib/profiler.rb
39 def call(env)
40   if mode = profiling?(env)
41     profile(env, mode)
42   else
43     @app.call(env)
44   end
45 end

Private Instance Methods

camel_case(word) click to toggle source
    # File lib/rack/contrib/profiler.rb
107 def camel_case(word)
108   word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }
109 end
headers(printer, env, mode) click to toggle source
   # File lib/rack/contrib/profiler.rb
84 def headers(printer, env, mode)
85   headers = { 'Content-Type' => CONTENT_TYPES[printer.name] }
86   if printer == ::RubyProf::CallTreePrinter
87     filename = ::File.basename(env['PATH_INFO'])
88     headers['Content-Disposition'] =
89       %(attachment; filename="#{filename}.#{mode}.tree")
90   end
91   headers
92 end
parse_printer(printer) click to toggle source
    # File lib/rack/contrib/profiler.rb
 94 def parse_printer(printer)
 95   if printer.is_a?(Class)
 96     printer
 97   else
 98     name = "#{camel_case(printer)}Printer"
 99     if ::RubyProf.const_defined?(name)
100       ::RubyProf.const_get(name)
101     else
102       ::RubyProf::FlatPrinter
103     end
104   end
105 end
print(printer, result) click to toggle source
profile(env, mode) click to toggle source
   # File lib/rack/contrib/profiler.rb
63 def profile(env, mode)
64   ::RubyProf.measure_mode = ::RubyProf.const_get(mode.upcase)
65 
66   GC.enable_stats if GC.respond_to?(:enable_stats)
67   request = Rack::Request.new(env.clone)
68   runs = (request.params['profiler_runs'] || @times).to_i
69   result = ::RubyProf.profile do
70     runs.times { @app.call(env) }
71   end
72   GC.disable_stats if GC.respond_to?(:disable_stats)
73 
74   [200, headers(@printer, env, mode), print(@printer, result)]
75 end
profiling?(env) click to toggle source
   # File lib/rack/contrib/profiler.rb
48 def profiling?(env)
49   unless ::RubyProf.running?
50     request = Rack::Request.new(env.clone)
51     if mode = request.params.delete('profile')
52       if ::RubyProf.const_defined?(mode.upcase)
53         mode
54       else
55         env['rack.errors'].write "Invalid RubyProf measure_mode: " +
56           "#{mode}. Use one of #{MODES.to_a.join(', ')}"
57         false
58       end
59     end
60   end
61 end