2009-05-18 19:15:43 -04:00
|
|
|
module AbstractController
|
|
|
|
module Benchmarker
|
2009-05-28 12:35:36 -04:00
|
|
|
extend ActiveSupport::Concern
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-05-29 18:03:23 -04:00
|
|
|
include Logger
|
2009-05-28 10:49:02 -04:00
|
|
|
|
2009-05-18 19:15:43 -04:00
|
|
|
module ClassMethods
|
2009-06-08 19:14:38 -04:00
|
|
|
# Execute the passed in block, timing the duration of the block in ms.
|
|
|
|
#
|
|
|
|
# ==== Parameters
|
|
|
|
# title<#to_s>:: The title of block to benchmark
|
|
|
|
# log_level<Integer>:: A valid log level. Defaults to Logger::DEBUG
|
|
|
|
# use_silence<TrueClass, FalseClass>:: Whether or not to silence the
|
|
|
|
# logger for the duration of the block.
|
|
|
|
#
|
|
|
|
# ==== Returns
|
|
|
|
# Object:: The result of the block
|
2009-05-18 19:15:43 -04:00
|
|
|
def benchmark(title, log_level = ::Logger::DEBUG, use_silence = true)
|
|
|
|
if logger && logger.level >= log_level
|
|
|
|
result = nil
|
|
|
|
ms = Benchmark.ms { result = use_silence ? silence { yield } : yield }
|
|
|
|
logger.add(log_level, "#{title} (#{('%.1f' % ms)}ms)")
|
|
|
|
result
|
|
|
|
else
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Silences the logger for the duration of the block.
|
|
|
|
def silence
|
|
|
|
old_logger_level, logger.level = logger.level, ::Logger::ERROR if logger
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
logger.level = old_logger_level if logger
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-05-28 10:49:02 -04:00
|
|
|
end
|