jadmanski | a9894d0 | 2009-08-21 16:49:48 +0000 | [diff] [blame] | 1 | #!/usr/bin/python |
Aviv Keshet | 39164ca | 2013-03-27 15:08:33 -0700 | [diff] [blame] | 2 | #pylint: disable-msg=C0111 |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 3 | """Unit Tests for autotest.client.common_lib.test""" |
| 4 | |
| 5 | __author__ = 'gps@google.com (Gregory P. Smith)' |
| 6 | |
Keith Haddow | d804bc8 | 2016-03-15 17:16:03 -0700 | [diff] [blame] | 7 | import json |
| 8 | import tempfile |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 9 | import unittest |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 10 | import common |
Scott Zawalski | 91493c8 | 2013-01-25 16:15:20 -0500 | [diff] [blame] | 11 | from autotest_lib.client.common_lib import test |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 12 | from autotest_lib.client.common_lib.test_utils import mock |
Aviv Keshet | 39164ca | 2013-03-27 15:08:33 -0700 | [diff] [blame] | 13 | from autotest_lib.client.common_lib import error as common_lib_error |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 14 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 15 | class TestTestCase(unittest.TestCase): |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 16 | class _neutered_base_test(test.base_test): |
| 17 | """A child class of base_test to avoid calling the constructor.""" |
| 18 | def __init__(self, *args, **kwargs): |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 19 | class MockJob(object): |
| 20 | pass |
| 21 | class MockProfilerManager(object): |
| 22 | def active(self): |
| 23 | return False |
mbligh | c6bf601 | 2009-10-02 00:02:15 +0000 | [diff] [blame] | 24 | def present(self): |
| 25 | return True |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 26 | self.job = MockJob() |
showard | a6082ef | 2009-10-12 20:25:44 +0000 | [diff] [blame] | 27 | self.job.default_profile_only = False |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 28 | self.job.profilers = MockProfilerManager() |
Scott Zawalski | 91493c8 | 2013-01-25 16:15:20 -0500 | [diff] [blame] | 29 | self.job.test_retry = 0 |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 30 | self._new_keyval = False |
mbligh | 5e703a2 | 2009-06-15 22:00:12 +0000 | [diff] [blame] | 31 | self.iteration = 0 |
mbligh | 742ae42 | 2009-05-13 20:46:41 +0000 | [diff] [blame] | 32 | self.before_iteration_hooks = [] |
| 33 | self.after_iteration_hooks = [] |
| 34 | |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 35 | |
| 36 | def setUp(self): |
| 37 | self.god = mock.mock_god() |
| 38 | self.test = self._neutered_base_test() |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | def tearDown(self): |
| 42 | self.god.unstub_all() |
| 43 | |
| 44 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 45 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 46 | class Test_base_test_execute(TestTestCase): |
| 47 | # Test the various behaviors of the base_test.execute() method. |
| 48 | def setUp(self): |
| 49 | TestTestCase.setUp(self) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 50 | self.god.stub_function(self.test, 'run_once_profiling') |
| 51 | self.god.stub_function(self.test, 'postprocess') |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 52 | self.god.stub_function(self.test, 'process_failed_constraints') |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 53 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 54 | |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 55 | def test_call_run_once(self): |
| 56 | # setup |
| 57 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 58 | self.god.stub_function(self.test, 'run_once') |
| 59 | self.god.stub_function(self.test, 'postprocess_iteration') |
mbligh | 7af0997 | 2009-04-17 22:17:08 +0000 | [diff] [blame] | 60 | self.god.stub_function(self.test, 'analyze_perf_constraints') |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 61 | before_hook = self.god.create_mock_function('before_hook') |
| 62 | after_hook = self.god.create_mock_function('after_hook') |
mbligh | 742ae42 | 2009-05-13 20:46:41 +0000 | [diff] [blame] | 63 | self.test.register_before_iteration_hook(before_hook) |
| 64 | self.test.register_after_iteration_hook(after_hook) |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 65 | |
| 66 | # tests the test._call_run_once implementation |
| 67 | self.test.drop_caches_between_iterations.expect_call() |
showard | d4ead17 | 2009-05-01 00:08:56 +0000 | [diff] [blame] | 68 | before_hook.expect_call(self.test) |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 69 | self.test.run_once.expect_call(1, 2, arg='val') |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 70 | self.test.postprocess_iteration.expect_call() |
mbligh | 7af0997 | 2009-04-17 22:17:08 +0000 | [diff] [blame] | 71 | self.test.analyze_perf_constraints.expect_call([]) |
Eric Li | daf6ff0 | 2011-03-01 15:31:31 -0800 | [diff] [blame] | 72 | after_hook.expect_call(self.test) |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 73 | self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'}) |
showard | d4ead17 | 2009-05-01 00:08:56 +0000 | [diff] [blame] | 74 | self.god.check_playback() |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 75 | |
| 76 | |
Eric Li | daf6ff0 | 2011-03-01 15:31:31 -0800 | [diff] [blame] | 77 | def test_call_run_once_with_exception(self): |
| 78 | # setup |
| 79 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 80 | self.god.stub_function(self.test, 'run_once') |
| 81 | before_hook = self.god.create_mock_function('before_hook') |
| 82 | after_hook = self.god.create_mock_function('after_hook') |
| 83 | self.test.register_before_iteration_hook(before_hook) |
| 84 | self.test.register_after_iteration_hook(after_hook) |
| 85 | error = Exception('fail') |
| 86 | |
| 87 | # tests the test._call_run_once implementation |
| 88 | self.test.drop_caches_between_iterations.expect_call() |
| 89 | before_hook.expect_call(self.test) |
| 90 | self.test.run_once.expect_call(1, 2, arg='val').and_raises(error) |
| 91 | after_hook.expect_call(self.test) |
| 92 | try: |
| 93 | self.test._call_run_once([], False, None, (1, 2), {'arg': 'val'}) |
| 94 | except: |
| 95 | pass |
| 96 | self.god.check_playback() |
| 97 | |
| 98 | |
Scott Zawalski | 91493c8 | 2013-01-25 16:15:20 -0500 | [diff] [blame] | 99 | def _setup_failed_test_calls(self, fail_count, error): |
| 100 | """ |
| 101 | Set up failed test calls for use with call_run_once_with_retry. |
| 102 | |
| 103 | @param fail_count: The amount of times to mock a failure. |
| 104 | @param error: The error to raise while failing. |
| 105 | """ |
| 106 | self.god.stub_function(self.test.job, 'record') |
| 107 | self.god.stub_function(self.test, '_call_run_once') |
| 108 | # tests the test._call_run_once implementation |
| 109 | for run in xrange(0, fail_count): |
| 110 | self.test._call_run_once.expect_call([], False, None, (1, 2), |
| 111 | {'arg': 'val'}).and_raises( |
| 112 | error) |
| 113 | info_str = 'Run %s failed with %s' % (run, error) |
| 114 | # On the final run we do not emit this message. |
Aviv Keshet | 39164ca | 2013-03-27 15:08:33 -0700 | [diff] [blame] | 115 | if run != self.test.job.test_retry and isinstance(error, |
| 116 | common_lib_error.TestFailRetry): |
Scott Zawalski | 91493c8 | 2013-01-25 16:15:20 -0500 | [diff] [blame] | 117 | self.test.job.record.expect_call('INFO', None, None, info_str) |
| 118 | |
| 119 | |
| 120 | def test_call_run_once_with_retry_exception(self): |
| 121 | """ |
| 122 | Test call_run_once_with_retry duplicating a test that will always fail. |
| 123 | """ |
| 124 | self.test.job.test_retry = 5 |
| 125 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 126 | self.god.stub_function(self.test, 'run_once') |
| 127 | before_hook = self.god.create_mock_function('before_hook') |
| 128 | after_hook = self.god.create_mock_function('after_hook') |
| 129 | self.test.register_before_iteration_hook(before_hook) |
| 130 | self.test.register_after_iteration_hook(after_hook) |
Aviv Keshet | 39164ca | 2013-03-27 15:08:33 -0700 | [diff] [blame] | 131 | error = common_lib_error.TestFailRetry('fail') |
Scott Zawalski | 91493c8 | 2013-01-25 16:15:20 -0500 | [diff] [blame] | 132 | self._setup_failed_test_calls(self.test.job.test_retry+1, error) |
| 133 | try: |
| 134 | self.test._call_run_once_with_retry([], False, None, (1, 2), |
| 135 | {'arg': 'val'}) |
| 136 | except Exception as err: |
| 137 | if err != error: |
| 138 | raise |
| 139 | self.god.check_playback() |
| 140 | |
| 141 | |
Aviv Keshet | 39164ca | 2013-03-27 15:08:33 -0700 | [diff] [blame] | 142 | def test_call_run_once_with_retry_exception_unretryable(self): |
| 143 | """ |
| 144 | Test call_run_once_with_retry duplicating a test that will always fail |
| 145 | with a non-retryable exception. |
| 146 | """ |
| 147 | self.test.job.test_retry = 5 |
| 148 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 149 | self.god.stub_function(self.test, 'run_once') |
| 150 | before_hook = self.god.create_mock_function('before_hook') |
| 151 | after_hook = self.god.create_mock_function('after_hook') |
| 152 | self.test.register_before_iteration_hook(before_hook) |
| 153 | self.test.register_after_iteration_hook(after_hook) |
| 154 | error = common_lib_error.TestFail('fail') |
| 155 | self._setup_failed_test_calls(1, error) |
| 156 | try: |
| 157 | self.test._call_run_once_with_retry([], False, None, (1, 2), |
| 158 | {'arg': 'val'}) |
| 159 | except Exception as err: |
| 160 | if err != error: |
| 161 | raise |
| 162 | self.god.check_playback() |
| 163 | |
| 164 | |
Scott Zawalski | 91493c8 | 2013-01-25 16:15:20 -0500 | [diff] [blame] | 165 | def test_call_run_once_with_retry_exception_and_pass(self): |
| 166 | """ |
| 167 | Test call_run_once_with_retry duplicating a test that fails at first |
| 168 | and later passes. |
| 169 | """ |
| 170 | # Stubbed out for the write_keyval call. |
| 171 | self.test.outputdir = '/tmp' |
Scott Zawalski | 91493c8 | 2013-01-25 16:15:20 -0500 | [diff] [blame] | 172 | |
| 173 | num_to_fail = 2 |
| 174 | self.test.job.test_retry = 5 |
| 175 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 176 | self.god.stub_function(self.test, 'run_once') |
| 177 | before_hook = self.god.create_mock_function('before_hook') |
| 178 | after_hook = self.god.create_mock_function('after_hook') |
| 179 | self.god.stub_function(self.test, '_call_run_once') |
| 180 | self.test.register_before_iteration_hook(before_hook) |
| 181 | self.test.register_after_iteration_hook(after_hook) |
| 182 | self.god.stub_function(self.test.job, 'record') |
| 183 | # tests the test._call_run_once implementation |
Aviv Keshet | 39164ca | 2013-03-27 15:08:33 -0700 | [diff] [blame] | 184 | error = common_lib_error.TestFailRetry('fail') |
Scott Zawalski | 91493c8 | 2013-01-25 16:15:20 -0500 | [diff] [blame] | 185 | self._setup_failed_test_calls(num_to_fail, error) |
| 186 | # Passing call |
| 187 | self.test._call_run_once.expect_call([], False, None, (1, 2), |
| 188 | {'arg': 'val'}) |
| 189 | self.test._call_run_once_with_retry([], False, None, (1, 2), |
| 190 | {'arg': 'val'}) |
| 191 | self.god.check_playback() |
| 192 | |
| 193 | |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 194 | def _expect_call_run_once(self): |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 195 | self.test._call_run_once.expect_call((), False, None, (), {}) |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 196 | |
| 197 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 198 | def test_execute_test_length(self): |
| 199 | # test that test_length overrides iterations and works. |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 200 | self.god.stub_function(self.test, '_call_run_once') |
| 201 | |
| 202 | self._expect_call_run_once() |
| 203 | self._expect_call_run_once() |
| 204 | self._expect_call_run_once() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 205 | self.test.run_once_profiling.expect_call(None) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 206 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 207 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 208 | |
| 209 | fake_time = iter(xrange(4)).next |
| 210 | self.test.execute(iterations=1, test_length=3, _get_time=fake_time) |
| 211 | self.god.check_playback() |
| 212 | |
| 213 | |
| 214 | def test_execute_iterations(self): |
| 215 | # test that iterations works. |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 216 | self.god.stub_function(self.test, '_call_run_once') |
| 217 | |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 218 | iterations = 2 |
| 219 | for _ in range(iterations): |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 220 | self._expect_call_run_once() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 221 | self.test.run_once_profiling.expect_call(None) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 222 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 223 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 224 | |
| 225 | self.test.execute(iterations=iterations) |
| 226 | self.god.check_playback() |
| 227 | |
| 228 | |
| 229 | def _mock_calls_for_execute_no_iterations(self): |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 230 | self.test.run_once_profiling.expect_call(None) |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 231 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 232 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 233 | |
| 234 | |
| 235 | def test_execute_iteration_zero(self): |
| 236 | # test that iterations=0 works. |
| 237 | self._mock_calls_for_execute_no_iterations() |
| 238 | |
| 239 | self.test.execute(iterations=0) |
| 240 | self.god.check_playback() |
| 241 | |
| 242 | |
| 243 | def test_execute_profile_only(self): |
mbligh | c6bf601 | 2009-10-02 00:02:15 +0000 | [diff] [blame] | 244 | # test that profile_only=True works. |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 245 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 246 | self.test.drop_caches_between_iterations.expect_call() |
| 247 | self.test.run_once_profiling.expect_call(None) |
| 248 | self.test.drop_caches_between_iterations.expect_call() |
| 249 | self.test.run_once_profiling.expect_call(None) |
| 250 | self.test.postprocess.expect_call() |
| 251 | self.test.process_failed_constraints.expect_call() |
mbligh | 4b835b8 | 2009-02-11 01:26:13 +0000 | [diff] [blame] | 252 | self.test.execute(profile_only=True, iterations=2) |
| 253 | self.god.check_playback() |
| 254 | |
| 255 | |
showard | a6082ef | 2009-10-12 20:25:44 +0000 | [diff] [blame] | 256 | def test_execute_default_profile_only(self): |
| 257 | # test that profile_only=True works. |
| 258 | self.god.stub_function(self.test, 'drop_caches_between_iterations') |
| 259 | for _ in xrange(3): |
| 260 | self.test.drop_caches_between_iterations.expect_call() |
| 261 | self.test.run_once_profiling.expect_call(None) |
| 262 | self.test.postprocess.expect_call() |
| 263 | self.test.process_failed_constraints.expect_call() |
| 264 | self.test.job.default_profile_only = True |
| 265 | self.test.execute(iterations=3) |
| 266 | self.god.check_playback() |
| 267 | |
| 268 | |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 269 | def test_execute_postprocess_profiled_false(self): |
| 270 | # test that postprocess_profiled_run=False works |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 271 | self.god.stub_function(self.test, '_call_run_once') |
| 272 | |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 273 | self.test._call_run_once.expect_call((), False, False, (), {}) |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 274 | self.test.run_once_profiling.expect_call(False) |
| 275 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 276 | self.test.process_failed_constraints.expect_call() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 277 | |
| 278 | self.test.execute(postprocess_profiled_run=False, iterations=1) |
| 279 | self.god.check_playback() |
| 280 | |
| 281 | |
| 282 | def test_execute_postprocess_profiled_true(self): |
| 283 | # test that postprocess_profiled_run=True works |
mbligh | 4395bbd | 2009-03-25 19:34:17 +0000 | [diff] [blame] | 284 | self.god.stub_function(self.test, '_call_run_once') |
| 285 | |
jadmanski | a93fbca | 2009-08-21 15:34:04 +0000 | [diff] [blame] | 286 | self.test._call_run_once.expect_call((), False, True, (), {}) |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 287 | self.test.run_once_profiling.expect_call(True) |
| 288 | self.test.postprocess.expect_call() |
mbligh | 32cb5b4 | 2009-05-01 23:05:09 +0000 | [diff] [blame] | 289 | self.test.process_failed_constraints.expect_call() |
mbligh | a49c5cb | 2009-02-26 01:01:09 +0000 | [diff] [blame] | 290 | |
| 291 | self.test.execute(postprocess_profiled_run=True, iterations=1) |
| 292 | self.god.check_playback() |
| 293 | |
| 294 | |
Keith Haddow | d804bc8 | 2016-03-15 17:16:03 -0700 | [diff] [blame] | 295 | def test_output_single_perf_value(self): |
| 296 | self.test.resultsdir = tempfile.mkdtemp() |
| 297 | |
| 298 | self.test.output_perf_value("Test", 1, units="ms", higher_is_better=True) |
| 299 | |
| 300 | f = open(self.test.resultsdir + "/results-chart.json") |
| 301 | expected_result = {"Test": {"summary": {"units": "ms", "type": "scalar", |
| 302 | "value": 1, "improvement_direction": "up"}}} |
| 303 | self.assertDictEqual(expected_result, json.loads(f.read())) |
| 304 | |
| 305 | |
| 306 | def test_output_single_perf_value_twice(self): |
| 307 | self.test.resultsdir = tempfile.mkdtemp() |
| 308 | |
| 309 | self.test.output_perf_value("Test", 1, units="ms", higher_is_better=True) |
| 310 | self.test.output_perf_value("Test", 2, units="ms", higher_is_better=True) |
| 311 | |
| 312 | f = open(self.test.resultsdir + "/results-chart.json") |
Keith Haddow | 4fc2465 | 2016-03-16 10:08:17 -0700 | [diff] [blame] | 313 | expected_result = {"Test": {"summary": {"units": "ms", |
| 314 | "type": "list_of_scalar_values", "values": [1, 2], |
| 315 | "improvement_direction": "up"}}} |
Keith Haddow | d804bc8 | 2016-03-15 17:16:03 -0700 | [diff] [blame] | 316 | self.assertDictEqual(expected_result, json.loads(f.read())) |
| 317 | |
| 318 | |
| 319 | def test_output_single_perf_value_three_times(self): |
| 320 | self.test.resultsdir = tempfile.mkdtemp() |
| 321 | |
Keith Haddow | 4fc2465 | 2016-03-16 10:08:17 -0700 | [diff] [blame] | 322 | self.test.output_perf_value("Test", 1, units="ms", |
| 323 | higher_is_better=True) |
Keith Haddow | d804bc8 | 2016-03-15 17:16:03 -0700 | [diff] [blame] | 324 | self.test.output_perf_value("Test", 2, units="ms", higher_is_better=True) |
| 325 | self.test.output_perf_value("Test", 3, units="ms", higher_is_better=True) |
| 326 | |
| 327 | f = open(self.test.resultsdir + "/results-chart.json") |
Keith Haddow | 4fc2465 | 2016-03-16 10:08:17 -0700 | [diff] [blame] | 328 | expected_result = {"Test": {"summary": {"units": "ms", |
| 329 | "type": "list_of_scalar_values", "values": [1, 2, 3], |
| 330 | "improvement_direction": "up"}}} |
Keith Haddow | d804bc8 | 2016-03-15 17:16:03 -0700 | [diff] [blame] | 331 | self.assertDictEqual(expected_result, json.loads(f.read())) |
| 332 | |
| 333 | |
| 334 | def test_output_list_perf_value(self): |
| 335 | self.test.resultsdir = tempfile.mkdtemp() |
| 336 | |
Keith Haddow | 4fc2465 | 2016-03-16 10:08:17 -0700 | [diff] [blame] | 337 | self.test.output_perf_value("Test", [1, 2, 3], units="ms", |
| 338 | higher_is_better=False) |
Keith Haddow | d804bc8 | 2016-03-15 17:16:03 -0700 | [diff] [blame] | 339 | |
| 340 | f = open(self.test.resultsdir + "/results-chart.json") |
Keith Haddow | 4fc2465 | 2016-03-16 10:08:17 -0700 | [diff] [blame] | 341 | expected_result = {"Test": {"summary": {"units": "ms", |
| 342 | "type": "list_of_scalar_values", "values": [1, 2, 3], |
| 343 | "improvement_direction": "down"}}} |
Keith Haddow | d804bc8 | 2016-03-15 17:16:03 -0700 | [diff] [blame] | 344 | self.assertDictEqual(expected_result, json.loads(f.read())) |
| 345 | |
| 346 | |
| 347 | def test_output_single_then_list_perf_value(self): |
| 348 | self.test.resultsdir = tempfile.mkdtemp() |
Keith Haddow | 4fc2465 | 2016-03-16 10:08:17 -0700 | [diff] [blame] | 349 | self.test.output_perf_value("Test", 1, units="ms", |
| 350 | higher_is_better=False) |
| 351 | self.test.output_perf_value("Test", [4, 3, 2], units="ms", |
| 352 | higher_is_better=False) |
Keith Haddow | d804bc8 | 2016-03-15 17:16:03 -0700 | [diff] [blame] | 353 | f = open(self.test.resultsdir + "/results-chart.json") |
Keith Haddow | 4fc2465 | 2016-03-16 10:08:17 -0700 | [diff] [blame] | 354 | expected_result = {"Test": {"summary": {"units": "ms", |
| 355 | "type": "list_of_scalar_values", |
| 356 | "values": [1, 4, 3, 2], |
| 357 | "improvement_direction": "down"}}} |
Keith Haddow | d804bc8 | 2016-03-15 17:16:03 -0700 | [diff] [blame] | 358 | self.assertDictEqual(expected_result, json.loads(f.read())) |
| 359 | |
| 360 | |
| 361 | def test_output_list_then_list_perf_value(self): |
| 362 | self.test.resultsdir = tempfile.mkdtemp() |
Keith Haddow | 4fc2465 | 2016-03-16 10:08:17 -0700 | [diff] [blame] | 363 | self.test.output_perf_value("Test", [1, 2, 3], units="ms", |
| 364 | higher_is_better=False) |
| 365 | self.test.output_perf_value("Test", [4, 3, 2], units="ms", |
| 366 | higher_is_better=False) |
Keith Haddow | d804bc8 | 2016-03-15 17:16:03 -0700 | [diff] [blame] | 367 | f = open(self.test.resultsdir + "/results-chart.json") |
Keith Haddow | 4fc2465 | 2016-03-16 10:08:17 -0700 | [diff] [blame] | 368 | expected_result = {"Test": {"summary": {"units": "ms", |
| 369 | "type": "list_of_scalar_values", |
| 370 | "values": [1, 2, 3, 4, 3, 2], |
| 371 | "improvement_direction": "down"}}} |
Keith Haddow | d804bc8 | 2016-03-15 17:16:03 -0700 | [diff] [blame] | 372 | self.assertDictEqual(expected_result, json.loads(f.read())) |
| 373 | |
| 374 | |
Keith Haddow | 4fc2465 | 2016-03-16 10:08:17 -0700 | [diff] [blame] | 375 | def test_output_single_perf_value_input_string(self): |
| 376 | self.test.resultsdir = tempfile.mkdtemp() |
| 377 | |
| 378 | self.test.output_perf_value("Test", u'-0.34', units="ms", |
| 379 | higher_is_better=True) |
| 380 | |
| 381 | f = open(self.test.resultsdir + "/results-chart.json") |
| 382 | expected_result = {"Test": {"summary": {"units": "ms", "type": "scalar", |
| 383 | "value": -0.34, "improvement_direction": "up"}}} |
| 384 | self.assertDictEqual(expected_result, json.loads(f.read())) |
| 385 | |
| 386 | |
| 387 | def test_output_single_perf_value_input_list_of_string(self): |
| 388 | self.test.resultsdir = tempfile.mkdtemp() |
| 389 | |
| 390 | self.test.output_perf_value("Test", [0, u'-0.34', 1], units="ms", |
| 391 | higher_is_better=True) |
| 392 | |
| 393 | f = open(self.test.resultsdir + "/results-chart.json") |
| 394 | expected_result = {"Test": {"summary": {"units": "ms", |
| 395 | "type": "list_of_scalar_values", |
| 396 | "values": [0, -0.34, 1], |
| 397 | "improvement_direction": "up"}}} |
| 398 | self.assertDictEqual(expected_result, json.loads(f.read())) |
| 399 | |
Po-Hsien Wang | 3c4fc6f | 2017-05-20 12:11:58 -0700 | [diff] [blame] | 400 | def test_output_list_then_replace_list_perf_value(self): |
| 401 | self.test.resultsdir = tempfile.mkdtemp() |
| 402 | self.test.output_perf_value("Test", [1, 2, 3], units="ms", |
| 403 | higher_is_better=False) |
| 404 | self.test.output_perf_value("Test", [4, 5, 6], units="ms", |
| 405 | higher_is_better=False, |
| 406 | replace_existing_values=True) |
| 407 | f = open(self.test.resultsdir + "/results-chart.json") |
| 408 | expected_result = {"Test": {"summary": {"units": "ms", |
| 409 | "type": "list_of_scalar_values", |
| 410 | "values": [4, 5, 6], |
| 411 | "improvement_direction": "down"}}} |
| 412 | self.assertDictEqual(expected_result, json.loads(f.read())) |
| 413 | |
| 414 | def test_output_single_then_replace_list_perf_value(self): |
| 415 | self.test.resultsdir = tempfile.mkdtemp() |
| 416 | self.test.output_perf_value("Test", 3, units="ms", |
| 417 | higher_is_better=False) |
| 418 | self.test.output_perf_value("Test", [4, 5, 6], units="ms", |
| 419 | higher_is_better=False, |
| 420 | replace_existing_values=True) |
| 421 | f = open(self.test.resultsdir + "/results-chart.json") |
| 422 | expected_result = {"Test": {"summary": {"units": "ms", |
| 423 | "type": "list_of_scalar_values", |
| 424 | "values": [4, 5, 6], |
| 425 | "improvement_direction": "down"}}} |
| 426 | self.assertDictEqual(expected_result, json.loads(f.read())) |
| 427 | |
| 428 | def test_output_list_then_replace_single_perf_value(self): |
| 429 | self.test.resultsdir = tempfile.mkdtemp() |
| 430 | self.test.output_perf_value("Test", [1,2,3], units="ms", |
| 431 | higher_is_better=False) |
| 432 | self.test.output_perf_value("Test", 4, units="ms", |
| 433 | higher_is_better=False, |
| 434 | replace_existing_values=True) |
| 435 | f = open(self.test.resultsdir + "/results-chart.json") |
| 436 | expected_result = {"Test": {"summary": {"units": "ms", |
| 437 | "type": "scalar", |
| 438 | "value": 4, |
| 439 | "improvement_direction": "down"}}} |
| 440 | self.assertDictEqual(expected_result, json.loads(f.read())) |
| 441 | |
| 442 | def test_output_single_then_replace_single_perf_value(self): |
| 443 | self.test.resultsdir = tempfile.mkdtemp() |
| 444 | self.test.output_perf_value("Test", 1, units="ms", |
| 445 | higher_is_better=False) |
| 446 | self.test.output_perf_value("Test", 2, units="ms", |
| 447 | higher_is_better=False, |
| 448 | replace_existing_values=True) |
| 449 | f = open(self.test.resultsdir + "/results-chart.json") |
| 450 | expected_result = {"Test": {"summary": {"units": "ms", |
| 451 | "type": "scalar", |
| 452 | "value": 2, |
| 453 | "improvement_direction": "down"}}} |
| 454 | self.assertDictEqual(expected_result, json.loads(f.read())) |
| 455 | |
| 456 | def test_output_perf_then_replace_certain_perf_value(self): |
| 457 | self.test.resultsdir = tempfile.mkdtemp() |
| 458 | self.test.output_perf_value("Test1", 1, units="ms", |
| 459 | higher_is_better=False) |
| 460 | self.test.output_perf_value("Test2", 2, units="ms", |
| 461 | higher_is_better=False) |
| 462 | self.test.output_perf_value("Test3", 3, units="ms", |
| 463 | higher_is_better=False) |
| 464 | self.test.output_perf_value("Test2", -1, units="ms", |
| 465 | higher_is_better=False, |
| 466 | replace_existing_values=True) |
| 467 | f = open(self.test.resultsdir + "/results-chart.json") |
| 468 | expected_result = {"Test1": {"summary": |
| 469 | {"units": "ms", |
| 470 | "type": "scalar", |
| 471 | "value": 1, |
| 472 | "improvement_direction": "down"}}, |
| 473 | "Test2": {"summary": |
| 474 | {"units": "ms", |
| 475 | "type": "scalar", |
| 476 | "value": -1, |
| 477 | "improvement_direction": "down"}}, |
| 478 | "Test3": {"summary": |
| 479 | {"units": "ms", |
| 480 | "type": "scalar", |
| 481 | "value": 3, |
| 482 | "improvement_direction": "down"}}} |
| 483 | self.assertDictEqual(expected_result, json.loads(f.read())) |
| 484 | |
Keith Haddow | df2730b | 2016-03-28 21:31:18 -0700 | [diff] [blame] | 485 | def test_chart_supplied(self): |
| 486 | self.test.resultsdir = tempfile.mkdtemp() |
Keith Haddow | 4fc2465 | 2016-03-16 10:08:17 -0700 | [diff] [blame] | 487 | |
Keith Haddow | df2730b | 2016-03-28 21:31:18 -0700 | [diff] [blame] | 488 | test_data = [("tcp_tx", "ch006_mode11B_none", "BT_connected_but_not_streaming", 0), |
| 489 | ("tcp_tx", "ch006_mode11B_none", "BT_streaming_audiofile", 5), |
| 490 | ("tcp_tx", "ch006_mode11B_none", "BT_disconnected_again", 0), |
| 491 | ("tcp_rx", "ch006_mode11B_none", "BT_connected_but_not_streaming", 0), |
| 492 | ("tcp_rx", "ch006_mode11B_none", "BT_streaming_audiofile", 8), |
| 493 | ("tcp_rx", "ch006_mode11B_none", "BT_disconnected_again", 0), |
| 494 | ("udp_tx", "ch006_mode11B_none", "BT_connected_but_not_streaming", 0), |
| 495 | ("udp_tx", "ch006_mode11B_none", "BT_streaming_audiofile", 6), |
| 496 | ("udp_tx", "ch006_mode11B_none", "BT_disconnected_again", 0), |
| 497 | ("udp_rx", "ch006_mode11B_none", "BT_connected_but_not_streaming", 0), |
| 498 | ("udp_rx", "ch006_mode11B_none", "BT_streaming_audiofile", 8), |
| 499 | ("udp_rx", "ch006_mode11B_none", "BT_streaming_audiofile", 9), |
| 500 | ("udp_rx", "ch006_mode11B_none", "BT_disconnected_again", 0)] |
| 501 | |
| 502 | |
| 503 | for (config_tag, ap_config_tag, bt_tag, drop) in test_data: |
| 504 | self.test.output_perf_value(config_tag + '_' + bt_tag + '_drop', |
| 505 | drop, units='percent_drop', |
| 506 | higher_is_better=False, |
| 507 | graph=ap_config_tag + '_drop') |
| 508 | f = open(self.test.resultsdir + "/results-chart.json") |
| 509 | expected_result = { |
| 510 | "ch006_mode11B_none_drop": { |
| 511 | "udp_tx_BT_streaming_audiofile_drop": { |
| 512 | "units": "percent_drop", |
| 513 | "type": "scalar", |
| 514 | "value": 6.0, |
| 515 | "improvement_direction": "down" |
| 516 | }, |
| 517 | "udp_rx_BT_disconnected_again_drop": { |
| 518 | "units": "percent_drop", |
| 519 | "type": "scalar", |
| 520 | "value": 0.0, |
| 521 | "improvement_direction": "down" |
| 522 | }, |
| 523 | "tcp_tx_BT_disconnected_again_drop": { |
| 524 | "units": "percent_drop", |
| 525 | "type": "scalar", |
| 526 | "value": 0.0, |
| 527 | "improvement_direction": "down" |
| 528 | }, |
| 529 | "tcp_rx_BT_streaming_audiofile_drop": { |
| 530 | "units": "percent_drop", |
| 531 | "type": "scalar", |
| 532 | "value": 8.0, |
| 533 | "improvement_direction": "down" |
| 534 | }, |
| 535 | "udp_tx_BT_connected_but_not_streaming_drop": { |
| 536 | "units": "percent_drop", |
| 537 | "type": "scalar", |
| 538 | "value": 0.0, |
| 539 | "improvement_direction": "down" |
| 540 | }, |
| 541 | "tcp_tx_BT_connected_but_not_streaming_drop": { |
| 542 | "units": "percent_drop", |
| 543 | "type": "scalar", |
| 544 | "value": 0.0, |
| 545 | "improvement_direction": "down" |
| 546 | }, |
| 547 | "udp_tx_BT_disconnected_again_drop": { |
| 548 | "units": "percent_drop", |
| 549 | "type": "scalar", |
| 550 | "value": 0.0, |
| 551 | "improvement_direction": "down" |
| 552 | }, |
| 553 | "tcp_tx_BT_streaming_audiofile_drop": { |
| 554 | "units": "percent_drop", |
| 555 | "type": "scalar", |
| 556 | "value": 5.0, |
| 557 | "improvement_direction": "down" |
| 558 | }, |
| 559 | "tcp_rx_BT_connected_but_not_streaming_drop": { |
| 560 | "units": "percent_drop", |
| 561 | "type": "scalar", |
| 562 | "value": 0.0, |
| 563 | "improvement_direction": "down" |
| 564 | }, |
| 565 | "udp_rx_BT_connected_but_not_streaming_drop": { |
| 566 | "units": "percent_drop", |
| 567 | "type": "scalar", |
| 568 | "value": 0.0, |
| 569 | "improvement_direction": "down" |
| 570 | }, |
| 571 | "udp_rx_BT_streaming_audiofile_drop": { |
| 572 | "units": "percent_drop", |
| 573 | "type": "list_of_scalar_values", |
| 574 | "values": [ |
| 575 | 8.0, |
| 576 | 9.0 |
| 577 | ], |
| 578 | "improvement_direction": "down" |
| 579 | }, |
| 580 | "tcp_rx_BT_disconnected_again_drop": { |
| 581 | "units": "percent_drop", |
| 582 | "type": "scalar", |
| 583 | "value": 0.0, |
| 584 | "improvement_direction": "down" |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | self.maxDiff = None |
| 589 | self.assertDictEqual(expected_result, json.loads(f.read())) |
Keith Haddow | 4fc2465 | 2016-03-16 10:08:17 -0700 | [diff] [blame] | 590 | |
mbligh | 234a84f | 2008-11-20 19:57:43 +0000 | [diff] [blame] | 591 | if __name__ == '__main__': |
| 592 | unittest.main() |