2005-04-27 04:17:25 -04:00
|
|
|
require 'benchmark'
|
|
|
|
|
|
|
|
module ActionView
|
|
|
|
module Helpers
|
|
|
|
module BenchmarkHelper
|
|
|
|
# Measures the execution time of a block in a template and reports the result to the log. Example:
|
|
|
|
#
|
|
|
|
# <% benchmark "Notes section" do %>
|
|
|
|
# <%= expensive_notes_operation %>
|
|
|
|
# <% end %>
|
|
|
|
#
|
2005-07-05 15:33:25 -04:00
|
|
|
# Will add something like "Notes section (0.34523)" to the log.
|
|
|
|
#
|
|
|
|
# You may give an optional logger level as the second argument
|
|
|
|
# (:debug, :info, :warn, :error). The default is :info.
|
|
|
|
def benchmark(message = "Benchmarking", level = :info)
|
|
|
|
if @logger
|
|
|
|
real = Benchmark.realtime { yield }
|
|
|
|
@logger.send level, "#{message} (#{'%.5f' % real})"
|
2005-04-27 04:17:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2005-07-05 15:33:25 -04:00
|
|
|
end
|