Functional tests now work properly, bearing in mind whether a user is logged in or...
[depot.git] / vendor / rails / actionpack / lib / action_view / helpers / benchmark_helper.rb
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 (345.2ms)" 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 controller.logger
25 seconds = Benchmark.realtime { yield }
26 controller.logger.send(level, "#{message} (#{'%.1f' % (seconds * 1000)}ms)")
27 else
28 yield
29 end
30 end
31 end
32 end
33 end