Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import unittest | 5 import unittest |
| 6 | 6 |
| 7 from telemetry import benchmark | 7 from telemetry import benchmark |
| 8 from telemetry import benchmark_runner | 8 from telemetry import benchmark_runner |
| 9 from telemetry.story import expectations | |
| 9 from telemetry.testing import stream | 10 from telemetry.testing import stream |
| 10 import mock | 11 import mock |
| 11 | 12 |
| 12 | 13 |
| 13 class BenchmarkFoo(benchmark.Benchmark): | 14 class DisabledExpectation(expectations.StoryExpectations): |
| 14 """ Benchmark Foo for testing.""" | 15 def SetExpectations(self): |
| 16 self.DisableBenchmark([expectations.ALL], 'crbug.com/123') | |
| 17 | |
| 18 | |
| 19 class BenchmarkEnabled(benchmark.Benchmark): | |
| 20 """ Enabled benchmark for testing.""" | |
| 15 | 21 |
| 16 @classmethod | 22 @classmethod |
| 17 def Name(cls): | 23 def Name(cls): |
| 18 return 'FooBenchmark' | 24 return 'EnabledBench' |
| 19 | 25 |
| 20 | 26 |
| 21 class BenchmarkBar(benchmark.Benchmark): | 27 class BenchmarkEnabledTwo(benchmark.Benchmark): |
| 22 """ Benchmark Bar for testing long description line.""" | 28 """ Second enabled benchmark for testing.""" |
| 23 | 29 |
| 24 @classmethod | 30 @classmethod |
| 25 def Name(cls): | 31 def Name(cls): |
| 26 return 'BarBenchmarkkkkk' | 32 return 'EnabledBench2' |
| 27 | 33 |
| 28 | 34 |
| 29 class UnusualBenchmark(benchmark.Benchmark): | 35 class BenchmarkDisabled(benchmark.Benchmark): |
| 36 """ Disabled benchmark for testing.""" | |
| 30 | 37 |
| 31 @classmethod | 38 @classmethod |
| 32 def Name(cls): | 39 def Name(cls): |
| 33 return 'I have a very unusual name' | 40 return 'DisabledBench' |
| 41 | |
| 42 def GetExpectations(self): | |
| 43 return DisabledExpectation() | |
| 34 | 44 |
| 35 | 45 |
| 36 class BenchmarkRunnerUnittest(unittest.TestCase): | 46 class BenchmarkRunnerUnittest(unittest.TestCase): |
| 37 | 47 |
| 38 def setUp(self): | 48 def setUp(self): |
| 39 self._stream = stream.TestOutputStream() | 49 self._stream = stream.TestOutputStream() |
| 40 self._mock_possible_browser = mock.MagicMock() | 50 self._mock_possible_browser = mock.MagicMock() |
| 41 self._mock_possible_browser.browser_type = 'TestBrowser' | 51 self._mock_possible_browser.browser_type = 'TestBrowser' |
| 42 | 52 |
| 43 def testPrintBenchmarkListWithNoDisabledBenchmark(self): | 53 def testPrintBenchmarkListWithNoDisabledBenchmark(self): |
| 44 expected_printed_stream = ( | 54 expected_printed_stream = ( |
| 45 'Available benchmarks for TestBrowser are:\n' | 55 'Available benchmarks for TestBrowser are:\n' |
| 46 ' BarBenchmarkkkkk Benchmark Bar for testing long description line.\n' | 56 ' EnabledBench Enabled benchmark for testing.\n' |
| 47 ' FooBenchmark Benchmark Foo for testing.\n' | 57 ' EnabledBench Enabled benchmark for testing.\n' |
| 48 'Pass --browser to list benchmarks for another browser.\n\n') | 58 'Pass --browser to list benchmarks for another browser.\n\n') |
| 49 with mock.patch('telemetry.benchmark_runner.decorators') as mock_module: | 59 benchmark_runner.PrintBenchmarkList([BenchmarkEnabled, BenchmarkEnabled], |
| 50 mock_module.IsEnabled.return_value = (True, None) | 60 self._mock_possible_browser, |
| 51 benchmark_runner.PrintBenchmarkList([BenchmarkFoo, BenchmarkBar], | 61 self._stream) |
| 52 self._mock_possible_browser, | 62 self.assertEquals(expected_printed_stream, self._stream.output_data) |
| 53 self._stream) | |
| 54 self.assertEquals(expected_printed_stream, self._stream.output_data) | |
| 55 | 63 |
| 56 def testPrintBenchmarkListWithOneDisabledBenchmark(self): | 64 def testPrintBenchmarkListWithOneDisabledBenchmark(self): |
| 57 expected_printed_stream = ( | 65 expected_printed_stream = ( |
| 58 'Available benchmarks for TestBrowser are:\n' | 66 'Available benchmarks for TestBrowser are:\n' |
| 59 ' FooBenchmark Benchmark Foo for testing.\n' | 67 ' EnabledBench Enabled benchmark for testing.\n' |
| 60 '\n' | 68 '\n' |
| 61 'Disabled benchmarks for TestBrowser are (force run with -d):\n' | 69 'Disabled benchmarks for TestBrowser are (force run with -d):\n' |
| 62 ' BarBenchmarkkkkk Benchmark Bar for testing long description line.\n' | 70 ' DisabledBench Disabled benchmark for testing.\n' |
| 63 'Pass --browser to list benchmarks for another browser.\n\n') | 71 'Pass --browser to list benchmarks for another browser.\n\n') |
| 64 with mock.patch('telemetry.benchmark_runner.decorators') as mock_module: | |
| 65 | 72 |
| 66 def FakeIsEnabled(benchmark_class, _): | 73 benchmark_runner.PrintBenchmarkList([BenchmarkEnabled, BenchmarkDisabled], |
| 67 if benchmark_class is BenchmarkFoo: | 74 self._mock_possible_browser, |
| 68 return True | 75 self._stream) |
| 69 else: | 76 self.assertEquals(expected_printed_stream, self._stream.output_data) |
| 70 return False | |
| 71 | |
| 72 mock_module.IsBenchmarkEnabled = FakeIsEnabled | |
| 73 benchmark_runner.PrintBenchmarkList([BenchmarkFoo, BenchmarkBar], | |
| 74 self._mock_possible_browser, | |
| 75 self._stream) | |
| 76 self.assertEquals(expected_printed_stream, self._stream.output_data) | |
| 77 | 77 |
| 78 def testShouldDisable(self): | 78 def testShouldDisable(self): |
| 79 """Ensure that overridden ShouldDisable class methods are respected.""" | 79 """Ensure that overridden ShouldDisable class methods are respected.""" |
| 80 expected_printed_stream = ( | 80 expected_printed_stream = ( |
| 81 'Available benchmarks for TestBrowser are:\n' | 81 'Available benchmarks for TestBrowser are:\n' |
| 82 ' BarBenchmarkkkkk Benchmark Bar for testing long description line.\n' | 82 ' EnabledBench Enabled benchmark for testing.\n' |
| 83 '\n' | 83 '\n' |
| 84 'Disabled benchmarks for TestBrowser are (force run with -d):\n' | 84 'Disabled benchmarks for TestBrowser are (force run with -d):\n' |
| 85 ' FooBenchmark Benchmark Foo for testing.\n' | 85 ' DisabledBench Disabled benchmark for testing.\n' |
| 86 'Pass --browser to list benchmarks for another browser.\n\n') | 86 'Pass --browser to list benchmarks for another browser.\n\n') |
| 87 | 87 |
| 88 @classmethod | 88 @classmethod |
| 89 def FakeShouldDisable(cls, possible_browser): | 89 def FakeShouldDisable(cls, possible_browser): |
| 90 del possible_browser # unused | 90 del possible_browser # unused |
| 91 return cls is BenchmarkFoo | 91 return cls is BenchmarkDisabled |
| 92 | 92 |
| 93 BenchmarkFoo.ShouldDisable = FakeShouldDisable | 93 BenchmarkDisabled.ShouldDisable = FakeShouldDisable |
|
nednguyen
2017/09/26 15:17:30
Are we also gonna remove these ShouldDisable metho
rnephew (Reviews Here)
2017/09/26 15:55:02
For now we cannot. The BattOr checks use it; but o
| |
| 94 BenchmarkBar.ShouldDisable = FakeShouldDisable | 94 BenchmarkEnabled.ShouldDisable = FakeShouldDisable |
| 95 benchmark_runner.PrintBenchmarkList( | 95 benchmark_runner.PrintBenchmarkList( |
| 96 [BenchmarkFoo, BenchmarkBar], self._mock_possible_browser, self._stream) | 96 [BenchmarkDisabled, BenchmarkEnabled], self._mock_possible_browser, |
| 97 self._stream) | |
| 97 self.assertEquals(expected_printed_stream, self._stream.output_data) | 98 self.assertEquals(expected_printed_stream, self._stream.output_data) |
| 98 | 99 |
| 99 def testShouldDisableComplex(self): | 100 def testShouldDisableComplex(self): |
| 100 """Ensure that browser-dependent ShouldDisable overrides are respected.""" | 101 """Ensure that browser-dependent ShouldDisable overrides are respected.""" |
| 101 expected_printed_stream = ( | 102 expected_printed_stream = ( |
| 102 # Expected output for 'TestBrowser': | 103 # Expected output for 'TestBrowser': |
| 103 'Available benchmarks for TestBrowser are:\n' | 104 'Available benchmarks for TestBrowser are:\n' |
| 104 ' FooBenchmark Benchmark Foo for testing.\n' | 105 ' EnabledBench Enabled benchmark for testing.\n' |
| 105 '\n' | 106 '\n' |
| 106 'Disabled benchmarks for TestBrowser are (force run with -d):\n' | 107 'Disabled benchmarks for TestBrowser are (force run with -d):\n' |
| 107 ' BarBenchmarkkkkk Benchmark Bar for testing long description line.\n' | 108 ' EnabledBench2 Second enabled benchmark for testing.\n' |
| 108 'Pass --browser to list benchmarks for another browser.\n\n' | 109 'Pass --browser to list benchmarks for another browser.\n\n' |
| 109 # Expected output for 'MockBrowser': | 110 # Expected output for 'MockBrowser': |
| 110 'Available benchmarks for MockBrowser are:\n' | 111 'Available benchmarks for MockBrowser are:\n' |
| 111 ' BarBenchmarkkkkk Benchmark Bar for testing long description line.\n' | 112 ' EnabledBench Enabled benchmark for testing.\n' |
| 112 ' FooBenchmark Benchmark Foo for testing.\n' | 113 ' EnabledBench2 Second enabled benchmark for testing.\n' |
| 113 'Pass --browser to list benchmarks for another browser.\n\n') | 114 'Pass --browser to list benchmarks for another browser.\n\n') |
| 114 | 115 |
| 115 @classmethod | 116 @classmethod |
| 116 def FakeShouldDisable(cls, possible_browser): | 117 def FakeShouldDisable(cls, possible_browser): |
| 117 return cls is BenchmarkBar and not 'Mock' in possible_browser.browser_type | 118 return ( |
| 119 cls is BenchmarkEnabledTwo and | |
| 120 not 'Mock' in possible_browser.browser_type) | |
| 118 | 121 |
| 119 BenchmarkFoo.ShouldDisable = FakeShouldDisable | 122 BenchmarkEnabled.ShouldDisable = FakeShouldDisable |
| 120 BenchmarkBar.ShouldDisable = FakeShouldDisable | 123 BenchmarkEnabledTwo.ShouldDisable = FakeShouldDisable |
| 121 benchmark_runner.PrintBenchmarkList( | 124 benchmark_runner.PrintBenchmarkList( |
| 122 [BenchmarkFoo, BenchmarkBar], self._mock_possible_browser, self._stream) | 125 [BenchmarkEnabled, BenchmarkEnabledTwo], self._mock_possible_browser, |
| 126 self._stream) | |
| 123 self._mock_possible_browser.browser_type = 'MockBrowser' | 127 self._mock_possible_browser.browser_type = 'MockBrowser' |
| 124 benchmark_runner.PrintBenchmarkList( | 128 benchmark_runner.PrintBenchmarkList( |
| 125 [BenchmarkFoo, BenchmarkBar], self._mock_possible_browser, self._stream) | 129 [BenchmarkEnabled, BenchmarkEnabledTwo], self._mock_possible_browser, |
| 130 self._stream) | |
| 126 self.assertEquals(expected_printed_stream, self._stream.output_data) | 131 self.assertEquals(expected_printed_stream, self._stream.output_data) |
| OLD | NEW |