Sha256: 926f5202b5a02bda37abdccaae497842d57ab9cc727a5d378ffe11cc81ab5d9f

Contents?: true

Size: 1.64 KB

Versions: 3

Compression:

Stored size: 1.64 KB

Contents

module Barnes
  module Instruments
    class Stopwatch
      def initialize(timepiece = Timepiece)
        @timepiece = timepiece
        @has_cpu_time = timepiece.respond_to?(:cpu)
      end

      def start!(state)
        state[:stopwatch] = current
      end

      def instrument!(state, counters, gauges)
        last = state[:stopwatch]
        wall_elapsed = @timepiece.wall - last[:wall]
        counters[:'Time.wall'] = wall_elapsed

        if @has_cpu_time
          cpu_elapsed = @timepiece.cpu - last[:cpu]
          idle_elapsed = wall_elapsed - cpu_elapsed

          counters[:'Time.cpu']      = cpu_elapsed
          counters[:'Time.idle']     = idle_elapsed

          if wall_elapsed == 0
            counters[:'Time.pct.cpu']  = 0
            counters[:'Time.pct.idle'] = 0
          else
            counters[:'Time.pct.cpu']  = 100.0 * cpu_elapsed  / wall_elapsed
            counters[:'Time.pct.idle'] = 100.0 * idle_elapsed / wall_elapsed
          end
        end

        state[:stopwatch] = current
      end

      private def current
        state = {
          :wall => @timepiece.wall,
        }
        state[:cpu]  = @timepiece.cpu if @has_cpu_time
        state
      end
    end

    module Timepiece
      def self.wall
        ::Time.now.to_f * 1000
      end

      # Ruby 2.1+
      if Process.respond_to?(:clock_gettime)
        def self.cpu
          Process.clock_gettime Process::CLOCK_PROCESS_CPUTIME_ID, :float_millisecond
        end

      # ruby-prof installed
      elsif defined? RubyProf::Measure::ProcessTime
        def self.cpu
          RubyProf::Measure::Process.measure * 1000
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
barnes-0.0.3 lib/barnes/instruments/stopwatch.rb
barnes-0.0.2 /Users/schneems/Documents/projects/barnes/lib/barnes/instruments/stopwatch.rb
barnes-0.0.1 /Users/schneems/Documents/projects/barnes/lib/barnes/instruments/stopwatch.rb