11
from test_psutil import kill, PYTHON, DEVNULL
16
"""Expects a sysctl command with an argument and parse the result
17
returning only the value of interest.
19
p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE)
20
result = p.communicate()[0].strip().split()[1]
21
if sys.version_info >= (3,):
22
result = str(result, sys.stdout.encoding)
28
def parse_sysctl_vmtotal(output):
29
"""Parse sysctl vm.vmtotal output returning total and free memory
32
line = output.split('\n')[4] # our line of interest
33
mobj = re.match(r'Virtual\s+Memory.*Total:\s+(\d+)K,\s+Active\s+(\d+)K.*', line)
34
total, active = mobj.groups()
35
# values are represented in kilo bytes
36
total = int(total) * 1024
37
active = int(active) * 1024
42
class BSDSpecificTestCase(unittest.TestCase):
45
self.pid = subprocess.Popen(PYTHON, stdout=DEVNULL, stderr=DEVNULL).pid
50
def test_TOTAL_PHYMEM(self):
51
sysctl_hwphymem = sysctl('sysctl hw.physmem')
52
self.assertEqual(sysctl_hwphymem, psutil.TOTAL_PHYMEM)
54
def test_avail_phymem(self):
55
# This test is not particularly accurate and may fail if the OS is
56
# consuming memory for other applications.
57
# We just want to test that the difference between psutil result
58
# and sysctl's is not too high.
59
_sum = sum((sysctl("sysctl vm.stats.vm.v_inactive_count"),
60
sysctl("sysctl vm.stats.vm.v_cache_count"),
61
sysctl("sysctl vm.stats.vm.v_free_count")
63
_pagesize = sysctl("sysctl hw.pagesize")
64
sysctl_avail_phymem = _sum * _pagesize
65
psutil_avail_phymem = psutil.avail_phymem()
66
difference = abs(psutil_avail_phymem - sysctl_avail_phymem)
67
# On my system both sysctl and psutil report the same values.
68
# Let's use a tollerance of 0.5 MB and consider the test as failed
70
if difference > (0.5 * 2**20):
71
self.fail("sysctl=%s; psutil=%s; difference=%s;" %(
72
sysctl_avail_phymem, psutil_avail_phymem, difference))
74
def test_total_virtmem(self):
75
# This test is not particularly accurate and may fail if the OS is
76
# consuming memory for other applications.
77
# We just want to test that the difference between psutil result
78
# and sysctl's is not too high.
79
p = subprocess.Popen("sysctl vm.vmtotal", shell=1, stdout=subprocess.PIPE)
80
result = p.communicate()[0].strip()
81
if sys.version_info >= (3,):
82
result = str(result, sys.stdout.encoding)
83
sysctl_total_virtmem, _ = parse_sysctl_vmtotal(result)
84
psutil_total_virtmem = psutil.total_virtmem()
85
difference = abs(sysctl_total_virtmem - psutil_total_virtmem)
87
# On my system I get a difference of 4657152 bytes, probably because
88
# the system is consuming memory for this same test.
89
# Assuming psutil is right, let's use a tollerance of 10 MB and consider
90
# the test as failed if we go over it.
91
if difference > (10 * 2**20):
92
self.fail("sysctl=%s; psutil=%s; difference=%s;" %(
93
sysctl_total_virtmem, psutil_total_virtmem, difference)
96
def test_avail_virtmem(self):
97
# This test is not particularly accurate and may fail if the OS is
98
# consuming memory for other applications.
99
# We just want to test that the difference between psutil result
100
# and sysctl's is not too high.
101
p = subprocess.Popen("sysctl vm.vmtotal", shell=1, stdout=subprocess.PIPE)
102
result = p.communicate()[0].strip()
103
if sys.version_info >= (3,):
104
result = str(result, sys.stdout.encoding)
105
_, sysctl_avail_virtmem = parse_sysctl_vmtotal(result)
106
psutil_avail_virtmem = psutil.avail_virtmem()
107
difference = abs(sysctl_avail_virtmem - psutil_avail_virtmem)
108
# let's assume the test is failed if difference is > 0.5 MB
109
if difference > (0.5 * 2**20):
110
self.fail("sysctl=%s; psutil=%s; difference=%s;" %(
111
sysctl_avail_virtmem, psutil_avail_virtmem, difference)
114
def test_process_create_time(self):
115
cmdline = "ps -o lstart -p %s" %self.pid
116
p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE)
117
output = p.communicate()[0]
118
if sys.version_info >= (3,):
119
output = str(output, sys.stdout.encoding)
120
start_ps = output.replace('STARTED', '').strip()
121
start_psutil = psutil.Process(self.pid).create_time
122
start_psutil = time.strftime("%a %b %e %H:%M:%S %Y",
123
time.localtime(start_psutil))
124
self.assertEqual(start_ps, start_psutil)
127
if __name__ == '__main__':
128
test_suite = unittest.TestSuite()
129
test_suite.addTest(unittest.makeSuite(BSDSpecificTestCase))
130
unittest.TextTestRunner(verbosity=2).run(test_suite)
3
# $Id: _bsd.py 950 2011-03-19 21:37:09Z g.rodola $
14
from test_psutil import reap_children, get_test_subprocess
19
"""Expects a sysctl command with an argument and parse the result
20
returning only the value of interest.
22
p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE)
23
result = p.communicate()[0].strip()
24
if sys.version_info >= (3,):
25
result = result[result.find(b": ") + 2:]
26
result = str(result, sys.stdout.encoding)
28
result = result[result.find(": ") + 2:]
34
def parse_sysctl_vmtotal(output):
35
"""Parse sysctl vm.vmtotal output returning total and free memory
38
line = output.split('\n')[4] # our line of interest
39
mobj = re.match(r'Virtual\s+Memory.*Total:\s+(\d+)K,\s+Active\s+(\d+)K.*', line)
40
total, active = mobj.groups()
41
# values are represented in kilo bytes
42
total = int(total) * 1024
43
active = int(active) * 1024
48
class BSDSpecificTestCase(unittest.TestCase):
51
self.pid = get_test_subprocess().pid
56
def test_TOTAL_PHYMEM(self):
57
sysctl_hwphymem = sysctl('sysctl hw.physmem')
58
self.assertEqual(sysctl_hwphymem, psutil.TOTAL_PHYMEM)
60
def test_BOOT_TIME(self):
61
s = sysctl('sysctl kern.boottime')
62
s = s[s.find(" sec = ") + 7:]
65
self.assertEqual(btime, psutil.BOOT_TIME)
67
def test_avail_phymem(self):
68
# This test is not particularly accurate and may fail if the OS is
69
# consuming memory for other applications.
70
# We just want to test that the difference between psutil result
71
# and sysctl's is not too high.
72
_sum = sum((sysctl("sysctl vm.stats.vm.v_inactive_count"),
73
sysctl("sysctl vm.stats.vm.v_cache_count"),
74
sysctl("sysctl vm.stats.vm.v_free_count")
76
_pagesize = sysctl("sysctl hw.pagesize")
77
sysctl_avail_phymem = _sum * _pagesize
78
psutil_avail_phymem = psutil.avail_phymem()
79
difference = abs(psutil_avail_phymem - sysctl_avail_phymem)
80
# On my system both sysctl and psutil report the same values.
81
# Let's use a tollerance of 0.5 MB and consider the test as failed
83
if difference > (0.5 * 2**20):
84
self.fail("sysctl=%s; psutil=%s; difference=%s;" %(
85
sysctl_avail_phymem, psutil_avail_phymem, difference))
87
def test_total_virtmem(self):
88
# This test is not particularly accurate and may fail if the OS is
89
# consuming memory for other applications.
90
# We just want to test that the difference between psutil result
91
# and sysctl's is not too high.
92
p = subprocess.Popen("sysctl vm.vmtotal", shell=1, stdout=subprocess.PIPE)
93
result = p.communicate()[0].strip()
94
if sys.version_info >= (3,):
95
result = str(result, sys.stdout.encoding)
96
sysctl_total_virtmem, _ = parse_sysctl_vmtotal(result)
97
psutil_total_virtmem = psutil.total_virtmem()
98
difference = abs(sysctl_total_virtmem - psutil_total_virtmem)
100
# On my system I get a difference of 4657152 bytes, probably because
101
# the system is consuming memory for this same test.
102
# Assuming psutil is right, let's use a tollerance of 10 MB and consider
103
# the test as failed if we go over it.
104
if difference > (10 * 2**20):
105
self.fail("sysctl=%s; psutil=%s; difference=%s;" %(
106
sysctl_total_virtmem, psutil_total_virtmem, difference))
108
def test_avail_virtmem(self):
109
# This test is not particularly accurate and may fail if the OS is
110
# consuming memory for other applications.
111
# We just want to test that the difference between psutil result
112
# and sysctl's is not too high.
113
p = subprocess.Popen("sysctl vm.vmtotal", shell=1, stdout=subprocess.PIPE)
114
result = p.communicate()[0].strip()
115
if sys.version_info >= (3,):
116
result = str(result, sys.stdout.encoding)
117
_, sysctl_avail_virtmem = parse_sysctl_vmtotal(result)
118
psutil_avail_virtmem = psutil.avail_virtmem()
119
difference = abs(sysctl_avail_virtmem - psutil_avail_virtmem)
120
# let's assume the test is failed if difference is > 0.5 MB
121
if difference > (0.5 * 2**20):
122
self.fail("sysctl=%s; psutil=%s; difference=%s;" %(
123
sysctl_avail_virtmem, psutil_avail_virtmem, difference))
125
def test_process_create_time(self):
126
cmdline = "ps -o lstart -p %s" %self.pid
127
p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE)
128
output = p.communicate()[0]
129
if sys.version_info >= (3,):
130
output = str(output, sys.stdout.encoding)
131
start_ps = output.replace('STARTED', '').strip()
132
start_psutil = psutil.Process(self.pid).create_time
133
start_psutil = time.strftime("%a %b %e %H:%M:%S %Y",
134
time.localtime(start_psutil))
135
self.assertEqual(start_ps, start_psutil)
138
if __name__ == '__main__':
139
test_suite = unittest.TestSuite()
140
test_suite.addTest(unittest.makeSuite(BSDSpecificTestCase))
141
unittest.TextTestRunner(verbosity=2).run(test_suite)