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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 304f86f07f2a4318944a13f241921faba0d8f969
4
- data.tar.gz: d90859be246a584b6cd460c8a8d942a1de39c07d
3
+ metadata.gz: 0cb33775dd53e24679b81e54fe707de632936b44
4
+ data.tar.gz: 90eb2a68611c1899e356c5440c330a6b76ade66a
5
5
  SHA512:
6
- metadata.gz: 6ff94dbc71c7d44f306a1698173ac7a05763a16b3c44d0090b182561d57eb565edd935431b36a70deec574743a7c1fa92054ac1cd59208607f4f37819366c565
7
- data.tar.gz: 66637e5c757d8e36a12911abb8395215d3d7119118fb6e703a6871c7c7bc1bc36761c088218813a831988bc2b629023d73c20fd36e58c112825009a264e6355e
6
+ metadata.gz: d8c82d33b55368e2b18af279beef4d1398e91987748e2450994b6acdf240b097a74a95f6019074a4397d19286c7fef129d969b87f4ee4e136e7939e788583710
7
+ data.tar.gz: 3d47ea83a1167bfa885ed6c0621fae1066f7e47d42ec3d69e8ab5174653e3c61a053898bb9f7f49901209e1e40ddf11e7ccbceaa4c00f880a2faf5723419d1be
data/README.md CHANGED
@@ -81,7 +81,7 @@ Duffy::System.cpus | 1
81
81
  Duffy::System.cores | 2
82
82
  Duffy::System.threads | 4
83
83
  Duffy::System.sane_load | 3
84
-
84
+ Duffy::System.cpu_percent| 16%
85
85
 
86
86
 
87
87
  ## View Helpers:
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 true cores is a waste, using all pseudo cores decreases performance.
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
@@ -1,7 +1,8 @@
1
1
  module Duffy
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
4
4
 
5
5
  # History
6
+ # 0.2.6 - Added cpu_percent to give the current total CPU usage percentage.
6
7
  # 0.2.5 - Added beast_mode helper to render partials in parallel
7
8
  # 0.2.4 - Added NilClass.to_date => nil
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.5
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-01-12 00:00:00.000000000 Z
11
+ date: 2016-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport