class Rack::MemProf::Middleware

Constants

REPDIR
REPEXT
UNSLUGGABLE

Public Class Methods

new(app, options = {}) click to toggle source
# 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

Public Instance Methods

call(env) click to toggle source
# 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

Private Instance Methods

build_report_path(env) click to toggle source
# File lib/rack/mem_prof/middleware.rb, line 51
def build_report_path(env)
  return generate_path(env['PATH_INFO']), generate_filename(env['QUERY_STRING'])
end
generate_dirname(path_info) click to toggle source
# File lib/rack/mem_prof/middleware.rb, line 60
def generate_dirname(path_info)
  dirname = path_info.gsub(/[^0-9A-Za-z]/, '').downcase

  return UNSLUGGABLE if dirname.length.zero?
end
generate_filename(query_string) click to toggle source
# File lib/rack/mem_prof/middleware.rb, line 66
def generate_filename(query_string)
  Digest::SHA1.hexdigest(query_string).concat(REPEXT)
end
generate_path(path_info) click to toggle source
# File lib/rack/mem_prof/middleware.rb, line 55
def generate_path(path_info)
  dirname_for_request = generate_dirname(path_info)
  ::File.join(reports_path, dirname_for_request)
end
reports_path() click to toggle source
# File lib/rack/mem_prof/middleware.rb, line 70
def reports_path
  ::File.join(@path, REPDIR)
end
write_report(report, path) click to toggle source

report to “/tmp/rack_mem_prof/<request path>/<query params sha1>.txt”

# File lib/rack/mem_prof/middleware.rb, line 44
def write_report(report, path)
  report.pretty_print(
    scale_bytes: @scale_bytes,
    to_file: path,
  )
end