module TimeBandits

a time consumer implementation for beetle publishing install into application_controller.rb with the line

time_bandit TimeBandits::TimeConsumers::Beetle

this consumer gets installed automatically by the plugin if this were not so

TimeBandit.add TimeBandits::TimeConsumers::Database

would do the job

a time consumer implementation for garbage collection

Constants

VERSION

Public Class Methods

add(bandit) click to toggle source
# File lib/time_bandits.rb, line 25
def self.add(bandit)
  self.time_bandits << bandit unless self.time_bandits.include?(bandit)
end
benchmark(title="Completed in", logger=Rails.logger) { || ... } click to toggle source
# File lib/time_bandits.rb, line 60
def self.benchmark(title="Completed in", logger=Rails.logger)
  reset
  result = nil
  e = nil
  seconds = Benchmark.realtime do
    begin
      result = yield
    rescue Exception => e
      logger.error "Exception: #{e.class}(#{e.message}):\n#{e.backtrace[0..5].join("\n")}"
    end
  end
  consumed # needs to be called for DB time consumer
  rc = e ? "500 Internal Server Error" : "200 OK"
  logger.info "#{title} #{sprintf("%.3f", seconds * 1000)}ms (#{runtime}) | #{rc}"
  raise e if e
  result
end
consumed() click to toggle source
# File lib/time_bandits.rb, line 33
def self.consumed
  time_bandits.map{|b| b.consumed}.sum
end
current_runtime(except = []) click to toggle source
# File lib/time_bandits.rb, line 37
def self.current_runtime(except = [])
  except = Array(except)
  time_bandits.map{|b| except.include?(b) ? 0 : b.current_runtime}.sum
end
metrics() click to toggle source
# File lib/time_bandits.rb, line 50
def self.metrics
  metrics = Hash.new(0)
  time_bandits.each do |bandit|
    bandit.metrics.each do |k,v|
      metrics[k] += v
    end
  end
  metrics
end
reset() click to toggle source
# File lib/time_bandits.rb, line 29
def self.reset
  time_bandits.each{|b| b.reset}
end
runtime() click to toggle source
# File lib/time_bandits.rb, line 46
def self.runtime
  runtimes.join(" | ")
end
runtimes() click to toggle source
# File lib/time_bandits.rb, line 42
def self.runtimes
  time_bandits.map{|b| b.runtime}.reject{|t| t.blank?}
end