sys-cpu 0.8.1 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
1
  require 'rake'
2
2
  require 'rake/clean'
3
- require 'rake/testtask'
4
3
  require 'rbconfig'
4
+ require 'rspec/core/rake_task'
5
5
  include RbConfig
6
6
 
7
- CLEAN.include('**/*.gem', '**/*.rbc', '**/*.rbx')
7
+ CLEAN.include('**/*.gem', '**/*.rbc', '**/*.rbx', '**/*.lock')
8
8
 
9
9
  namespace 'gem' do
10
10
  desc "Create the sys-cpu gem"
@@ -12,7 +12,7 @@ namespace 'gem' do
12
12
  require 'rubygems/package'
13
13
  spec = eval(IO.read('sys-cpu.gemspec'))
14
14
  spec.signing_key = File.join(Dir.home, '.ssh', 'gem-private_key.pem')
15
- Gem::Package.build(spec, true)
15
+ Gem::Package.build(spec)
16
16
  end
17
17
 
18
18
  desc "Install the sys-cpu gem"
@@ -44,9 +44,7 @@ task :example => [:clean] do
44
44
 
45
45
  end
46
46
 
47
- Rake::TestTask.new do |t|
48
- t.libs << 'test'
49
- t.test_files = FileList['test/test_sys_cpu.rb']
50
- end
47
+ desc "Run the test suite"
48
+ RSpec::Core::RakeTask.new(:spec)
51
49
 
52
- task :default => :test
50
+ task :default => :spec
@@ -5,7 +5,7 @@ require 'rbconfig'
5
5
  module Sys
6
6
  class CPU
7
7
  # The version of the sys-cpu gem.
8
- VERSION = '0.8.1'.freeze
8
+ VERSION = '1.0.1'.freeze
9
9
  end
10
10
  end
11
11
 
@@ -70,11 +70,43 @@ module Sys
70
70
  array unless block_given?
71
71
  end
72
72
 
73
+ # Return the total number of logical CPU on the system.
74
+ #
75
+ def self.num_cpu
76
+ CPU_ARRAY.size
77
+ end
78
+
79
+ # Return the architecture of the CPU.
80
+ #
81
+ def self.architecture
82
+ case CPU_ARRAY.first['cpu_family']
83
+ when '3'
84
+ "x86"
85
+ when '6'
86
+ "x86_64"
87
+ else
88
+ nil
89
+ end
90
+ end
91
+
92
+ # Returns a string indicating the CPU model.
93
+ #
94
+ def self.model
95
+ CPU_ARRAY.first['model_name']
96
+ end
97
+
98
+ # Returns an integer indicating the speed of the CPU.
99
+ #
100
+ def self.freq
101
+ CPU_ARRAY.first['cpu_mhz'].to_f.round
102
+ end
103
+
73
104
  private
74
105
 
75
106
  # Create singleton methods for each of the attributes.
76
107
  #
77
108
  def self.method_missing(id, arg=0)
109
+ raise NoMethodError, "'#{id}'" unless CPU_ARRAY[arg].has_key?(id.to_s)
78
110
  rv = CPU_ARRAY[arg][id.to_s]
79
111
  if rv.nil?
80
112
  id = id.to_s + "?"
@@ -278,7 +278,7 @@ module Sys
278
278
  # Note that this value returns nil on my system.
279
279
  #
280
280
  def self.get_cmec(num)
281
- case
281
+ case num
282
282
  when 0
283
283
  str = "The device is working properly."
284
284
  return str
