Last active
March 16, 2017 22:15
-
-
Save jcmuller/a84eda68119983fc5aaf7a9838f63062 to your computer and use it in GitHub Desktop.
Someone today asked for a way to aggregate timings of operations in different places across an app. This is a single threaded solution.
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
class Timer | |
include Singleton | |
def initialize | |
Thread.current[:timers] ||= Hash.new(0) | |
end | |
def display(timer) | |
Thread.current[:timers][timer] | |
end | |
def reset(timer) | |
Thread.current[:timers][timer] = 0 | |
end | |
def time(timer, &block) | |
start = Time.zone.now | |
block.call | |
time_end = Time.zone.now | |
Thread.current[:timers][timer] += time_end - start | |
end | |
end | |
def time(timer, &block) | |
Timer.instance.time(timer, &block) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment