Sha256: dcd319156d409641150d9989045ddf4844a631c13addcc6273922907062f14bc

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module CPU
  class Usage < Struct.new('Usage', :usage, :processor_id, :user, :nice,
                           :system, :idle, :start_at, :stop_at)
    include Shared

    def +(other)
      self.class.new(*(
        [
          usage,
          processor_id == other.processor_id ? processor_id : 0,
        ] +
        values_at(2..-3).zip(other.values_at(2..-3)).map { |x, y| x + y } +
        [
          [ start_at, other.start_at ].min,
          [ stop_at, other.stop_at ].max
        ])
      )
    end

    def -(other)
      self + other * -1
    end

    def *(scalar)
      scalar = scalar.to_f
      self.class.new(*(
        [
          usage,
          processor_id,
        ] +
        values_at(2..-3).map { |x| x * scalar } +
        [
          start_at,
          stop_at,
        ])
      )
    end

    def /(scalar)
      self * (1.0 / scalar)
    end

    def process_time
      user + nice + system
    end

    def real_time
      stop_at - start_at
    end

    def total_time
      values_at(2..-3).inject(0.0) { |s,  x| s + x }
    end

    def percentage(time = total_time)
      100.0 * process_time / time
    end

    def inspect
      "#<#{self.class}: #{percentage}>"
    end

    alias to_s inspect
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cpu-0.0.0 lib/cpu/usage.rb