~ubuntu-branches/ubuntu/oneiric/python-psutil/oneiric

« back to all changes in this revision

Viewing changes to test/_bsd.py

  • Committer: Bazaar Package Importer
  • Author(s): Sandro Tosi
  • Date: 2011-04-04 20:26:42 UTC
  • mfrom: (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110404202642-u2fyar19eabqb2mn
Tags: 0.2.1-1
* New upstream release
* debian/copyright
  - extended packaging copyright years
* debian/rules
  - use the correct PYTHONPATH when running tests, for all supported versions
* debian/control
  - it now contains also extensions, so it's an arch:any package
  - move python-support from b-d-i to b-d
  - we now need python-all-dev in b-d
  - added procps to b-d, needed to run tests
* debian/{control, pyversion}
  - removed pyversion, replaced by XS-P-V field

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
import unittest
4
 
import subprocess
5
 
import time
6
 
import re
7
 
import sys
8
 
 
9
 
import psutil
10
 
 
11
 
from test_psutil import kill, PYTHON, DEVNULL
12
 
from _posix import ps
13
 
 
14
 
 
15
 
def sysctl(cmdline):
16
 
    """Expects a sysctl command with an argument and parse the result
17
 
    returning only the value of interest.
18
 
    """
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)
23
 
    try:
24
 
        return int(result)
25
 
    except ValueError:
26
 
        return result
27
 
 
28
 
def parse_sysctl_vmtotal(output):
29
 
    """Parse sysctl vm.vmtotal output returning total and free memory
30
 
    values.
31
 
    """
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
38
 
    free = total - active
39
 
    return total, free
40
 
 
41
 
 
42
 
class BSDSpecificTestCase(unittest.TestCase):
43
 
 
44
 
    def setUp(self):
45
 
        self.pid = subprocess.Popen(PYTHON, stdout=DEVNULL, stderr=DEVNULL).pid
46
 
 
47
 
    def tearDown(self):
48
 
        kill(self.pid)
49
 
 
50
 
    def test_TOTAL_PHYMEM(self):
51
 
        sysctl_hwphymem = sysctl('sysctl hw.physmem')
52
 
        self.assertEqual(sysctl_hwphymem, psutil.TOTAL_PHYMEM)
53
 
 
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")
62
 
                   ))
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
69
 
        # if we go over it.
70
 
        if difference > (0.5 * 2**20):
71
 
            self.fail("sysctl=%s; psutil=%s; difference=%s;" %(
72
 
                      sysctl_avail_phymem, psutil_avail_phymem, difference))
73
 
 
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)
86
 
 
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)
94
 
                      )
95
 
 
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)
112
 
                      )
113
 
 
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)
125
 
 
126
 
 
127
 
if __name__ == '__main__':
128
 
    test_suite = unittest.TestSuite()
129
 
    test_suite.addTest(unittest.makeSuite(BSDSpecificTestCase))
130
 
    unittest.TextTestRunner(verbosity=2).run(test_suite)
131
 
 
132
 
 
133
 
 
134
 
 
 
1
#!/usr/bin/env python
 
2
#
 
3
# $Id: _bsd.py 950 2011-03-19 21:37:09Z g.rodola $
 
4
#
 
5
 
 
6
import unittest
 
7
import subprocess
 
8
import time
 
9
import re
 
10
import sys
 
11
 
 
12
import psutil
 
13
 
 
14
from test_psutil import reap_children, get_test_subprocess
 
15
from _posix import ps
 
16
 
 
17
 
 
18
def sysctl(cmdline):
 
19
    """Expects a sysctl command with an argument and parse the result
 
20
    returning only the value of interest.
 
21
    """
 
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)
 
27
    else:
 
28
        result = result[result.find(": ") + 2:]
 
29
    try:
 
30
        return int(result)
 
31
    except ValueError:
 
32
        return result
 
33
 
 
34
def parse_sysctl_vmtotal(output):
 
35
    """Parse sysctl vm.vmtotal output returning total and free memory
 
36
    values.
 
37
    """
 
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
 
44
    free = total - active
 
45
    return total, free
 
46
 
 
47
 
 
48
class BSDSpecificTestCase(unittest.TestCase):
 
49
 
 
50
    def setUp(self):
 
51
        self.pid = get_test_subprocess().pid
 
52
 
 
53
    def tearDown(self):
 
54
        reap_children()
 
55
 
 
56
    def test_TOTAL_PHYMEM(self):
 
57
        sysctl_hwphymem = sysctl('sysctl hw.physmem')
 
58
        self.assertEqual(sysctl_hwphymem, psutil.TOTAL_PHYMEM)
 
59
 
 
60
    def test_BOOT_TIME(self):
 
61
        s = sysctl('sysctl kern.boottime')
 
62
        s = s[s.find(" sec = ") + 7:]
 
63
        s = s[:s.find(',')]
 
64
        btime = int(s)
 
65
        self.assertEqual(btime, psutil.BOOT_TIME)
 
66
 
 
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")
 
75
                   ))
 
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
 
82
        # if we go over it.
 
83
        if difference > (0.5 * 2**20):
 
84
            self.fail("sysctl=%s; psutil=%s; difference=%s;" %(
 
85
                      sysctl_avail_phymem, psutil_avail_phymem, difference))
 
86
 
 
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)
 
99
 
 
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))
 
107
 
 
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))
 
124
 
 
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)
 
136
 
 
137
 
 
138
if __name__ == '__main__':
 
139
    test_suite = unittest.TestSuite()
 
140
    test_suite.addTest(unittest.makeSuite(BSDSpecificTestCase))
 
141
    unittest.TextTestRunner(verbosity=2).run(test_suite)
 
142
 
 
143
 
 
144
 
 
145