5 # This helper offers a method to measure the execution time of a block
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.
14 # <% benchmark "Process data files" do %>
15 # <%= expensive_files_operation %>
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.
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)
25 seconds
= Benchmark
.realtime
{ yield }
26 controller
.logger
.send(level
, "#{message} (#{'%.1f' % (seconds * 1000)}ms)")