Class: Merb::Rack::Profiler

Inherits:
Middleware show all
Defined in:
lib/merb-core/rack/middleware/profiler.rb

Instance Method Summary collapse

Methods included from DeferrableMiddleware

#deferred?

Constructor Details

#initialize(app, types = [RubyProf::ALLOCATIONS, RubyProf::PROCESS_TIME]) ⇒ Profiler

:api: private



12
13
14
15
# File 'lib/merb-core/rack/middleware/profiler.rb', line 12

def initialize(app, types = [RubyProf::ALLOCATIONS, RubyProf::PROCESS_TIME])
  super(app)
  @types = types
end

Instance Method Details

#call(env) ⇒ Object

:api: plugin



18
19
20
21
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
49
50
# File 'lib/merb-core/rack/middleware/profiler.rb', line 18

def call(env)
  measure_names = { RubyProf::ALLOCATIONS => 'allocations', 
    RubyProf::PROCESS_TIME => 'time', RubyProf::MEMORY => "memory" }

  ret = nil

  GC.disable
  @types.each do |type|
    next if type.nil?
    
    if GC.respond_to?(:enable_stats)
      GC.enable_stats || GC.clear_stats
    end

    RubyProf.measure_mode = type
    RubyProf.start
    100.times do
      ret = super
    end
    result = RubyProf.stop
    printer = RubyProf::CallTreePrinter.new(result)
    path = "merb_profile_results" / env["PATH_INFO"]
    FileUtils.mkdir_p(path)
    printer.print(
      File.open(
        "#{path}/callgrind.out.#{measure_names[RubyProf::measure_mode]}",     
        'w'))

    GC.disable_stats if GC.respond_to?(:disable_stats)
  end
  GC.enable
  ret
end