mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
c9901176be
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1719 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
24 lines
749 B
Ruby
24 lines
749 B
Ruby
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 %>
|
|
#
|
|
# 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})"
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|