def profile_method(klass, method, type = :profile, &blk)
default_name = type==:counter ? method.to_s : klass.to_s + " " + method.to_s
clean = clean_method_name(method)
with_profiling = ("#{clean}_with_mini_profiler").intern
without_profiling = ("#{clean}_without_mini_profiler").intern
if klass.send :method_defined?, with_profiling
return
end
klass.send :alias_method, without_profiling, method
klass.send :define_method, with_profiling do |*args, &orig|
return self.send without_profiling, *args, &orig unless Rack::MiniProfiler.current
name = default_name
if blk
name =
if respond_to?(:instance_exec)
instance_exec(*args, &blk)
else
blk.bind(self).call(*args)
end
end
parent_timer = Rack::MiniProfiler.current.current_timer
if type == :counter
start = Time.now
begin
self.send without_profiling, *args, &orig
ensure
duration_ms = (Time.now - start).to_f * 1000
parent_timer.add_custom(name, duration_ms, Rack::MiniProfiler.current.page_struct )
end
else
Rack::MiniProfiler.current.current_timer = current_timer = parent_timer.add_child(name)
begin
self.send without_profiling, *args, &orig
ensure
current_timer.record_time
Rack::MiniProfiler.current.current_timer = parent_timer
end
end
end
klass.send :alias_method, method, with_profiling
end