class Rack::Lineprof

Constants

CONTEXT
CRITICAL
DEFAULT_LOGGER
NOMINAL
WARNING

Attributes

app[R]
options[R]

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/rack/lineprof.rb, line 27
def initialize(app, options = {})
  @app, @options = app, options
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/lineprof.rb, line 31
def call(env)
  request = Rack::Request.new(env)
  matcher = request.params['lineprof'] || options[:profile]
  logger  = options[:logger] || DEFAULT_LOGGER

  return @app.call(env) unless matcher

  response = nil
  profile = lineprof(%r{#{matcher}}) { response = @app.call(env) }

  logger.error Term::ANSIColor.blue("\n[Rack::Lineprof] #{'=' * 63}") + "\n\n" + format_profile(profile) + "\n"

  response
end
format_profile(profile) click to toggle source
# File lib/rack/lineprof.rb, line 46
def format_profile(profile)
  sources = profile.map do |filename, samples|
    Source.new(filename, samples, options)
  end

  sources.map(&:format).compact.join "\n"
end