Class: Rack::MemProf::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/mem_prof/middleware.rb

Constant Summary collapse

REPEXT =
'.txt'.freeze
REPDIR =
'rack_mem_prof'.freeze
UNSLUGGABLE =
'stub_for_unslaggable_requests'.freeze

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Middleware.



13
14
15
16
17
# File 'lib/rack/mem_prof/middleware.rb', line 13

def initialize(app, options = {})
  @app = app
  @path = options[:path] || "/tmp"
  @scale_bytes = options[:scale_bytes] || true
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rack/mem_prof/middleware.rb', line 19

def call(env)
  report_path, report_filename = build_report_path(env)
  path_with_file = ::File.join(report_path, report_filename)

  if ::File.exists?(path_with_file)
    status, headers, body = @app.call(env)
  else
    FileUtils.mkdir_p report_path

    MemoryProfiler.start

    status, headers, body = @app.call(env)

    write_report(MemoryProfiler.stop, path_with_file)
  end

  [status, headers, body]
rescue => e
  MemoryProfiler.stop
  raise e
end