| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
import multiprocessing |
| 6 |
import time |
| 7 |
import unittest |
| 8 |
|
| 9 |
import mozunit |
| 10 |
from six import integer_types |
| 11 |
|
| 12 |
try: |
| 13 |
import psutil |
| 14 |
except ImportError: |
| 15 |
psutil = None |
| 16 |
|
| 17 |
from mozsystemmonitor.resourcemonitor import SystemResourceMonitor, SystemResourceUsage |
| 18 |
|
| 19 |
|
| 20 |
@unittest.skipIf(psutil is None, "Resource monitor requires psutil.") |
| 21 |
class TestResourceMonitor(unittest.TestCase): |
| 22 |
def test_basic(self): |
| 23 |
monitor = SystemResourceMonitor(poll_interval=0.5) |
| 24 |
|
| 25 |
monitor.start() |
| 26 |
time.sleep(3) |
| 27 |
|
| 28 |
monitor.stop() |
| 29 |
|
| 30 |
data = list(monitor.range_usage()) |
| 31 |
self.assertGreater(len(data), 3) |
| 32 |
|
| 33 |
self.assertIsInstance(data[0], SystemResourceUsage) |
| 34 |
|
| 35 |
def test_empty(self): |
| 36 |
monitor = SystemResourceMonitor(poll_interval=2.0) |
| 37 |
monitor.start() |
| 38 |
monitor.stop() |
| 39 |
|
| 40 |
data = list(monitor.range_usage()) |
| 41 |
self.assertEqual(len(data), 0) |
| 42 |
|
| 43 |
def test_phases(self): |
| 44 |
monitor = SystemResourceMonitor(poll_interval=0.25) |
| 45 |
|
| 46 |
monitor.start() |
| 47 |
time.sleep(1) |
| 48 |
|
| 49 |
with monitor.phase("phase1"): |
| 50 |
time.sleep(1) |
| 51 |
|
| 52 |
with monitor.phase("phase2"): |
| 53 |
time.sleep(1) |
| 54 |
|
| 55 |
monitor.stop() |
| 56 |
|
| 57 |
self.assertEqual(len(monitor.phases), 2) |
| 58 |
self.assertEqual(["phase2", "phase1"], list(monitor.phases.keys())) |
| 59 |
|
| 60 |
all = list(monitor.range_usage()) |
| 61 |
data1 = list(monitor.phase_usage("phase1")) |
| 62 |
data2 = list(monitor.phase_usage("phase2")) |
| 63 |
|
| 64 |
self.assertGreater(len(all), len(data1)) |
| 65 |
self.assertGreater(len(data1), len(data2)) |
| 66 |
|
| 67 |
# This could fail if time.monotonic() takes more than 0.1s. It really |
| 68 |
# shouldn't. |
| 69 |
self.assertAlmostEqual(data1[-1].end, data2[-1].end, delta=0.25) |
| 70 |
|
| 71 |
def test_no_data(self): |
| 72 |
monitor = SystemResourceMonitor() |
| 73 |
|
| 74 |
data = list(monitor.range_usage()) |
| 75 |
self.assertEqual(len(data), 0) |
| 76 |
|
| 77 |
def test_events(self): |
| 78 |
monitor = SystemResourceMonitor(poll_interval=0.25) |
| 79 |
|
| 80 |
monitor.start() |
| 81 |
time.sleep(0.5) |
| 82 |
|
| 83 |
t0 = time.monotonic() |
| 84 |
monitor.record_event("t0") |
| 85 |
time.sleep(2) |
| 86 |
|
| 87 |
monitor.record_event("t1") |
| 88 |
time.sleep(0.5) |
| 89 |
monitor.stop() |
| 90 |
|
| 91 |
events = monitor.events |
| 92 |
self.assertEqual(len(events), 2) |
| 93 |
|
| 94 |
event = events[0] |
| 95 |
|
| 96 |
self.assertEqual(event[1], "t0") |
| 97 |
self.assertAlmostEqual(event[0], t0, delta=0.25) |
| 98 |
|
| 99 |
data = list(monitor.between_events_usage("t0", "t1")) |
| 100 |
self.assertGreater(len(data), 0) |
| 101 |
|
| 102 |
def test_aggregate_cpu(self): |
| 103 |
monitor = SystemResourceMonitor(poll_interval=0.25) |
| 104 |
|
| 105 |
monitor.start() |
| 106 |
time.sleep(1) |
| 107 |
monitor.stop() |
| 108 |
|
| 109 |
values = monitor.aggregate_cpu_percent() |
| 110 |
self.assertIsInstance(values, list) |
| 111 |
self.assertEqual(len(values), multiprocessing.cpu_count()) |
| 112 |
for v in values: |
| 113 |
self.assertIsInstance(v, float) |
| 114 |
|
| 115 |
value = monitor.aggregate_cpu_percent(per_cpu=False) |
| 116 |
self.assertIsInstance(value, float) |
| 117 |
|
| 118 |
values = monitor.aggregate_cpu_times() |
| 119 |
self.assertIsInstance(values, list) |
| 120 |
self.assertGreater(len(values), 0) |
| 121 |
self.assertTrue(hasattr(values[0], "user")) |
| 122 |
|
| 123 |
t = type(values[0]) |
| 124 |
|
| 125 |
value = monitor.aggregate_cpu_times(per_cpu=False) |
| 126 |
self.assertIsInstance(value, t) |
| 127 |
|
| 128 |
def test_aggregate_io(self): |
| 129 |
monitor = SystemResourceMonitor(poll_interval=0.25) |
| 130 |
|
| 131 |
# There's really no easy way to ensure I/O occurs. For all we know |
| 132 |
# reads and writes will all be serviced by the page cache. |
| 133 |
monitor.start() |
| 134 |
time.sleep(1.0) |
| 135 |
monitor.stop() |
| 136 |
|
| 137 |
values = monitor.aggregate_io() |
| 138 |
self.assertTrue(hasattr(values, "read_count")) |
| 139 |
|
| 140 |
def test_memory(self): |
| 141 |
monitor = SystemResourceMonitor(poll_interval=0.25) |
| 142 |
|
| 143 |
monitor.start() |
| 144 |
time.sleep(1.0) |
| 145 |
monitor.stop() |
| 146 |
|
| 147 |
v = monitor.min_memory_available() |
| 148 |
self.assertIsInstance(v, integer_types) |
| 149 |
|
| 150 |
v = monitor.max_memory_percent() |
| 151 |
self.assertIsInstance(v, float) |
| 152 |
|
| 153 |
def test_as_dict(self): |
| 154 |
monitor = SystemResourceMonitor(poll_interval=0.25) |
| 155 |
|
| 156 |
monitor.start() |
| 157 |
time.sleep(0.1) |
| 158 |
monitor.begin_phase("phase1") |
| 159 |
monitor.record_event("foo") |
| 160 |
time.sleep(0.1) |
| 161 |
monitor.begin_phase("phase2") |
| 162 |
monitor.record_event("bar") |
| 163 |
time.sleep(0.2) |
| 164 |
monitor.finish_phase("phase1") |
| 165 |
time.sleep(0.2) |
| 166 |
monitor.finish_phase("phase2") |
| 167 |
time.sleep(0.4) |
| 168 |
monitor.stop() |
| 169 |
|
| 170 |
d = monitor.as_dict() |
| 171 |
|
| 172 |
self.assertEqual(d["version"], 2) |
| 173 |
self.assertEqual(len(d["events"]), 2) |
| 174 |
self.assertEqual(len(d["phases"]), 2) |
| 175 |
self.assertIn("system", d) |
| 176 |
self.assertIsInstance(d["system"], dict) |
| 177 |
self.assertIsInstance(d["overall"], dict) |
| 178 |
self.assertIn("duration", d["overall"]) |
| 179 |
self.assertIn("cpu_times", d["overall"]) |
| 180 |
|
| 181 |
|
| 182 |
if __name__ == "__main__": |
| 183 |
mozunit.main() |