Sha256: 1c19fd4b87bafe99cf011b5549bb3fe33265a6d20335d32cb2f7ca8e1aaca940

Contents?: true

Size: 1.26 KB

Versions: 13

Compression:

Stored size: 1.26 KB

Contents

module Vedeu

  # Measure the duration. Used for debugging.
  #
  # @api public
  class Timer

    class << self

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

    end # Eigenclass

    # Returns a new instance of Vedeu::Timer.
    #
    # @param message [String]
    # @return [Vedeu::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]
    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 # Vedeu

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
vedeu-0.4.58 lib/vedeu/timer.rb
vedeu-0.4.57 lib/vedeu/timer.rb
vedeu-0.4.56 lib/vedeu/timer.rb
vedeu-0.4.55 lib/vedeu/timer.rb
vedeu-0.4.54 lib/vedeu/support/timer.rb
vedeu-0.4.53 lib/vedeu/support/timer.rb
vedeu-0.4.52 lib/vedeu/support/timer.rb
vedeu-0.4.51 lib/vedeu/support/timer.rb
vedeu-0.4.50 lib/vedeu/support/timer.rb
vedeu-0.4.49 lib/vedeu/support/timer.rb
vedeu-0.4.48 lib/vedeu/support/timer.rb
vedeu-0.4.47 lib/vedeu/support/timer.rb
vedeu-0.4.46 lib/vedeu/support/timer.rb