1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
class Statsd
# = MonotonicTime: a helper for getting monotonic time
#
# @example
# MonotonicTime.time_in_ms #=> 287138801.144576
# MonotonicTime guarantees that the time is strictly linearly
# increasing (unlike realtime).
# @see http://pubs.opengroup.org/onlinepubs/9699919799/functions/clock_getres.html
module MonotonicTime
class << self
# @return [Integer] current monotonic time in milliseconds
def time_in_ms
time_in_nanoseconds / (10.0 ** 6)
end
private
if defined?(Process::CLOCK_MONOTONIC)
def time_in_nanoseconds
Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
end
elsif RUBY_ENGINE == 'jruby'
def time_in_nanoseconds
java.lang.System.nanoTime
end
else
def time_in_nanoseconds
t = Time.now
t.to_i * (10 ** 9) + t.nsec
end
end
end
end
end
|