class Async::Clock
@public Since *Async v1*.
A convenient wrapper around the internal monotonic clock.
def self.measure
@yields {...} The block to execute.
Measure the execution of a block of code.
def self.measure start_time = self.now yield return self.now - start_time end
def self.now
def self.now ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) end
def self.start
Start measuring elapsed time from now.
def self.start self.new.tap(&:start!) end
def as_json(...)
Convert the clock to a JSON-compatible hash.
def as_json(...) { started: self.started, total: self.total, } end
def initialize(total = 0)
Create a new clock with the initial total time.
def initialize(total = 0) @total = total @started = nil end
def reset!
def reset! @total = 0 if @started @started = Clock.now end end
def start!
def start! @started ||= Clock.now end
def stop!
def stop! if @started @total += (Clock.now - @started) @started = nil end return @total end
def to_json(...)
Convert the clock to a JSON string.
def to_json(...) self.as_json.to_json(...) end
def total
def total total = @total if @started total += (Clock.now - @started) end return total end