Module: ActionController::Benchmarking::ClassMethods

Defined in:
lib/action_controller/benchmarking.rb

Instance Method Summary collapse

Instance Method Details

#benchmark(title, log_level = Logger::DEBUG, use_silence = true) ⇒ Object

Log and benchmark the workings of a single block and silence whatever logging that may have happened inside it (unless use_silence is set to false).

The benchmark is only recorded if the current level of the logger matches the log_level, which makes it easy to include benchmarking statements in production software that will remain inexpensive because the benchmark will only be conducted if the log level is low enough.


26
27
28
29
30
31
32
33
34
35
# File 'lib/action_controller/benchmarking.rb', line 26

def benchmark(title, log_level = Logger::DEBUG, use_silence = true)
  if logger && logger.level == log_level
    result = nil
    seconds = Benchmark.realtime { result = use_silence ? silence { yield } : yield }
    logger.add(log_level, "#{title} (#{'%.5f' % seconds})")
    result
  else
    yield
  end
end

#silenceObject

Silences the logger for the duration of the block.


38
39
40
41
42
43
# File 'lib/action_controller/benchmarking.rb', line 38

def silence
  old_logger_level, logger.level = logger.level, Logger::ERROR if logger
  yield
ensure
  logger.level = old_logger_level if logger
end