Class: LitmusPaper::Metric::CPULoad

Inherits:
Object
  • Object
show all
Defined in:
lib/litmus_paper/metric/cpu_load.rb

Instance Method Summary collapse

Constructor Details

#initialize(weight) ⇒ CPULoad

Returns a new instance of CPULoad.



4
5
6
# File 'lib/litmus_paper/metric/cpu_load.rb', line 4

def initialize(weight)
  @weight = weight
end

Instance Method Details

#current_healthObject



8
9
10
# File 'lib/litmus_paper/metric/cpu_load.rb', line 8

def current_health
  [@weight - (@weight * load_average / processor_count), 1].max
end

#load_averageObject



23
24
25
# File 'lib/litmus_paper/metric/cpu_load.rb', line 23

def load_average
  File.read('/proc/loadavg').split(' ').first.to_f
end

#processor_countObject



12
13
14
15
16
17
18
19
20
21
# File 'lib/litmus_paper/metric/cpu_load.rb', line 12

def processor_count
  @processor_count ||= File.readlines('/proc/cpuinfo').reduce(0) do |memo, line|
    if line =~ /^processor/
      memo + 1
    else
      memo
    end
  end

end

#statsObject



27
28
29
30
31
# File 'lib/litmus_paper/metric/cpu_load.rb', line 27

def stats
  {
    :cpu_load_average => load_average,
  }
end

#to_sObject



33
34
35
# File 'lib/litmus_paper/metric/cpu_load.rb', line 33

def to_s
  "Metric::CPULoad(#{@weight})"
end