Sha256: 98ecfe1e3e7d584ff975c861158b45e57c8c723f9bc7c79cbc28a9386674d32a

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

module Datadog
  module Utils
    # Common database-related utility functions.
    module Time
      PROCESS_TIME_SUPPORTED = Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.1.0')

      module_function

      # Current monotonic time.
      # Falls back to `now` if monotonic clock
      # is not available.
      #
      # @return [Float] in seconds, since some unspecified starting point
      def get_time
        PROCESS_TIME_SUPPORTED ? Process.clock_gettime(Process::CLOCK_MONOTONIC) : now.to_f
      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

3 entries across 3 versions & 1 rubygems

Version Path
ddtrace-0.49.0 lib/ddtrace/utils/time.rb
ddtrace-0.48.0 lib/ddtrace/utils/time.rb
ddtrace-0.47.0 lib/ddtrace/utils/time.rb