Sha256: 31711837cc1bc942651b6cf15785df6a2e9f78e20242a5b5b42a646f0a66292d

Contents?: true

Size: 788 Bytes

Versions: 1

Compression:

Stored size: 788 Bytes

Contents

# frozen_string_literal: true

require 'time'

module SiteHealth
  class Timer
    attr_reader :started_at, :finished_at

    def self.start
      new.tap(&:start)
    end

    def self.measure(&block)
      new.tap { |timer| timer.measure(&block) }
    end

    def measure
      start
      yield
      finish
      self
    end

    def start
      @started = high_precision_time
      @started_at = Time.now
    end

    def finish
      @finished = high_precision_time
      @finished_at = Time.now
    end

    def diff
      fail(StandardError, 'timer must be started') unless @started

      finish = @finished || high_precision_time
      finish - @started
    end

    private

    def high_precision_time
      Process.clock_gettime(Process::CLOCK_MONOTONIC)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
site_health-0.2.0 lib/site_health/timer.rb