Sha256: 8e6c6413bbcd631a0f7404a5fda0020a5a75adf42822c4895e2fdb8c2267e9aa

Contents?: true

Size: 1.47 KB

Versions: 13

Compression:

Stored size: 1.47 KB

Contents

module Vedeu

  module Logging

    # Measure the duration. Used for debugging.
    #
    class Timer

      class << self

        # @example
        #   Vedeu.timer 'message' do
        #     # ... code to be measured
        #   end
        #
        # @param message [String]
        # @param block [Proc]
        # @return [void] The return value of the executed block.
        def timer(message = '', &block)
          new(message).measure(&block)
        end

      end # Eigenclass

      # Returns a new instance of Vedeu::Logging::Timer.
      #
      # @param message [String]
      # @return [Vedeu::Logging::Timer]
      def initialize(message = '')
        @message = message
        @started = Time.now.to_f
      end

      # Write an entry to the log file stating how long a section of
      # code took in milliseconds. Useful for debugging performance.
      #
      # @return [void] The return value of the executed block.
      def measure
        work = yield

        Vedeu.log(type: :timer, message: "#{message} took #{elapsed}ms.")

        work
      end

      protected

      # @!attribute [r] started
      # @return [Time]
      attr_reader :started

      # @!attribute [r] message
      # @return [String]
      attr_reader :message

      # Returns the elapsed time in milliseconds with 3 decimal
      # places.
      #
      # @return [Float]
      def elapsed
        ((Time.now.to_f - started) * 1000).round(3)
      end

    end # Timer

  end # Logging

end # Vedeu

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
vedeu-0.6.20 lib/vedeu/logging/timer.rb
vedeu-0.6.19 lib/vedeu/logging/timer.rb
vedeu-0.6.18 lib/vedeu/logging/timer.rb
vedeu-0.6.17 lib/vedeu/logging/timer.rb
vedeu-0.6.16 lib/vedeu/logging/timer.rb
vedeu-0.6.15 lib/vedeu/logging/timer.rb
vedeu-0.6.14 lib/vedeu/logging/timer.rb
vedeu-0.6.13 lib/vedeu/logging/timer.rb
vedeu-0.6.12 lib/vedeu/logging/timer.rb
vedeu-0.6.11 lib/vedeu/logging/timer.rb
vedeu-0.6.10 lib/vedeu/logging/timer.rb
vedeu-0.6.9 lib/vedeu/logging/timer.rb
vedeu-0.6.8 lib/vedeu/logging/timer.rb