Skip to content

Instantly share code, notes, and snippets.

@stellard
Created March 23, 2012 09:46
Show Gist options
  • Save stellard/2169089 to your computer and use it in GitHub Desktop.
Save stellard/2169089 to your computer and use it in GitHub Desktop.
Scott Trace
# -*- encoding : utf-8 -*-
class Object
def scott_trace
now = Time.now
$last_scott_trace ||= now
diff = now - $last_scott_trace
notify = diff > 1 ? "!!!!!!!!!!!!!!" : ""
puts "#{notify} RAM USAGE: #{`ps -o rss= -p #{$$}`.to_f / 1000.0} MB TIME:#{now - $last_scott_trace}, CALLER: #{CallChain.caller_info.inspect}"
$last_scott_trace = now
end
end
class CallChain
def self.caller_method(depth=1)
parse_caller(caller(depth+1).first).last
end
def self.caller_info(depth=1)
parse_caller(caller(depth+1).first)
end
private
#Stolen from ActionMailer, where this was used but was not made reusable
def self.parse_caller(at)
if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
file = Regexp.last_match[1]
line = Regexp.last_match[2].to_i
method = Regexp.last_match[3]
[file, line, method]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment