Sha256: 09ff67c35b14bb5151103094a54487826a638494c0072cc527cdf4de86020929

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

# typed: true
module Datadog
  module Utils
    # Common database-related utility functions.
    module Time
      include Kernel # Ensure that kernel methods are always available (https://sorbet.org/docs/error-reference#7003)

      module_function

      # Current monotonic time
      #
      # @param unit [Symbol] unit for the resulting value, same as ::Process#clock_gettime, defaults to :float_second
      # @return [Numeric] timestamp in the requested unit, since some unspecified starting point
      def get_time(unit = :float_second)
        Process.clock_gettime(Process::CLOCK_MONOTONIC, unit)
      end

      # Current wall time.
      #
      # @return [Time] current time object
      def now
        ::Time.now
      end

      # Overrides the implementation of `#now
      # with the provided callable.
      #
      # Overriding the method `#now` instead of
      # indirectly calling `block` removes
      # one level of method call overhead.
      #
      # @param block [Proc] block that returns a `Time` object representing the current wall time
      def now_provider=(block)
        define_singleton_method(:now, &block)
      end

      def measure
        before = get_time
        yield
        after = get_time
        after - before
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ddtrace-0.53.0 lib/ddtrace/utils/time.rb