Class: Rack::RubyProf

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-prof/rack.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ RubyProf

Returns a new instance of RubyProf.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruby-prof/rack.rb', line 6

def initialize(app, options = {})
  @app = app
  @options = options
  @options[:min_percent] ||= 1

  @tmpdir = options[:path] || Dir.tmpdir
  FileUtils.mkdir_p(@tmpdir)

  @printer_klasses = @options[:printers]  || {::RubyProf::FlatPrinter => 'flat.txt',
                                              ::RubyProf::GraphPrinter => 'graph.txt',
                                              ::RubyProf::GraphHtmlPrinter => 'graph.html',
                                              ::RubyProf::CallStackPrinter => 'call_stack.html'}

  @skip_paths = options[:skip_paths] || [%r{^/assets}, %r{\.(css|js|png|jpeg|jpg|gif)$}]
  @only_paths = options[:only_paths]
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby-prof/rack.rb', line 23

def call(env)
  request = Rack::Request.new(env)

  if should_profile?(request.path)
    begin
      result = nil
      data = ::RubyProf::Profile.profile(profiling_options) do
        result = @app.call(env)
      end

      path = request.path.gsub('/', '-')
      path.slice!(0)

      print(data, path)
      result
    end
  else
    @app.call(env)
  end
end