Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/actionpack/lib/action_view/helpers/benchmark_helper.rb

Revision 7106, 1.2 kB (checked in by david, 1 year ago)

Massive documentation update for all helpers (closes #8223, #8177, #8175, #8108, #7977, #7972, #7971, #7969) [jeremymcanally]

Line 
1 require 'benchmark'
2
3 module ActionView
4   module Helpers
5     # This helper offers a method to measure the execution time of a block
6     # in a template.
7     module BenchmarkHelper
8       # Allows you to measure the execution time of a block
9       # in a template and records the result to the log. Wrap this block around
10       # expensive operations or possible bottlenecks to get a time reading
11       # for the operation.  For example, let's say you thought your file
12       # processing method was taking too long; you could wrap it in a benchmark block.
13       #
14       #  <% benchmark "Process data files" do %>
15       #    <%= expensive_files_operation %>
16       #  <% end %>
17       #
18       # That would add something like "Process data files (0.34523)" to the log,
19       # which you can then use to compare timings when optimizing your code.
20       #
21       # You may give an optional logger level as the second argument
22       # (:debug, :info, :warn, :error); the default value is :info.
23       def benchmark(message = "Benchmarking", level = :info)
24         if @logger
25           real = Benchmark.realtime { yield }
26           @logger.send level, "#{message} (#{'%.5f' % real})"
27         end
28       end
29     end
30   end
31 end
Note: See TracBrowser for help on using the browser.