duffy 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/duffy/system.rb +30 -1
- data/lib/duffy/version.rb +2 -1
- data/spec/system_spec.rb +11 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0cb33775dd53e24679b81e54fe707de632936b44
|
4
|
+
data.tar.gz: 90eb2a68611c1899e356c5440c330a6b76ade66a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d8c82d33b55368e2b18af279beef4d1398e91987748e2450994b6acdf240b097a74a95f6019074a4397d19286c7fef129d969b87f4ee4e136e7939e788583710
|
7
|
+
data.tar.gz: 3d47ea83a1167bfa885ed6c0621fae1066f7e47d42ec3d69e8ab5174653e3c61a053898bb9f7f49901209e1e40ddf11e7ccbceaa4c00f880a2faf5723419d1be
|
data/README.md
CHANGED
data/lib/duffy/system.rb
CHANGED
@@ -40,11 +40,40 @@ module Duffy
|
|
40
40
|
end
|
41
41
|
|
42
42
|
# What is a sane number of threads to use for data processing.
|
43
|
-
# Only using
|
43
|
+
# Only using physical cores is a waste, using all logical cores decreases performance.
|
44
44
|
def sane_load
|
45
45
|
(cores + threads) / 2
|
46
46
|
end
|
47
47
|
|
48
|
+
# The system's current CPU utilization.
|
49
|
+
# Darwin: Get a list of all processes' CPU percentage and add them up. Accurate to a couple percent vs. Activity Monitor.
|
50
|
+
# Linux: Read /proc/stat twice and take the difference to give cpu time used in that interval.
|
51
|
+
def cpu_percent
|
52
|
+
case RUBY_PLATFORM
|
53
|
+
when /darwin/ then (`ps -A -o %cpu`.lines.map(&:to_f).inject(:+) rescue 0) / threads
|
54
|
+
when /linux/ then proc_diff rescue 0
|
55
|
+
else 0
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
# [CPU USE, CPU IDLE]
|
63
|
+
def proc_stat
|
64
|
+
cpu = File.open("/proc/stat", "r").read.lines.first
|
65
|
+
[cpu.split[1 .. 3].map(&:to_i).inject(:+), cpu.split[1 .. 4].map(&:to_i).inject(:+)]
|
66
|
+
end
|
67
|
+
|
68
|
+
# Poll proc_stat twice and find the usage in that time.
|
69
|
+
# Higher Sleep is more accurate, but obviously slower.
|
70
|
+
def proc_diff
|
71
|
+
s = proc_stat
|
72
|
+
sleep 0.2
|
73
|
+
f = proc_stat
|
74
|
+
(f[0] - s[0]) * 100.0 / (f[1] - s[1])
|
75
|
+
end
|
76
|
+
|
48
77
|
end
|
49
78
|
end
|
50
79
|
end
|
data/lib/duffy/version.rb
CHANGED
data/spec/system_spec.rb
CHANGED
@@ -20,4 +20,15 @@ describe Duffy::System do
|
|
20
20
|
Duffy::System.threads.should be_an(Integer)
|
21
21
|
end
|
22
22
|
end
|
23
|
+
|
24
|
+
describe "cpu_percent" do
|
25
|
+
it "returns the current cpu load percentage" do
|
26
|
+
Duffy::System.cpu_percent.should be_a(Float)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
|
23
34
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: duffy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jacob Duffy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|