Sha256: 206d0e20b52785e2041d428eee8d166be5564467a160330f8c433f707dc97141

Contents?: true

Size: 779 Bytes

Versions: 1

Compression:

Stored size: 779 Bytes

Contents

# frozen_string_literal: true

module DDTelemetry
  class Stopwatch
    attr_reader :duration

    class AlreadyRunningError < StandardError
      def message
        'Cannot start, because stopwatch is already running'
      end
    end

    class NotRunningError < StandardError
      def message
        'Cannot stop, because stopwatch is not running'
      end
    end

    def initialize
      @duration = 0.0
      @last_start = nil
    end

    def start
      raise AlreadyRunningError if running?
      @last_start = Time.now
    end

    def stop
      raise NotRunningError unless running?
      @duration += (Time.now - @last_start)
      @last_start = nil
    end

    def running?
      !@last_start.nil?
    end

    def stopped?
      !running?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ddtelemetry-1.0.0a1 lib/ddtelemetry/stopwatch.rb