| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # | 2 # |
| 3 # $Id: _bsd.py 664 2010-10-09 16:14:34Z g.rodola $ | 3 # $Id: _bsd.py 1142 2011-10-05 18:45:49Z g.rodola $ |
| 4 # | 4 # |
| 5 # Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. |
| 6 # Use of this source code is governed by a BSD-style license that can be |
| 7 # found in the LICENSE file. |
| 8 |
| 9 """BSD specific tests. These are implicitly run by test_psutil.py.""" |
| 5 | 10 |
| 6 import unittest | 11 import unittest |
| 7 import subprocess | 12 import subprocess |
| 8 import time | 13 import time |
| 9 import re | 14 import re |
| 10 import sys | 15 import sys |
| 11 | 16 |
| 12 import psutil | 17 import psutil |
| 13 | 18 |
| 14 from test_psutil import reap_children, get_test_subprocess | 19 from test_psutil import reap_children, get_test_subprocess, sh |
| 15 from _posix import ps | |
| 16 | |
| 17 | 20 |
| 18 def sysctl(cmdline): | 21 def sysctl(cmdline): |
| 19 """Expects a sysctl command with an argument and parse the result | 22 """Expects a sysctl command with an argument and parse the result |
| 20 returning only the value of interest. | 23 returning only the value of interest. |
| 21 """ | 24 """ |
| 22 p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE) | 25 result = sh("sysctl " + cmdline) |
| 23 result = p.communicate()[0].strip().split()[1] | 26 result = result[result.find(": ") + 2:] |
| 24 if sys.version_info >= (3,): | |
| 25 result = str(result, sys.stdout.encoding) | |
| 26 try: | 27 try: |
| 27 return int(result) | 28 return int(result) |
| 28 except ValueError: | 29 except ValueError: |
| 29 return result | 30 return result |
| 30 | 31 |
| 31 def parse_sysctl_vmtotal(output): | 32 def parse_sysctl_vmtotal(output): |
| 32 """Parse sysctl vm.vmtotal output returning total and free memory | 33 """Parse sysctl vm.vmtotal output returning total and free memory |
| 33 values. | 34 values. |
| 34 """ | 35 """ |
| 35 line = output.split('\n')[4] # our line of interest | 36 line = output.split('\n')[4] # our line of interest |
| (...skipping 11 matching lines...) Expand all Loading... |
| 47 def setUp(self): | 48 def setUp(self): |
| 48 self.pid = get_test_subprocess().pid | 49 self.pid = get_test_subprocess().pid |
| 49 | 50 |
| 50 def tearDown(self): | 51 def tearDown(self): |
| 51 reap_children() | 52 reap_children() |
| 52 | 53 |
| 53 def test_TOTAL_PHYMEM(self): | 54 def test_TOTAL_PHYMEM(self): |
| 54 sysctl_hwphymem = sysctl('sysctl hw.physmem') | 55 sysctl_hwphymem = sysctl('sysctl hw.physmem') |
| 55 self.assertEqual(sysctl_hwphymem, psutil.TOTAL_PHYMEM) | 56 self.assertEqual(sysctl_hwphymem, psutil.TOTAL_PHYMEM) |
| 56 | 57 |
| 58 def test_BOOT_TIME(self): |
| 59 s = sysctl('sysctl kern.boottime') |
| 60 s = s[s.find(" sec = ") + 7:] |
| 61 s = s[:s.find(',')] |
| 62 btime = int(s) |
| 63 self.assertEqual(btime, psutil.BOOT_TIME) |
| 64 |
| 57 def test_avail_phymem(self): | 65 def test_avail_phymem(self): |
| 58 # This test is not particularly accurate and may fail if the OS is | 66 # This test is not particularly accurate and may fail if the OS is |
| 59 # consuming memory for other applications. | 67 # consuming memory for other applications. |
| 60 # We just want to test that the difference between psutil result | 68 # We just want to test that the difference between psutil result |
| 61 # and sysctl's is not too high. | 69 # and sysctl's is not too high. |
| 62 _sum = sum((sysctl("sysctl vm.stats.vm.v_inactive_count"), | 70 _sum = sum((sysctl("sysctl vm.stats.vm.v_inactive_count"), |
| 63 sysctl("sysctl vm.stats.vm.v_cache_count"), | 71 sysctl("sysctl vm.stats.vm.v_cache_count"), |
| 64 sysctl("sysctl vm.stats.vm.v_free_count") | 72 sysctl("sysctl vm.stats.vm.v_free_count") |
| 65 )) | 73 )) |
| 66 _pagesize = sysctl("sysctl hw.pagesize") | 74 _pagesize = sysctl("sysctl hw.pagesize") |
| 67 sysctl_avail_phymem = _sum * _pagesize | 75 sysctl_avail_phymem = _sum * _pagesize |
| 68 psutil_avail_phymem = psutil.avail_phymem() | 76 psutil_avail_phymem = psutil.phymem_usage().free |
| 69 difference = abs(psutil_avail_phymem - sysctl_avail_phymem) | 77 difference = abs(psutil_avail_phymem - sysctl_avail_phymem) |
| 70 # On my system both sysctl and psutil report the same values. | 78 # On my system both sysctl and psutil report the same values. |
| 71 # Let's use a tollerance of 0.5 MB and consider the test as failed | 79 # Let's use a tollerance of 0.5 MB and consider the test as failed |
| 72 # if we go over it. | 80 # if we go over it. |
| 73 if difference > (0.5 * 2**20): | 81 if difference > (0.5 * 2**20): |
| 74 self.fail("sysctl=%s; psutil=%s; difference=%s;" %( | 82 self.fail("sysctl=%s; psutil=%s; difference=%s;" %( |
| 75 sysctl_avail_phymem, psutil_avail_phymem, difference)) | 83 sysctl_avail_phymem, psutil_avail_phymem, difference)) |
| 76 | 84 |
| 77 def test_total_virtmem(self): | 85 def test_total_virtmem(self): |
| 78 # This test is not particularly accurate and may fail if the OS is | 86 # This test is not particularly accurate and may fail if the OS is |
| 79 # consuming memory for other applications. | 87 # consuming memory for other applications. |
| 80 # We just want to test that the difference between psutil result | 88 # We just want to test that the difference between psutil result |
| 81 # and sysctl's is not too high. | 89 # and sysctl's is not too high. |
| 82 p = subprocess.Popen("sysctl vm.vmtotal", shell=1, stdout=subprocess.PIP
E) | 90 p = subprocess.Popen("sysctl vm.vmtotal", shell=1, stdout=subprocess.PIP
E) |
| 83 result = p.communicate()[0].strip() | 91 result = p.communicate()[0].strip() |
| 84 if sys.version_info >= (3,): | 92 if sys.version_info >= (3,): |
| 85 result = str(result, sys.stdout.encoding) | 93 result = str(result, sys.stdout.encoding) |
| 86 sysctl_total_virtmem, _ = parse_sysctl_vmtotal(result) | 94 sysctl_total_virtmem, _ = parse_sysctl_vmtotal(result) |
| 87 psutil_total_virtmem = psutil.total_virtmem() | 95 psutil_total_virtmem = psutil.virtmem_usage().total |
| 88 difference = abs(sysctl_total_virtmem - psutil_total_virtmem) | 96 difference = abs(sysctl_total_virtmem - psutil_total_virtmem) |
| 89 | 97 |
| 90 # On my system I get a difference of 4657152 bytes, probably because | 98 # On my system I get a difference of 4657152 bytes, probably because |
| 91 # the system is consuming memory for this same test. | 99 # the system is consuming memory for this same test. |
| 92 # Assuming psutil is right, let's use a tollerance of 10 MB and consider | 100 # Assuming psutil is right, let's use a tollerance of 10 MB and consider |
| 93 # the test as failed if we go over it. | 101 # the test as failed if we go over it. |
| 94 if difference > (10 * 2**20): | 102 if difference > (10 * 2**20): |
| 95 self.fail("sysctl=%s; psutil=%s; difference=%s;" %( | 103 self.fail("sysctl=%s; psutil=%s; difference=%s;" %( |
| 96 sysctl_total_virtmem, psutil_total_virtmem, difference) | 104 sysctl_total_virtmem, psutil_total_virtmem, difference)) |
| 97 ) | |
| 98 | 105 |
| 99 def test_avail_virtmem(self): | 106 def test_avail_virtmem(self): |
| 100 # This test is not particularly accurate and may fail if the OS is | 107 # This test is not particularly accurate and may fail if the OS is |
| 101 # consuming memory for other applications. | 108 # consuming memory for other applications. |
| 102 # We just want to test that the difference between psutil result | 109 # We just want to test that the difference between psutil result |
| 103 # and sysctl's is not too high. | 110 # and sysctl's is not too high. |
| 104 p = subprocess.Popen("sysctl vm.vmtotal", shell=1, stdout=subprocess.PIP
E) | 111 p = subprocess.Popen("sysctl vm.vmtotal", shell=1, stdout=subprocess.PIP
E) |
| 105 result = p.communicate()[0].strip() | 112 result = p.communicate()[0].strip() |
| 106 if sys.version_info >= (3,): | 113 if sys.version_info >= (3,): |
| 107 result = str(result, sys.stdout.encoding) | 114 result = str(result, sys.stdout.encoding) |
| 108 _, sysctl_avail_virtmem = parse_sysctl_vmtotal(result) | 115 _, sysctl_avail_virtmem = parse_sysctl_vmtotal(result) |
| 109 psutil_avail_virtmem = psutil.avail_virtmem() | 116 psutil_avail_virtmem = psutil.virtmem_usage().free |
| 110 difference = abs(sysctl_avail_virtmem - psutil_avail_virtmem) | 117 difference = abs(sysctl_avail_virtmem - psutil_avail_virtmem) |
| 111 # let's assume the test is failed if difference is > 0.5 MB | 118 # let's assume the test is failed if difference is > 0.5 MB |
| 112 if difference > (0.5 * 2**20): | 119 if difference > (0.5 * 2**20): |
| 113 self.fail("sysctl=%s; psutil=%s; difference=%s;" %( | 120 self.fail(difference) |
| 114 sysctl_avail_virtmem, psutil_avail_virtmem, difference)) | |
| 115 | 121 |
| 116 def test_process_create_time(self): | 122 def test_process_create_time(self): |
| 117 cmdline = "ps -o lstart -p %s" %self.pid | 123 cmdline = "ps -o lstart -p %s" %self.pid |
| 118 p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE) | 124 p = subprocess.Popen(cmdline, shell=1, stdout=subprocess.PIPE) |
| 119 output = p.communicate()[0] | 125 output = p.communicate()[0] |
| 120 if sys.version_info >= (3,): | 126 if sys.version_info >= (3,): |
| 121 output = str(output, sys.stdout.encoding) | 127 output = str(output, sys.stdout.encoding) |
| 122 start_ps = output.replace('STARTED', '').strip() | 128 start_ps = output.replace('STARTED', '').strip() |
| 123 start_psutil = psutil.Process(self.pid).create_time | 129 start_psutil = psutil.Process(self.pid).create_time |
| 124 start_psutil = time.strftime("%a %b %e %H:%M:%S %Y", | 130 start_psutil = time.strftime("%a %b %e %H:%M:%S %Y", |
| 125 time.localtime(start_psutil)) | 131 time.localtime(start_psutil)) |
| 126 self.assertEqual(start_ps, start_psutil) | 132 self.assertEqual(start_ps, start_psutil) |
| 127 | 133 |
| 134 def test_disks(self): |
| 135 # test psutil.disk_usage() and psutil.disk_partitions() |
| 136 # against "df -a" |
| 137 def df(path): |
| 138 out = sh('df -k "%s"' % path).strip() |
| 139 lines = out.split('\n') |
| 140 lines.pop(0) |
| 141 line = lines.pop(0) |
| 142 dev, total, used, free = line.split()[:4] |
| 143 if dev == 'none': |
| 144 dev = '' |
| 145 total = int(total) * 1024 |
| 146 used = int(used) * 1024 |
| 147 free = int(free) * 1024 |
| 148 return dev, total, used, free |
| 149 |
| 150 for part in psutil.disk_partitions(all=False): |
| 151 usage = psutil.disk_usage(part.mountpoint) |
| 152 dev, total, used, free = df(part.mountpoint) |
| 153 self.assertEqual(part.device, dev) |
| 154 self.assertEqual(usage.total, total) |
| 155 # 10 MB tollerance |
| 156 if abs(usage.free - free) > 10 * 1024 * 1024: |
| 157 self.fail("psutil=%s, df=%s" % usage.free, free) |
| 158 if abs(usage.used - used) > 10 * 1024 * 1024: |
| 159 self.fail("psutil=%s, df=%s" % usage.used, used) |
| 160 |
| 128 | 161 |
| 129 if __name__ == '__main__': | 162 if __name__ == '__main__': |
| 130 test_suite = unittest.TestSuite() | 163 test_suite = unittest.TestSuite() |
| 131 test_suite.addTest(unittest.makeSuite(BSDSpecificTestCase)) | 164 test_suite.addTest(unittest.makeSuite(BSDSpecificTestCase)) |
| 132 unittest.TextTestRunner(verbosity=2).run(test_suite) | 165 unittest.TextTestRunner(verbosity=2).run(test_suite) |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| OLD | NEW |