Created
June 6, 2011 15:41
-
-
Save jlecour/1010488 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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