Skip to content

Instantly share code, notes, and snippets.

@haileys
Forked from SamSaffron/gist:2340416
Created April 9, 2012 01:51
Show Gist options
  • Save haileys/2340780 to your computer and use it in GitHub Desktop.
Save haileys/2340780 to your computer and use it in GitHub Desktop.
MiniProfiler API test
class Animal
def walk(distance, unit)
sleep 0.1
puts "walked #{distance} #{unit}"
end
end
class Dog < Animal
end
module MiniProfiler
def self.profile(klass, method, before, after)
with_profiling = (method.to_s + "_with_mini_profiler").intern
without_profiling = (method.to_s + "_without_mini_profiler").intern
klass.send :alias_method, without_profiling, method
klass.send :define_method, with_profiling do |*args|
before.call *args
start = Time.now
self.send without_profiling, *args
after.call ((Time.now - start) * 1000)
end
klass.send :alias_method, method, with_profiling
end
end
MiniProfiler.profile(Animal, :walk, Proc.new{|distance,unit| puts "intercepted #{distance} #{unit}"}, Proc.new{|duration| puts "took #{duration}ms"})
Dog.new.walk(100, "miles")
#intercepted 100 miles
#walked 100 miles
#took 100.343863ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment