benchmark_helper.rb 636 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
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.345234)" to the log.
      def benchmark(message = "Benchmarking", &block)
14 15
        return if @logger.nil?

16 17 18 19
        bm = Benchmark.measure do
          block.call
        end
        
D
David Heinemeier Hansson 已提交
20
        @logger.info("#{message} (#{sprintf("%.5f", bm.real)})")
21 22 23 24
      end
    end
  end
end