| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import subprocess |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
import mozunit |
| 7 |
import pytest |
| 8 |
from mozpower import MozPower |
| 9 |
from mozpower.mozpower import MissingProcessorInfoError, OsCpuComboMissingError |
| 10 |
|
| 11 |
|
| 12 |
def test_mozpower_android_init_failure(): |
| 13 |
"""Tests that the MozPower object fails when the android |
| 14 |
flag is set. Remove this test once android is implemented. |
| 15 |
""" |
| 16 |
with pytest.raises(NotImplementedError): |
| 17 |
MozPower(android=True) |
| 18 |
|
| 19 |
|
| 20 |
def test_mozpower_oscpu_combo_missing_error(): |
| 21 |
"""Tests that the error OsCpuComboMissingError is raised |
| 22 |
when we can't find a OS, and CPU combination (and, therefore, cannot |
| 23 |
find a power measurer). |
| 24 |
""" |
| 25 |
with mock.patch.object( |
| 26 |
MozPower, "_get_os", return_value="Not-An-OS" |
| 27 |
) as _, mock.patch.object( |
| 28 |
MozPower, "_get_processor_info", return_value="Not-A-Processor" |
| 29 |
) as _: |
| 30 |
with pytest.raises(OsCpuComboMissingError): |
| 31 |
MozPower() |
| 32 |
|
| 33 |
|
| 34 |
def test_mozpower_processor_info_missing_error(): |
| 35 |
"""Tests that the error MissingProcessorInfoError is raised |
| 36 |
when failures occur during processor information parsing. |
| 37 |
""" |
| 38 |
builtins_name = "builtins" |
| 39 |
|
| 40 |
def os_side_effect_true(*args, **kwargs): |
| 41 |
"""Used as a passing side effect for os.path.exists calls.""" |
| 42 |
return True |
| 43 |
|
| 44 |
def os_side_effect_false(*args, **kwargs): |
| 45 |
"""Used as a failing side effect for os.path.exists calls.""" |
| 46 |
return False |
| 47 |
|
| 48 |
def subprocess_side_effect_fail(*args, **kwargs): |
| 49 |
"""Used to mock a failure in subprocess.check_output calls.""" |
| 50 |
raise subprocess.CalledProcessError(1, "Testing failure") |
| 51 |
|
| 52 |
# Test failures in macos processor information parsing |
| 53 |
with mock.patch.object(MozPower, "_get_os", return_value="Darwin") as _: |
| 54 |
with mock.patch("os.path.exists") as os_mock: |
| 55 |
os_mock.side_effect = os_side_effect_false |
| 56 |
|
| 57 |
# Check that we fail properly if the processor |
| 58 |
# information file doesn't exist. |
| 59 |
with pytest.raises(MissingProcessorInfoError): |
| 60 |
MozPower() |
| 61 |
|
| 62 |
# Check that we fail properly when an error occurs |
| 63 |
# in the subprocess call. |
| 64 |
os_mock.side_effect = os_side_effect_true |
| 65 |
with mock.patch("subprocess.check_output") as subprocess_mock: |
| 66 |
subprocess_mock.side_effect = subprocess_side_effect_fail |
| 67 |
with pytest.raises(MissingProcessorInfoError): |
| 68 |
MozPower() |
| 69 |
|
| 70 |
# Test failures in linux processor information parsing |
| 71 |
with mock.patch.object(MozPower, "_get_os", return_value="Linux") as _: |
| 72 |
with mock.patch("os.path.exists") as os_mock: |
| 73 |
os_mock.side_effect = os_side_effect_false |
| 74 |
|
| 75 |
# Check that we fail properly if the processor |
| 76 |
# information file doesn't exist. |
| 77 |
with pytest.raises(MissingProcessorInfoError): |
| 78 |
MozPower() |
| 79 |
|
| 80 |
# Check that we fail properly when the model cannot be found |
| 81 |
# with by searching for 'model name'. |
| 82 |
os_mock.side_effect = os_side_effect_true |
| 83 |
with mock.patch( |
| 84 |
"%s.open" % builtins_name, mock.mock_open(read_data="") |
| 85 |
) as _: |
| 86 |
with pytest.raises(MissingProcessorInfoError): |
| 87 |
MozPower() |
| 88 |
|
| 89 |
|
| 90 |
def test_mozpower_oscpu_combo(mozpower_obj): |
| 91 |
"""Tests that the correct class is instantiated for a given |
| 92 |
OS and CPU combination (MacIntelPower in this case). |
| 93 |
""" |
| 94 |
assert mozpower_obj.measurer.__class__.__name__ == "MacIntelPower" |
| 95 |
assert ( |
| 96 |
mozpower_obj.measurer._os == "darwin" and mozpower_obj.measurer._cpu == "intel" |
| 97 |
) |
| 98 |
|
| 99 |
|
| 100 |
def test_mozpower_measuring(mozpower_obj): |
| 101 |
"""Tests that measurers are properly called with each method.""" |
| 102 |
with mock.patch( |
| 103 |
"mozpower.macintelpower.MacIntelPower.initialize_power_measurements" |
| 104 |
) as _, mock.patch( |
| 105 |
"mozpower.macintelpower.MacIntelPower.finalize_power_measurements" |
| 106 |
) as _, mock.patch( |
| 107 |
"mozpower.macintelpower.MacIntelPower.get_perfherder_data" |
| 108 |
) as _: |
| 109 |
mozpower_obj.initialize_power_measurements() |
| 110 |
mozpower_obj.measurer.initialize_power_measurements.assert_called() |
| 111 |
|
| 112 |
mozpower_obj.finalize_power_measurements() |
| 113 |
mozpower_obj.measurer.finalize_power_measurements.assert_called() |
| 114 |
|
| 115 |
mozpower_obj.get_perfherder_data() |
| 116 |
mozpower_obj.measurer.get_perfherder_data.assert_called() |
| 117 |
|
| 118 |
|
| 119 |
def test_mozpower_measuring_with_no_measurer(mozpower_obj): |
| 120 |
"""Tests that no errors occur when the measurer is None, and the |
| 121 |
initialize, finalize, and get_perfherder_data functions are called. |
| 122 |
""" |
| 123 |
with mock.patch( |
| 124 |
"mozpower.macintelpower.MacIntelPower.initialize_power_measurements" |
| 125 |
) as _, mock.patch( |
| 126 |
"mozpower.macintelpower.MacIntelPower.finalize_power_measurements" |
| 127 |
) as _, mock.patch( |
| 128 |
"mozpower.macintelpower.MacIntelPower.get_perfherder_data" |
| 129 |
) as _: |
| 130 |
measurer = mozpower_obj.measurer |
| 131 |
mozpower_obj.measurer = None |
| 132 |
|
| 133 |
mozpower_obj.initialize_power_measurements() |
| 134 |
assert not measurer.initialize_power_measurements.called |
| 135 |
|
| 136 |
mozpower_obj.finalize_power_measurements() |
| 137 |
assert not measurer.finalize_power_measurements.called |
| 138 |
|
| 139 |
mozpower_obj.get_perfherder_data() |
| 140 |
assert not measurer.get_perfherder_data.called |
| 141 |
|
| 142 |
mozpower_obj.get_full_perfherder_data("mozpower") |
| 143 |
assert not measurer.get_perfherder_data.called |
| 144 |
|
| 145 |
|
| 146 |
def test_mozpower_get_full_perfherder_data(mozpower_obj): |
| 147 |
"""Tests that the full perfherder data blob is properly |
| 148 |
produced given a partial perfherder data blob with correct |
| 149 |
entries. |
| 150 |
""" |
| 151 |
partial_perfherder = { |
| 152 |
"utilization": { |
| 153 |
"type": "power", |
| 154 |
"test": "mozpower", |
| 155 |
"unit": "%", |
| 156 |
"values": {"cpu": 50, "gpu": 0}, |
| 157 |
}, |
| 158 |
"power-usage": { |
| 159 |
"type": "power", |
| 160 |
"test": "mozpower", |
| 161 |
"unit": "mWh", |
| 162 |
"values": {"cpu": 2.0, "dram": 0.1, "gpu": 4.0}, |
| 163 |
}, |
| 164 |
"frequency-cpu": { |
| 165 |
"type": "power", |
| 166 |
"test": "mozpower", |
| 167 |
"unit": "MHz", |
| 168 |
"values": { |
| 169 |
"cpu-favg": 2.0, |
| 170 |
"cpu-fmax": 5.0, |
| 171 |
"cpu-fmin": 0.0, |
| 172 |
}, |
| 173 |
}, |
| 174 |
"frequency-gpu": { |
| 175 |
"type": "power", |
| 176 |
"test": "mozpower", |
| 177 |
"unit": "MHz", |
| 178 |
"values": {"gpu-favg": 3.0, "gpu-fmax": 6.0, "gpu-fmin": 0.0}, |
| 179 |
}, |
| 180 |
} |
| 181 |
utilization_vals = [0, 50] |
| 182 |
power_usage_vals = [2.0, 0.1, 4.0] |
| 183 |
frequency_cpu_vals = [2.0, 5.0, 0.0] |
| 184 |
frequency_gpu_vals = [3.0, 6.0, 0.0] |
| 185 |
|
| 186 |
with mock.patch("mozpower.macintelpower.MacIntelPower.get_perfherder_data") as gpd: |
| 187 |
gpd.return_value = partial_perfherder |
| 188 |
|
| 189 |
full_perfherder = mozpower_obj.get_full_perfherder_data("mozpower") |
| 190 |
assert full_perfherder["framework"]["name"] == "mozpower" |
| 191 |
assert len(full_perfherder["suites"]) == 4 |
| 192 |
|
| 193 |
# Check that each of the two suites were created correctly. |
| 194 |
suites = full_perfherder["suites"] |
| 195 |
for suite in suites: |
| 196 |
assert "subtests" in suite |
| 197 |
|
| 198 |
assert suite["type"] == "power" |
| 199 |
assert suite["alertThreshold"] == 2.0 |
| 200 |
assert suite["lowerIsBetter"] |
| 201 |
|
| 202 |
all_vals = [] |
| 203 |
for subtest in suite["subtests"]: |
| 204 |
assert "value" in subtest |
| 205 |
|
| 206 |
# Check that the subtest names were created correctly |
| 207 |
if "utilization" in suite["name"]: |
| 208 |
assert "utilization" in subtest["name"] |
| 209 |
elif "power-usage" in suite["name"]: |
| 210 |
assert "power-usage" in subtest["name"] |
| 211 |
elif "frequency-cpu" in suite["name"]: |
| 212 |
assert "frequency-cpu" in subtest["name"] |
| 213 |
elif "frequency-gpu" in suite["name"]: |
| 214 |
assert "frequency-gpu" in subtest["name"] |
| 215 |
else: |
| 216 |
assert False, "Unknown subtest name %s" % subtest["name"] |
| 217 |
|
| 218 |
all_vals.append(subtest["value"]) |
| 219 |
|
| 220 |
if "utilization" in suite["name"]: |
| 221 |
assert len(all_vals) == 2 |
| 222 |
assert suite["unit"] == "%" |
| 223 |
assert suite["name"] == "mozpower-utilization" |
| 224 |
assert not list(set(all_vals) - set(utilization_vals)) |
| 225 |
assert suite["value"] == float(25) |
| 226 |
elif "power-usage" in suite["name"]: |
| 227 |
assert len(all_vals) == 3 |
| 228 |
assert suite["unit"] == "mWh" |
| 229 |
assert suite["name"] == "mozpower-power-usage" |
| 230 |
assert not list(set(all_vals) - set(power_usage_vals)) |
| 231 |
assert suite["value"] == 6.1 |
| 232 |
elif "frequency-cpu" in suite["name"]: |
| 233 |
assert len(all_vals) == 3 |
| 234 |
assert suite["unit"] == "MHz" |
| 235 |
assert suite["name"] == "mozpower-frequency-cpu" |
| 236 |
assert not list(set(all_vals) - set(frequency_cpu_vals)) |
| 237 |
assert suite["value"] == 2.0 |
| 238 |
elif "frequency-gpu" in suite["name"]: |
| 239 |
assert len(all_vals) == 3 |
| 240 |
assert suite["unit"] == "MHz" |
| 241 |
assert suite["name"] == "mozpower-frequency-gpu" |
| 242 |
assert not list(set(all_vals) - set(frequency_gpu_vals)) |
| 243 |
assert suite["value"] == 3.0 |
| 244 |
else: |
| 245 |
assert False, "Unknown suite name %s" % suite["name"] |
| 246 |
|
| 247 |
|
| 248 |
if __name__ == "__main__": |
| 249 |
mozunit.main() |