Class: Rfc::CpuTimeAverager
- Inherits:
-
Object
- Object
- Rfc::CpuTimeAverager
- Defined in:
- lib/rfc/cpu_time_averager.rb
Defined Under Namespace
Classes: Sample
Instance Method Summary collapse
- #enough? ⇒ Boolean
- #first ⇒ Object
-
#initialize ⇒ CpuTimeAverager
constructor
A new instance of CpuTimeAverager.
- #last ⇒ Object
- #sample ⇒ Object
Constructor Details
#initialize ⇒ CpuTimeAverager
Returns a new instance of CpuTimeAverager.
8 9 10 |
# File 'lib/rfc/cpu_time_averager.rb', line 8 def initialize @samples = [] end |
Instance Method Details
#enough? ⇒ Boolean
32 33 34 |
# File 'lib/rfc/cpu_time_averager.rb', line 32 def enough? @samples.last.first - @samples.first.first >= 2 end |
#first ⇒ Object
36 37 38 |
# File 'lib/rfc/cpu_time_averager.rb', line 36 def first @samples.first end |
#last ⇒ Object
40 41 42 |
# File 'lib/rfc/cpu_time_averager.rb', line 40 def last @samples.last end |
#sample ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rfc/cpu_time_averager.rb', line 12 def sample # https://www.linuxhowtos.org/manpages/5/proc.htm IO.readlines('/proc/stat').each do |line| if line =~ /^cpu/ _, user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice = line.split.map(&:to_i) @samples << [Time.now, Sample.new( user, nice, system, idle, iowait, irq, softirq, steal, guest, guest_nice, )] break end end threshold = Time.now - 10 while @samples.length > 2 && @samples.first.first < threshold @samples.shift end end |