Jonas Arvidsson
12/2/2004 8:39:00 PM
> Joe Van Dyk wrote:
>> Hi,
>>
>> Still pretty new to Ruby and profiling. Are there any
>> tools I can use to analyze my Ruby program to see where
>> it's spending the bulk of the CPU time?
>>
>> Thanks,
>> Joe
>
> I found the '-r profile' option. Are there any better tools out there?
>
Hello,
I felt the need for something that pinpoints which lines in
MY code are bottlenecks, so I wrote profstack.rb, which is a lot
slower than profile.rb, but presents the calling stack for each
entry in the list. Something I find useful.
Perhaps there's a smarter way to do this. Redefining Symbol#id2name
seems very costly, but at least I was able to modify the behavior
of profile.rb without modifying profile.rb.
Regards,
--Jonas
# Include stack trace in profile report.
# By requiring this file, you get profile reporting like this:
#
# % cumulative self self total
# time seconds seconds calls ms/call ms/call name
# 47.37 34.17 34.17 1 34173.00 64813.00 Range#each
# file.rb:141 each
# file.rb:141 process
# file.rb:328
require 'profile'
class Symbol
alias_method :old_id2name, :id2name
def id2name
stack = ""
c = caller()
if c.to_s =~ /profiler\.rb/ # Have we been called by the profiler?
c.each { |frame|
next if frame =~ /profiler\.rb/ # Skip internal profiler frames
stack += "\n" + (" " * 54) + frame.to_s.sub(/:in `(.*)'$/, ' \1')
}
end
old_id2name() + stack
end
end