Sha256: d6c17ff097a5bc9f6e52c50750485fe19e5b310628329a4fbffdef0dfc67ac66

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

module Contrast
  module Utils
    # Timer is class that can track state about when an event starts and how long it takes
    # Also containes utility methods to get time values in milliseconds
    class Timer
      attr_reader :start_at, :start_ms, :events

      def initialize time = Time.now
        @start_at = time
        @start_ms = (@start_at.to_f * 1000).to_i
        @events = {}
      end

      def elapsed label
        before = Time.now
        result = yield if block_given?
        events[label.to_s] = ((Time.now - before) * 1000).to_i
        result
      end

      def to_s
        pairs = events.to_a.map { |pair| "#{ pair[0] }=#{ pair[1] }ms" }
        start_at.strftime('%Y-%m-%d %H:%M:%S.%L') + pairs.join(Contrast::Utils::ObjectShare::SPACE)
      end

      def now_ms
        (Time.now.to_f * 1000).to_i
      end

      def self.now_ms
        (Time.now.to_f * 1000).to_i
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
contrast-agent-3.12.2 lib/contrast/utils/timer.rb
contrast-agent-3.12.1 lib/contrast/utils/timer.rb
contrast-agent-3.12.0 lib/contrast/utils/timer.rb
contrast-agent-3.11.0 lib/contrast/utils/timer.rb