OLD | NEW |
| (Empty) |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 from telemetry import page_test | |
5 | |
6 class MeasurementFailure(page_test.Failure): | |
7 """Exception that can be thrown from MeasurePage to indicate an undesired but | |
8 designed-for problem.""" | |
9 pass | |
10 | |
11 # TODO(nduca): Rename to page_benchmark | |
12 class MultiPageBenchmark(page_test.PageTest): | |
13 """Glue code for running a benchmark across a set of pages. | |
14 | |
15 To use this, subclass from the benchmark and override MeasurePage. For | |
16 example: | |
17 | |
18 class BodyChildElementBenchmark(MultiPageBenchmark): | |
19 def MeasurePage(self, page, tab, results): | |
20 body_child_count = tab.EvaluateJavaScript( | |
21 'document.body.children.length') | |
22 results.Add('body_children', 'count', body_child_count) | |
23 | |
24 if __name__ == '__main__': | |
25 multi_page_benchmark.Main(BodyChildElementBenchmark()) | |
26 | |
27 All benchmarks should include a unit test! | |
28 | |
29 TODO(nduca): Add explanation of how to write the unit test. | |
30 | |
31 To add test-specific options: | |
32 | |
33 class BodyChildElementBenchmark(MultiPageBenchmark): | |
34 def AddCommandLineOptions(parser): | |
35 parser.add_option('--element', action='store', default='body') | |
36 | |
37 def MeasurePage(self, page, tab, results): | |
38 body_child_count = tab.EvaluateJavaScript( | |
39 'document.querySelector('%s').children.length') | |
40 results.Add('children', 'count', child_count) | |
41 """ | |
42 def __init__(self, | |
43 action_name_to_run='', | |
44 needs_browser_restart_after_each_run=False): | |
45 super(MultiPageBenchmark, self).__init__( | |
46 '_RunTest', | |
47 action_name_to_run, | |
48 needs_browser_restart_after_each_run) | |
49 | |
50 def _RunTest(self, page, tab, results): | |
51 results.WillMeasurePage(page) | |
52 self.MeasurePage(page, tab, results) | |
53 results.DidMeasurePage() | |
54 | |
55 @property | |
56 def results_are_the_same_on_every_page(self): | |
57 """By default, benchmarks are assumed to output the same values for every | |
58 page. This allows incremental output, for example in CSV. If, however, the | |
59 benchmark discovers what values it can report as it goes, and those values | |
60 may vary from page to page, you need to override this function and return | |
61 False. Output will not appear in this mode until the entire pageset has | |
62 run.""" | |
63 return True | |
64 | |
65 def MeasurePage(self, page, tab, results): | |
66 """Override to actually measure the page's performance. | |
67 | |
68 page is a page_set.Page | |
69 tab is an instance of telemetry.Tab | |
70 | |
71 Should call results.Add(name, units, value) for each result, or raise an | |
72 exception on failure. The name and units of each Add() call must be | |
73 the same across all iterations. The name 'url' must not be used. | |
74 | |
75 Prefer field names that are in accordance with python variable style. E.g. | |
76 field_name. | |
77 | |
78 Put together: | |
79 | |
80 def MeasurePage(self, page, tab, results): | |
81 res = tab.EvaluateJavaScript('2+2') | |
82 if res != 4: | |
83 raise Exception('Oh, wow.') | |
84 results.Add('two_plus_two', 'count', res) | |
85 """ | |
86 raise NotImplementedError() | |
OLD | NEW |