Created
April 8, 2012 23:48
-
-
Save SamSaffron/2340416 to your computer and use it in GitHub Desktop.
MiniProfiler API test
This file contains hidden or 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
class Animal | |
def walk(distance, unit) | |
sleep 0.1 | |
puts "walked #{distance} #{unit}" | |
end | |
end | |
class Dog < Animal | |
end | |
class Module | |
def define_method_mini_profiler(name, &block) | |
define_method name, block | |
end | |
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.define_method_mini_profiler 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