sys-proctable 0.9.1-universal-linux

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,32 @@
1
+ require 'sys/proctable'
2
+ require 'rbconfig'
3
+
4
+ # The Sys module serves as a namespace only
5
+ module Sys
6
+
7
+ # The Top class serves as a toplevel name for the 'top' method.
8
+ class Top
9
+
10
+ # The version of the sys-top library
11
+ VERSION = '1.0.3'
12
+
13
+ # Returns an array of Struct::ProcTableStruct elements containing up
14
+ # to +num+ elements, sorted by +field+. The default number of elements
15
+ # is 10, while the default field is 'pctcpu'.
16
+ #
17
+ # Exception: the default sort field is 'pid' on Linux and Windows.
18
+ #
19
+ def self.top(num=10, field='pctcpu')
20
+ field = field.to_s if field.is_a?(Symbol)
21
+
22
+ windows = /mswin|win32|windows|dos|cygwin|mingw/i
23
+
24
+ # Sort by pid on Windows by default
25
+ if Config::CONFIG['host_os'].match(windows) && field == 'pctcpu'
26
+ field = 'pid'
27
+ end
28
+
29
+ Sys::ProcTable.ps.sort_by{ |obj| obj.send(field) || '' }[0..num-1]
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+ require 'rbconfig'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'sys-proctable'
6
+ spec.version = '0.9.1'
7
+ spec.author = 'Daniel J. Berger'
8
+ spec.license = 'Artistic 2.0'
9
+ spec.email = 'djberg96@gmail.com'
10
+ spec.homepage = 'http://www.rubyforge.org/projects/sysutils'
11
+ spec.platform = Gem::Platform::CURRENT # Probably altered by Rake task
12
+ spec.summary = 'An interface for providing process table information'
13
+ spec.test_files = ['test/test_sys_proctable_all.rb']
14
+
15
+ # Additional files for your platform are added by the 'rake gem' task.
16
+ spec.files = [
17
+ 'benchmarks/bench_ps.rb',
18
+ 'examples/example_ps.rb',
19
+ 'lib/sys/top.rb',
20
+ 'CHANGES',
21
+ 'MANIFEST',
22
+ 'Rakefile',
23
+ 'README',
24
+ 'sys-proctable.gemspec'
25
+ ]
26
+
27
+ spec.rubyforge_project = 'sysutils'
28
+ spec.extra_rdoc_files = ['CHANGES', 'README', 'MANIFEST', 'doc/top.txt']
29
+
30
+ spec.add_development_dependency('test-unit', '>= 2.1.2')
31
+
32
+ spec.description = <<-EOF
33
+ The sys-proctable library provides an interface for gathering information
34
+ about processes on your system, i.e. the process table. Most major
35
+ platforms are supported and, while different platforms may return
36
+ different information, the external interface is identical across
37
+ platforms.
38
+ EOF
39
+ end
@@ -0,0 +1,85 @@
1
+ #######################################################################
2
+ # test_sys_proctable_all.rb
3
+ #
4
+ # Test suite for methods common to all platforms. Generally speaking
5
+ # you should run this test case using the 'rake test' task.
6
+ #######################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
10
+ require 'test/unit'
11
+ require 'sys/proctable'
12
+ require 'rbconfig'
13
+ require 'test/test_sys_top'
14
+ include Sys
15
+
16
+ class TC_ProcTable_All < Test::Unit::TestCase
17
+ def self.startup
18
+ @@windows = Config::CONFIG['host_os'] =~ /windows|win32|msdos|mswin32|mingw|cygwin/i
19
+ end
20
+
21
+ def setup
22
+ @pid = @@windows ? 0 : 1
23
+ end
24
+
25
+ def test_version
26
+ assert_equal('0.9.1', ProcTable::VERSION)
27
+ end
28
+
29
+ def test_fields
30
+ assert_respond_to(ProcTable, :fields)
31
+ assert_nothing_raised{ ProcTable.fields }
32
+ assert_kind_of(Array, ProcTable.fields)
33
+ assert_kind_of(String, ProcTable.fields.first)
34
+ end
35
+
36
+ def test_ps
37
+ assert_respond_to(ProcTable, :ps)
38
+ assert_nothing_raised{ ProcTable.ps }
39
+ assert_nothing_raised{ ProcTable.ps{} }
40
+ end
41
+
42
+ def test_ps_with_pid
43
+ assert_nothing_raised{ ProcTable.ps(0) }
44
+ end
45
+
46
+ def test_ps_with_explicit_nil
47
+ assert_nothing_raised{ ProcTable.ps(nil) }
48
+ assert_kind_of(Array, ProcTable.ps(nil))
49
+ end
50
+
51
+ def test_ps_return_value
52
+ assert_kind_of(Array, ProcTable.ps)
53
+ assert_kind_of(Struct::ProcTableStruct, ProcTable.ps(@pid))
54
+ assert_equal(nil, ProcTable.ps(999999999))
55
+ assert_equal(nil, ProcTable.ps(999999999){})
56
+ assert_equal(nil, ProcTable.ps{})
57
+ end
58
+
59
+ def test_ps_returned_struct_is_frozen
60
+ assert_true(ProcTable.ps.first.frozen?)
61
+ end
62
+
63
+ def test_ps_expected_errors
64
+ assert_raises(TypeError){ ProcTable.ps('vim') }
65
+ omit_if(@@windows, 'ArgumentError check skipped on MS Windows')
66
+ assert_raises(ArgumentError){ ProcTable.ps(0, 'localhost') }
67
+ end
68
+
69
+ def test_new_not_allowed
70
+ assert_raise(NoMethodError){ Sys::ProcTable.new }
71
+ end
72
+
73
+ def test_error_class_defined
74
+ assert_not_nil(Sys::ProcTable::Error)
75
+ assert_kind_of(StandardError, Sys::ProcTable::Error.new)
76
+ end
77
+
78
+ def teardown
79
+ @pid = nil
80
+ end
81
+
82
+ def self.teardown
83
+ @@windows = nil
84
+ end
85
+ end
@@ -0,0 +1,298 @@
1
+ #######################################################################
2
+ # test_sys_proctable_linux.rb
3
+ #
4
+ # Test suite for the Linux version of the sys-proctable library. You
5
+ # should run these tests via the 'rake test' task.
6
+ #######################################################################
7
+ require 'rubygems'
8
+ gem 'test-unit'
9
+
10
+ require 'sys/proctable'
11
+ require 'test/test_sys_proctable_all'
12
+ include Sys
13
+
14
+ class TC_ProcTable_Linux < Test::Unit::TestCase
15
+ def self.startup
16
+ @@fields = %w/
17
+ cmdline cwd exe pid name uid euid gid egid comm state ppid pgrp
18
+ session tty_num tpgid flags minflt cminflt majflt cmajflt utime
19
+ stime cutime cstime priority nice itrealvalue starttime vsize
20
+ rss rlim startcode endcode startstack kstkesp kstkeip signal blocked
21
+ sigignore sigcatch wchan nswap cnswap exit_signal processor environ
22
+ pctcpu pctmem
23
+ /
24
+ end
25
+
26
+ def setup
27
+ @ptable = ProcTable.ps.last
28
+ end
29
+
30
+ def test_cmdline
31
+ assert_respond_to(@ptable, :cmdline)
32
+ assert_kind_of(String, @ptable.cmdline)
33
+ end
34
+
35
+ def test_cwd
36
+ assert_respond_to(@ptable, :cwd)
37
+ assert_true([NilClass, String].include?(@ptable.cwd.class))
38
+ end
39
+
40
+ def test_environ
41
+ assert_respond_to(@ptable, :environ)
42
+ assert_kind_of(Hash, @ptable.environ)
43
+ end
44
+
45
+ def test_exe
46
+ assert_respond_to(@ptable, :exe)
47
+ assert_kind_of(String, @ptable.exe)
48
+ end
49
+
50
+ def test_fd
51
+ assert_respond_to(@ptable, :fd)
52
+ assert_kind_of(Hash, @ptable.fd)
53
+ end
54
+
55
+ def test_root
56
+ assert_respond_to(@ptable, :root)
57
+ assert_kind_of(String, @ptable.root)
58
+ end
59
+
60
+ def test_pid
61
+ assert_respond_to(@ptable, :pid)
62
+ assert_kind_of(Fixnum, @ptable.pid)
63
+ end
64
+
65
+ def test_comm
66
+ assert_respond_to(@ptable, :comm)
67
+ assert_kind_of(String, @ptable.comm)
68
+ end
69
+
70
+ def test_state
71
+ assert_respond_to(@ptable, :state)
72
+ assert_kind_of(String, @ptable.state)
73
+ end
74
+
75
+ def test_ppid
76
+ assert_respond_to(@ptable, :ppid)
77
+ assert_kind_of(Fixnum, @ptable.ppid)
78
+ end
79
+
80
+ def test_pgrp
81
+ assert_respond_to(@ptable, :pgrp)
82
+ assert_kind_of(Fixnum, @ptable.pgrp)
83
+ end
84
+
85
+ def test_session
86
+ assert_respond_to(@ptable, :session)
87
+ assert_kind_of(Fixnum, @ptable.session)
88
+ end
89
+
90
+ def test_tty_nr
91
+ assert_respond_to(@ptable, :tty_nr)
92
+ assert_kind_of(Fixnum, @ptable.tty_nr)
93
+ end
94
+
95
+ def test_tpgid
96
+ assert_respond_to(@ptable, :tpgid)
97
+ assert_kind_of(Fixnum, @ptable.tpgid)
98
+ end
99
+
100
+ def test_flags
101
+ assert_respond_to(@ptable, :flags)
102
+ assert_kind_of(Fixnum, @ptable.flags)
103
+ end
104
+
105
+ def test_minflt
106
+ assert_respond_to(@ptable, :minflt)
107
+ assert_kind_of(Fixnum, @ptable.minflt)
108
+ end
109
+
110
+ def test_cminflt
111
+ assert_respond_to(@ptable, :cminflt)
112
+ assert_kind_of(Fixnum, @ptable.cminflt)
113
+ end
114
+
115
+ def test_majflt
116
+ assert_respond_to(@ptable, :majflt)
117
+ assert_kind_of(Fixnum, @ptable.majflt)
118
+ end
119
+
120
+ def test_cmajflt
121
+ assert_respond_to(@ptable, :cmajflt)
122
+ assert_kind_of(Fixnum, @ptable.cmajflt)
123
+ end
124
+
125
+ def test_utime
126
+ assert_respond_to(@ptable, :utime)
127
+ assert_kind_of(Fixnum, @ptable.utime)
128
+ end
129
+
130
+ def test_stime
131
+ assert_respond_to(@ptable, :stime)
132
+ assert_kind_of(Fixnum, @ptable.stime)
133
+ end
134
+
135
+ def test_cutime
136
+ assert_respond_to(@ptable, :cutime)
137
+ assert_kind_of(Fixnum, @ptable.cutime)
138
+ end
139
+
140
+ def test_cstime
141
+ assert_respond_to(@ptable, :cstime)
142
+ assert_kind_of(Fixnum, @ptable.cstime)
143
+ end
144
+
145
+ def test_priority
146
+ assert_respond_to(@ptable, :priority)
147
+ assert_kind_of(Fixnum, @ptable.priority)
148
+ end
149
+
150
+ def test_nice
151
+ assert_respond_to(@ptable, :nice)
152
+ assert_kind_of(Fixnum, @ptable.nice)
153
+ end
154
+
155
+ def test_itrealvalue
156
+ assert_respond_to(@ptable, :itrealvalue)
157
+ assert_kind_of(Fixnum, @ptable.itrealvalue)
158
+ end
159
+
160
+ def test_starttime
161
+ assert_respond_to(@ptable, :starttime)
162
+ assert_kind_of(Fixnum, @ptable.starttime)
163
+ end
164
+
165
+ def test_vsize
166
+ assert_respond_to(@ptable, :vsize)
167
+ assert_kind_of(Fixnum, @ptable.vsize)
168
+ end
169
+
170
+ def test_rss
171
+ assert_respond_to(@ptable, :rss)
172
+ assert_kind_of(Fixnum, @ptable.rss)
173
+ end
174
+
175
+ def test_rlim
176
+ assert_respond_to(@ptable, :rlim)
177
+ assert_kind_of(Integer, @ptable.rlim)
178
+ end
179
+
180
+ def test_startcode
181
+ assert_respond_to(@ptable, :startcode)
182
+ assert_kind_of(Fixnum, @ptable.startcode)
183
+ end
184
+
185
+ def test_endcode
186
+ assert_respond_to(@ptable, :endcode)
187
+ assert_kind_of(Fixnum, @ptable.endcode)
188
+ end
189
+
190
+ def test_startstack
191
+ assert_respond_to(@ptable, :startstack)
192
+ assert_kind_of(Integer, @ptable.startstack)
193
+ end
194
+
195
+ def test_kstkesp
196
+ assert_respond_to(@ptable, :kstkesp)
197
+ assert_kind_of(Integer, @ptable.kstkesp)
198
+ end
199
+
200
+ def test_kstkeip
201
+ assert_respond_to(@ptable, :kstkeip)
202
+ assert_kind_of(Integer, @ptable.kstkeip)
203
+ end
204
+
205
+ def test_signal
206
+ assert_respond_to(@ptable, :signal)
207
+ assert_kind_of(Integer, @ptable.signal)
208
+ end
209
+
210
+ def test_blocked
211
+ assert_respond_to(@ptable, :blocked)
212
+ assert_kind_of(Fixnum, @ptable.blocked)
213
+ end
214
+
215
+ def test_sigignore
216
+ assert_respond_to(@ptable, :sigignore)
217
+ assert_kind_of(Fixnum, @ptable.sigignore)
218
+ end
219
+
220
+ def test_sigcatch
221
+ assert_respond_to(@ptable, :sigcatch)
222
+ assert_kind_of(Numeric, @ptable.sigcatch)
223
+ end
224
+
225
+ def test_wchan
226
+ assert_respond_to(@ptable, :wchan)
227
+ assert_kind_of(Numeric, @ptable.wchan)
228
+ end
229
+
230
+ def test_nswap
231
+ assert_respond_to(@ptable, :nswap)
232
+ assert_kind_of(Fixnum, @ptable.nswap)
233
+ end
234
+
235
+ def test_cnswap
236
+ assert_respond_to(@ptable, :cnswap)
237
+ assert_kind_of(Fixnum, @ptable.cnswap)
238
+ end
239
+
240
+ def test_exit_signal
241
+ assert_respond_to(@ptable, :exit_signal)
242
+ assert_kind_of(Fixnum, @ptable.exit_signal)
243
+ end
244
+
245
+ def test_processor
246
+ assert_respond_to(@ptable, :processor)
247
+ assert_kind_of(Fixnum, @ptable.processor)
248
+ end
249
+
250
+ def test_rt_priority
251
+ assert_respond_to(@ptable, :rt_priority)
252
+ assert_kind_of(Fixnum, @ptable.rt_priority)
253
+ end
254
+
255
+ def test_policy
256
+ assert_respond_to(@ptable, :policy)
257
+ assert_kind_of(Fixnum, @ptable.policy)
258
+ end
259
+
260
+ def test_name
261
+ assert_respond_to(@ptable, :name)
262
+ assert_kind_of(String, @ptable.name)
263
+ end
264
+
265
+ def test_uid
266
+ assert_respond_to(@ptable, :uid)
267
+ assert_kind_of(Fixnum, @ptable.uid)
268
+ end
269
+
270
+ def test_euid
271
+ assert_respond_to(@ptable, :euid)
272
+ assert_kind_of(Fixnum, @ptable.euid)
273
+ end
274
+
275
+ def test_gid
276
+ assert_respond_to(@ptable, :gid)
277
+ assert_kind_of(Fixnum, @ptable.gid)
278
+ end
279
+
280
+ def test_egid
281
+ assert_respond_to(@ptable, :egid)
282
+ assert_kind_of(Fixnum, @ptable.egid)
283
+ end
284
+
285
+ def test_pctmem
286
+ assert_respond_to(@ptable, :pctmem)
287
+ assert_kind_of(Float, @ptable.pctmem)
288
+ end
289
+
290
+ def test_pctcpu
291
+ assert_respond_to(@ptable, :pctcpu)
292
+ assert_kind_of(Float, @ptable.pctcpu)
293
+ end
294
+
295
+ def teardown
296
+ @ptable = nil
297
+ end
298
+ end