Permalink
Browse files

Serve call stacks as HTML. Make CallStackPrinter the default since it…

…'s much easier to deal with than the other formats.
  • Loading branch information...
1 parent ae55f71 commit acdcb256bb6e7da2f86a02457e287a338cdbde5e @jeremy jeremy committed Sep 8, 2011
Showing with 22 additions and 27 deletions.
  1. +14 −24 lib/rack/contrib/profiler.rb
  2. +8 −3 test/spec_rack_profiler.rb
View
38 lib/rack/contrib/profiler.rb
@@ -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
@@ -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'] =
@@ -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
View
11 test/spec_rack_profiler.rb
@@ -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

0 comments on commit acdcb25

Please sign in to comment.