@@ -0,0 +1,9 @@
1
+ require 'rspec'
2
+
3
+ RSpec.configure do |config|
4
+ config.filter_run_excluding(:bsd) if RbConfig::CONFIG['host_os'] !~ /bsd|darwin|mach|osx/i
5
+ config.filter_run_excluding(:sunos) if RbConfig::CONFIG['host_os'] !~ /sunos|solaris/i
6
+ config.filter_run_excluding(:windows) if RbConfig::CONFIG['host_os'] !~ /mswin|win32|dos|mingw|cygwin/i
7
+ config.filter_run_excluding(:hpux) if RbConfig::CONFIG['host_os'] !~ /hpux/i
8
+ config.filter_run_excluding(:linux) if RbConfig::CONFIG['host_os'] !~ /linux/i
9
+ end
@@ -0,0 +1,94 @@
1
+ #############################################################
2
+ # test_sys_cpu_bsd.rb
3
+ #
4
+ # The test case for sys-cpu on BSD flavors, including OS X.
5
+ #############################################################
6
+ require 'sys/cpu'
7
+ require 'spec_helper'
8
+
9
+ RSpec.describe Sys::CPU, :bsd => true do
10
+ example "architecture method basic functionality" do
11
+ expect(Sys::CPU).to respond_to(:architecture)
12
+ expect{ Sys::CPU.architecture }.not_to raise_error
13
+ end
14
+
15
+ example "architecture method returns a sane value" do
16
+ expect(Sys::CPU.architecture).to be_kind_of(String)
17
+ expect(Sys::CPU.architecture.size).to be > 0
18
+ end
19
+
20
+ example "architecture method does not accept any arguments" do
21
+ expect{ Sys::CPU.architecture(0) }.to raise_error(ArgumentError)
22
+ end
23
+
24
+ example "freq method basic functionality" do
25
+ expect(Sys::CPU).to respond_to(:freq)
26
+ expect{ Sys::CPU.freq }.not_to raise_error
27
+ end
28
+
29
+ example "freq method returns expected value" do
30
+ expect(Sys::CPU.freq).to be_kind_of(Integer)
31
+ expect(Sys::CPU.freq).to be > 0
32
+ end
33
+
34
+ example "freq method does not accept any arguments" do
35
+ expect{ Sys::CPU.freq(0) }.to raise_error(ArgumentError)
36
+ end
37
+
38
+ example "load_avg method basic functionality" do
39
+ expect(Sys::CPU).to respond_to(:load_avg)
40
+ expect{ Sys::CPU.load_avg }.not_to raise_error
41
+ end
42
+
43
+ example "load_avg returns the expected results" do
44
+ expect(Sys::CPU.load_avg).to be_kind_of(Array)
45
+ expect(Sys::CPU.load_avg.length).to eq(3)
46
+ expect(Sys::CPU.load_avg[0]).to be_kind_of(Float)
47
+ end
48
+
49
+ example "load_avg does not accept any arguments" do
50
+ expect{ Sys::CPU.load_avg(0) }.to raise_error(ArgumentError)
51
+ end
52
+
53
+ example "machine method basic functionality" do
54
+ expect(Sys::CPU).to respond_to(:machine)
55
+ expect{ Sys::CPU.machine }.not_to raise_error
56
+ end
57
+
58
+ example "machine method returns sane value" do
59
+ expect(Sys::CPU.machine).to be_kind_of(String)
60
+ expect(Sys::CPU.machine.size).to be > 0
61
+ end
62
+
63
+ example "machine method does not accept any arguments" do
64
+ expect{ Sys::CPU.machine(0) }.to raise_error(ArgumentError)
65
+ end
66
+
67
+ example "model method basic functionality" do
68
+ expect(Sys::CPU).to respond_to(:model)
69
+ expect{ Sys::CPU.model }.not_to raise_error
70
+ end
71
+
72
+ example "model method returns sane value" do
73
+ expect(Sys::CPU.model).to be_kind_of(String)
74
+ expect(Sys::CPU.model.length).to be > 0
75
+ end
76
+
77
+ example "model method does not accept any arguments" do
78
+ expect{ Sys::CPU.model(0) }.to raise_error(ArgumentError)
79
+ end
80
+
81
+ example "num_cpu method basic functionality" do
82
+ expect(Sys::CPU).to respond_to(:num_cpu)
83
+ expect{ Sys::CPU.num_cpu }.not_to raise_error
84
+ end
85
+
86
+ example "num_cpu method returns expected value" do
87
+ expect(Sys::CPU.num_cpu).to be_kind_of(Integer)
88
+ expect(Sys::CPU.num_cpu).to be > 0
89
+ end
90
+
91
+ example "num_cpu method does not accept any arguments" do
92
+ expect{ Sys::CPU.num_cpu(0) }.to raise_error(ArgumentError)
93
+ end
94
+ end
@@ -0,0 +1,50 @@
1
+ #####################################################################
2
+ # sys_cpu_hpux_spec.rb
3
+ #
4
+ # Test suite for the HP-UX platform. This should be run via the
5
+ # 'rake test' task.
6
+ #####################################################################
7
+ require 'sys/cpu'
8
+ require 'spec_helper'
9
+
10
+ RSpec.describe Sys::CPU, :hpux => true do
11
+ example "cpu_freq" do
12
+ expect(Sys::CPU).to respond_to(:freq)
13
+ expect{ Sys::CPU.freq }.not_to raise_error
14
+ expect{ Sys::CPU.freq(0) }.not_to raise_error
15
+ expect(Sys::CPU.freq).to be_kind_of(Integer)
16
+ end
17
+
18
+ example "num_cpu" do
19
+ expect(Sys::CPU).to respond_to(:num_cpu)
20
+ expect{ Sys::CPU.num_cpu }.not_to raise_error
21
+ expect(Sys::CPU.num_cpu).to be_kind_of(Integer)
22
+ end
23
+
24
+ example "num_active_cpu" do
25
+ expect(Sys::CPU).to respond_to(:num_active_cpu)
26
+ expect{ Sys::CPU.num_active_cpu }.not_to raise_error
27
+ expect(Sys::CPU.num_active_cpu).to be_kind_of(Integer)
28
+ end
29
+
30
+ example "cpu_architecture" do
31
+ expect(Sys::CPU).to respond_to(:architecture)
32
+ expect{ Sys::CPU.architecture }.not_to raise_error
33
+ expect(Sys::CPU.architecture).to be_kind_of(String)
34
+ end
35
+
36
+ example "load_avg sanity check" do
37
+ expect(Sys::CPU).to respond_to(:load_avg)
38
+ expect{ Sys::CPU.load_avg }.not_to raise_error
39
+ expect{ Sys::CPU.load_avg(0) }.not_to raise_error
40
+ expect{ Sys::CPU.load_avg{ |e| }.not_to raise_error }
41
+ expect{ Sys::CPU.load_avg(0){ }.to raise_error(ArgumentError) }
42
+ end
43
+
44
+ example "load_avg expected results" do
45
+ expect(Sys::CPU.load_avg).to be_kind_of(Array)
46
+ expect(Sys::CPU.load_avg(0)).to be_kind_of(Array)
47
+ expect(Sys::CPU.load_avg.length).to eq(3)
48
+ expect(Sys::CPU.load_avg(0).length).to eq(3)
49
+ end
50
+ end
@@ -0,0 +1,53 @@
1
+ ###########################################################
2
+ # sys_cpu_linux_spec.rb
3
+ #
4
+ # Specs for sys-cpu for Linux. This should be run via
5
+ # the 'rake spec' task.
6
+ ###########################################################
7
+ require 'sys/cpu'
8
+ require 'spec_helper'
9
+
10
+ RSpec.describe Sys::CPU, :linux => true do
11
+ example "dynamic methods are defined as expected" do
12
+ expect{
13
+ Sys::CPU.processors{ |cs|
14
+ cs.members.each{ |m| cs[m].to_s }
15
+ }
16
+ }.not_to raise_error
17
+ end
18
+
19
+ example "load average works as expected" do
20
+ expect{ Sys::CPU.load_avg }.not_to raise_error
21
+ expect(Sys::CPU.load_avg.length).to eq(3)
22
+ end
23
+
24
+ example "cpu_stats works as expected" do
25
+ expect{ Sys::CPU.cpu_stats }.not_to raise_error
26
+ expect(Sys::CPU.cpu_stats).to be_kind_of(Hash)
27
+ expect(Sys::CPU.cpu_stats['cpu0'].length).to be >= 4
28
+ end
29
+
30
+ example "architecture works as expected" do
31
+ expect{ Sys::CPU.architecture }.not_to raise_error
32
+ expect(Sys::CPU.architecture).to be_kind_of(String)
33
+ end
34
+
35
+ example "model works as expected" do
36
+ expect{ Sys::CPU.model }.not_to raise_error
37
+ expect(Sys::CPU.model).to be_kind_of(String)
38
+ end
39
+
40
+ example "freq works as expected" do
41
+ expect{ Sys::CPU.freq }.not_to raise_error
42
+ expect(Sys::CPU.freq).to be_kind_of(Numeric)
43
+ end
44
+
45
+ example "num_cpu works as expected" do
46
+ expect{ Sys::CPU.num_cpu }.not_to raise_error
47
+ expect(Sys::CPU.num_cpu).to be_kind_of(Numeric)
48
+ end
49
+
50
+ example "bogus methods are not picked up by method_missing" do
51
+ expect{Sys::CPU.bogus }.to raise_error(NoMethodError)
52
+ end
53
+ end
@@ -0,0 +1,18 @@
1
+ #######################################################################
2
+ # sys_cpu_spec.rb
3
+ #
4
+ # Only shared specs go here. Everything else goes into its own tagged
5
+ # spec file.
6
+ #######################################################################
7
+ require 'sys/cpu'
8
+ require 'rspec'
9
+
10
+ RSpec.describe Sys::CPU::VERSION do
11
+ example "version number is set to the expected value" do
12
+ expect(Sys::CPU::VERSION).to eq('1.0.1')
13
+ end
14
+
15
+ example "version number is frozen" do
16
+ expect(Sys::CPU::VERSION).to be_frozen
17
+ end
18
+ end
@@ -0,0 +1,80 @@
1
+ ###########################################################
2
+ # sys_cpu_sunos_spec.rb
3
+ #
4
+ # Test suite for sys-cpu on Solaris. This should be run
5
+ # via the 'rake spec' task.
6
+ ###########################################################
7
+ require 'sys/cpu'
8
+ require 'spec_helper'
9
+
10
+ RSpec.describe Sys::CPU, :sunos => true do
11
+ example "freq method basic functionality" do
12
+ expect(Sys::CPU).to respond_to(:freq)
13
+ expect{ Sys::CPU.freq }.not_to raise_error
14
+ end
15
+
16
+ example "freq method does not accept any arguments" do
17
+ expect{ Sys::CPU.freq(0) }.to raise_error(ArgumentError)
18
+ end
19
+
20
+ example "freq method returns a sane value" do
21
+ expect(Sys::CPU.freq).to be_kind_of(Integer)
22
+ expect(Sys::CPU.freq).to be > 100
23
+ end
24
+
25
+ example "fpu_type basic functionality" do
26
+ expect(Sys::CPU).to respond_to(:fpu_type)
27
+ expect{ Sys::CPU.fpu_type }.not_to raise_error
28
+ end
29
+
30
+ example "fpu_type returns a sane value" do
31
+ expect(Sys::CPU.fpu_type).to be_kind_of(String)
32
+ expect(Sys::CPU.fpu_type).not_to be_empty
33
+ end
34
+
35
+ example "load_avg basic functionality" do
36
+ expect(Sys::CPU).to respond_to(:load_avg)
37
+ expect{ Sys::CPU.load_avg }.not_to raise_error
38
+ end
39
+
40
+ example "load_avg method returns the expected values" do
41
+ expect(Sys::CPU.load_avg).to be_kind_of(Array)
42
+ expect(Sys::CPU.load_avg.length).to eq(3)
43
+ expect(Sys::CPU.load_avg.first).to be_kind_of(Float)
44
+ end
45
+
46
+ example "model method basic functionality" do
47
+ expect(Sys::CPU).to respond_to(:model)
48
+ expect{ Sys::CPU.model }.not_to raise_error
49
+ end
50
+
51
+ example "model method returns a sane value" do
52
+ expect(Sys::CPU.model).to be_kind_of(String)
53
+ expect(Sys::CPU.model).not_to be_empty
54
+ end
55
+
56
+ example "num_cpu method basic functionalty" do
57
+ expect(Sys::CPU).to respond_to(:num_cpu)
58
+ expect{ Sys::CPU.num_cpu }.not_to raise_error
59
+ end
60
+
61
+ example "num_cpu method returns a sane value" do
62
+ expect(Sys::CPU.num_cpu).to be_kind_of(Integer)
63
+ expect(Sys::CPU.num_cpu).to be > 0
64
+ end
65
+
66
+ example "state basic functionality" do
67
+ expect(Sys::CPU).to respond_to(:state)
68
+ expect{ Sys::CPU.state }.not_to raise_error
69
+ end
70
+
71
+ example "state method accepts one optional argument" do
72
+ expect{ Sys::CPU.state(0) }.not_to raise_error
73
+ expect{ Sys::CPU.state(0,0) }.to raise_error(ArgumentError)
74
+ end
75
+
76
+ example "state method returns a sane value" do
77
+ expect(Sys::CPU.state(0)).to be_kind_of(String)
78
+ expect(Sys::CPU.state.empty?).to be false
79
+ end
80
+ end
@@ -0,0 +1,61 @@
1
+ ######################################################################
2
+ # sys_cpu_windows_spec.rb
3
+ #
4
+ # Test suite for MS Windows systems. This should be run via the
5
+ # 'rake test' task.
6
+ ######################################################################
7
+ require 'spec_helper'
8
+ require 'sys/cpu'
9
+ require 'socket'
10
+
11
+ RSpec.describe Sys::CPU, :windows => true do
12
+ let(:host) { Socket.gethostname }
13
+
14
+ example "architecture" do
15
+ expect(Sys::CPU).to respond_to(:architecture)
16
+ expect{ Sys::CPU.architecture }.not_to raise_error
17
+ expect{ Sys::CPU.architecture(host) }.not_to raise_error
18
+ expect(Sys::CPU.architecture).to be_kind_of(String)
19
+ end
20
+
21
+ example "freq" do
22
+ expect(Sys::CPU).to respond_to(:freq)
23
+ expect{ Sys::CPU.freq }.not_to raise_error
24
+ expect{ Sys::CPU.freq(0) }.not_to raise_error
25
+ expect{ Sys::CPU.freq(0, host) }.not_to raise_error
26
+ expect(Sys::CPU.freq).to be_kind_of(Integer)
27
+ end
28
+
29
+ example "model" do
30
+ expect(Sys::CPU).to respond_to(:model)
31
+ expect{ Sys::CPU.model }.not_to raise_error
32
+ expect{ Sys::CPU.model(host) }.not_to raise_error
33
+ expect(Sys::CPU.model).to be_kind_of(String)
34
+ end
35
+
36
+ example "num_cpu" do
37
+ expect(Sys::CPU).to respond_to(:num_cpu)
38
+ expect{ Sys::CPU.num_cpu }.not_to raise_error
39
+ expect{ Sys::CPU.num_cpu(host) }.not_to raise_error
40
+ expect(Sys::CPU.num_cpu).to be_kind_of(Integer)
41
+ end
42
+
43
+ example "cpu_type" do
44
+ expect(Sys::CPU).to respond_to(:cpu_type)
45
+ expect{ Sys::CPU.cpu_type }.not_to raise_error
46
+ expect{ Sys::CPU.cpu_type(host) }.not_to raise_error
47
+ expect(Sys::CPU.cpu_type).to be_kind_of(String)
48
+ end
49
+
50
+ example "load_avg" do
51
+ expect(Sys::CPU).to respond_to(:load_avg)
52
+ expect{ Sys::CPU.load_avg }.not_to raise_error
53
+ expect{ Sys::CPU.load_avg(0, host) }.not_to raise_error
54
+ expect(Sys::CPU.load_avg).to be_kind_of(Integer)
55
+ end
56
+
57
+ example "processors" do
58
+ expect(Sys::CPU).to respond_to(:processors)
59
+ expect{ Sys::CPU.processors{}.not_to raise_error }
60
+ end
61
+ end