Class: Rack::RequestProfiler
- Inherits:
-
Object
- Object
- Rack::RequestProfiler
- Defined in:
- lib/rack/request_profiler.rb
Instance Method Summary collapse
- #call(env) ⇒ Object
- #format(printer) ⇒ Object
-
#initialize(app, options = {}) ⇒ RequestProfiler
constructor
A new instance of RequestProfiler.
- #prefix(printer) ⇒ Object
- #profile_mode(request) ⇒ Object
- #write_result(result, request) ⇒ Object
Constructor Details
#initialize(app, options = {}) ⇒ RequestProfiler
Returns a new instance of RequestProfiler.
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/rack/request_profiler.rb', line 5 def initialize(app, = {}) @app = app @printer = [:printer] || ::RubyProf::GraphHtmlPrinter @exclusions = [:exclude] @path = [:path] @path ||= Rails.root + 'tmp/performance' if defined?(Rails) @path ||= ::File.join((ENV["TMPDIR"] || "/tmp"), 'performance') @path = Pathname(@path) end |
Instance Method Details
#call(env) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rack/request_profiler.rb', line 16 def call(env) request = Rack::Request.new(env) mode = profile_mode(request) if mode ::RubyProf.measure_mode = mode ::RubyProf.start end status, headers, body = @app.call(env) if mode result = ::RubyProf.stop write_result(result, request) end [status, headers, body] end |
#format(printer) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rack/request_profiler.rb', line 45 def format(printer) case printer when ::RubyProf::FlatPrinter 'txt' when ::RubyProf::FlatPrinterWithLineNumbers 'txt' when ::RubyProf::GraphPrinter 'txt' when ::RubyProf::GraphHtmlPrinter 'html' when ::RubyProf::DotPrinter 'dot' when ::RubyProf::CallTreePrinter "out.#{Process.pid}" when ::RubyProf::CallStackPrinter 'html' else 'txt' end end |
#prefix(printer) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/rack/request_profiler.rb', line 66 def prefix(printer) case printer when ::RubyProf::CallTreePrinter "callgrind." else "" end end |
#profile_mode(request) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rack/request_profiler.rb', line 34 def profile_mode(request) mode_string = request.params["profile_request"] if mode_string if mode_string.downcase == "true" or mode_string == "1" ::RubyProf::PROCESS_TIME else ::RubyProf.const_get(mode_string.upcase) end end end |
#write_result(result, request) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rack/request_profiler.rb', line 75 def write_result(result, request) result.eliminate_methods!(@exclusions) if @exclusions printer = @printer.new(result) Dir.mkdir(@path) unless ::File.exists?(@path) url = request.fullpath.gsub(/[?\/]/, '-') filename = "#{prefix(printer)}#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}-#{url.slice(0, 50)}.#{format(printer)}" ::File.open(@path + filename, 'w+') do |f| printer.print(f) end end |