Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Merb profiler to output call tree
- Loading branch information
Showing
2 changed files
with
41 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,55 @@ | ||
begin | ||
require "ruby-prof" | ||
rescue LoadError => e | ||
Merb.fatal! "You need ruby-prof installed to use the profiler middleware", e | ||
end | ||
|
||
module Merb | ||
module Rack | ||
class Profiler < Merb::Rack::Middleware | ||
|
||
# :api: private | ||
def initialize(app, min=1, iter=1) | ||
def initialize(app, types = [RubyProf::ALLOCATIONS, RubyProf::PROCESS_TIME]) | ||
super(app) | ||
@min, @iter = min, iter | ||
@types = types | ||
end | ||
|
||
# :api: plugin | ||
def call(env) | ||
__profile__("profile_output", @min, @iter) do | ||
@app.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 | ||
|
||
|
||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters