Class: Rack::Lineprof

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/lineprof.rb,
lib/rack/lineprof/sample.rb,
lib/rack/lineprof/source.rb

Defined Under Namespace

Classes: Sample, Source

Constant Summary collapse

CONTEXT =
0
NOMINAL =
1
WARNING =
2
CRITICAL =
3
DEFAULT_LOGGER =
if defined?(::Rails)
  if ::Rails.env.development?
    ::Logger.new(STDOUT)
  else
    ::Logger.new(::Rails.root.join('log/profiler.log'))
  end
else
  ::Logger.new(STDOUT)
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Lineprof.



27
28
29
# File 'lib/rack/lineprof.rb', line 27

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



25
26
27
# File 'lib/rack/lineprof.rb', line 25

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/rack/lineprof.rb', line 25

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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) ⇒ Object



46
47
48
49
50
51
52
# 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