Class: Rack::RubyProf

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

Defined Under Namespace

Classes: RackProfiler

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[:min_percent] ||= 1

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

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

  @max_requests = options[:max_requests]

  @options = options
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
47
48
# File 'lib/ruby-prof/rack.rb', line 22

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

  if should_profile?(request.path)
    profiler.resume
    begin
      result = @app.call(env)
    ensure
      profiler.pause
    end

    if profiler.max_requests_reached?
      prefix = if aggregate_requests?
          nil
        else
          request.path.gsub('/', '-')[1..-1]
        end

      profiler.print!(prefix)
      delete_profiler!
    end

    result
  else
    @app.call(env)
  end
end