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
# File 'lib/ruby-prof/rack.rb', line 6

def initialize(app, options = {})
  @app = app
  @options = options

  @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



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

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

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

      if @options[:merge_fibers]
        profile.merge!
      end


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

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