Skip to content

Commit

Permalink
Serve call stacks as HTML. Make CallStackPrinter the default since it…
Browse files Browse the repository at this point in the history
…'s much easier to deal with than the other formats.
  • Loading branch information
jeremy committed Sep 8, 2011
1 parent ae55f71 commit acdcb25
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
38 changes: 14 additions & 24 deletions lib/rack/contrib/profiler.rb
Expand Up @@ -6,30 +6,22 @@ module Rack
#
# Pass the :printer option to pick a different result format.
class Profiler
MODES = %w(
process_time
wall_time
cpu_time
allocations
memory
gc_runs
gc_time
)
MODES = %w(process_time wall_time cpu_time
allocations memory gc_runs gc_time)

DEFAULT_PRINTER = ::RubyProf::CallTreePrinter
DEFAULT_CONTENT_TYPE = 'application/octet-stream'
DEFAULT_PRINTER = :call_stack

PRINTER_CONTENT_TYPE = {
::RubyProf::FlatPrinter => 'text/plain',
::RubyProf::GraphPrinter => 'text/plain',
::RubyProf::GraphHtmlPrinter => 'text/html'
}
CONTENT_TYPES = Hash.new('application/octet-stream').merge(
'RubyProf::FlatPrinter' => 'text/plain',
'RubyProf::GraphPrinter' => 'text/plain',
'RubyProf::GraphHtmlPrinter' => 'text/html',
'RubyProf::CallStackPrinter' => 'text/html')

# Accepts a :printer => [:call_tree|:graph_html|:graph|:flat] option
# defaulting to :call_tree.
# Accepts a :printer => [:call_stack|:call_tree|:graph_html|:graph|:flat]
# option defaulting to :call_stack.
def initialize(app, options = {})
@app = app
@printer = parse_printer(options[:printer])
@printer = parse_printer(options[:printer] || DEFAULT_PRINTER)
@times = (options[:times] || 1).to_i
end

Expand Down Expand Up @@ -77,7 +69,7 @@ def print(printer, result)
end

def headers(printer, env, mode)
headers = { 'Content-Type' => PRINTER_CONTENT_TYPE[printer] || DEFAULT_CONTENT_TYPE }
headers = { 'Content-Type' => CONTENT_TYPES[printer.name] }
if printer == ::RubyProf::CallTreePrinter
filename = ::File.basename(env['PATH_INFO'])
headers['Content-Disposition'] =
Expand All @@ -87,16 +79,14 @@ def headers(printer, env, mode)
end

def parse_printer(printer)
if printer.nil?
DEFAULT_PRINTER
elsif printer.is_a?(Class)
if printer.is_a?(Class)
printer
else
name = "#{camel_case(printer)}Printer"
if ::RubyProf.const_defined?(name)
::RubyProf.const_get(name)
else
DEFAULT_PRINTER
::RubyProf::FlatPrinter
end
end
end
Expand Down
11 changes: 8 additions & 3 deletions test/spec_rack_profiler.rb
Expand Up @@ -8,14 +8,19 @@
app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'Oh hai der'] }
request = Rack::MockRequest.env_for("/", :params => "profile=process_time")

specify 'printer defaults to RubyProf::CallTreePrinter' do
specify 'printer defaults to RubyProf::CallStackPrinter' do
profiler = Rack::Profiler.new(nil)
profiler.instance_variable_get('@printer').should.equal RubyProf::CallTreePrinter
profiler.instance_variable_get('@printer').should.equal RubyProf::CallStackPrinter
profiler.instance_variable_get('@times').should.equal 1
end

specify 'CallStackPrinter has Content-Type test/html' do
headers = Rack::Profiler.new(app, :printer => :call_stack).call(request)[1]
headers.should.equal "Content-Type"=>"text/html"
end

specify 'CallTreePrinter has correct headers' do
headers = Rack::Profiler.new(app).call(request)[1]
headers = Rack::Profiler.new(app, :printer => :call_tree).call(request)[1]
headers.should.equal "Content-Disposition"=>"attachment; filename=\"/.process_time.tree\"", "Content-Type"=>"application/octet-stream"
end

Expand Down

0 comments on commit acdcb25

Please sign in to comment.