Skip to content

Instantly share code, notes, and snippets.

@jlecour
Created June 6, 2011 15:41
Show Gist options
  • Save jlecour/1010488 to your computer and use it in GitHub Desktop.
Save jlecour/1010488 to your computer and use it in GitHub Desktop.
require 'my_namespace/timer'
module MyNamespace
class MyService
include MyNamespace::Timer
def service_name
'the_service_name'
end
def my_method(options = {})
result = run_with_timer('my_method') do
# some processing to be benchmarked
end
end
end
end
module MyNamespace
module Timer
def run_with_timer(method = nil, &block)
require "benchmark"
result = nil
time = Benchmark.realtime do
result = yield
end
# bm = Benchmark
# ws = WebServices
# rt = Response times
key = "bm:ws:rt"
key += ":#{self.service_name}"
key += ":#{method}" if method.present?
args = [key, Time.now.utc.to_i, time.round(3)]
begin
$redis.zadd *args
rescue => ex
msg = "#{ex.class.name}: #{ex.message} (#{__FILE__}:#{__LINE__})"
AUDIT_LOG.error msg
end
result
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment