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 import os | |
5 import unittest | |
6 | |
7 from telemetry.page import page_benchmark_results | |
8 from telemetry.page import page_set | |
9 from telemetry.page import perf_tests_helper | |
10 | |
11 def _MakePageSet(): | |
12 return page_set.PageSet.FromDict({ | |
13 "description": "hello", | |
14 "archive_path": "foo.wpr", | |
15 "pages": [ | |
16 {"url": "http://www.foo.com/"}, | |
17 {"url": "http://www.bar.com/"} | |
18 ] | |
19 }, os.path.dirname(__file__)) | |
20 | |
21 class NonPrintingPageBenchmarkResults( | |
22 page_benchmark_results.PageBenchmarkResults): | |
23 def __init__(self): | |
24 super(NonPrintingPageBenchmarkResults, self).__init__() | |
25 | |
26 def _PrintPerfResult(self, *args): | |
27 pass | |
28 | |
29 class SummarySavingPageBenchmarkResults( | |
30 page_benchmark_results.PageBenchmarkResults): | |
31 def __init__(self): | |
32 super(SummarySavingPageBenchmarkResults, self).__init__() | |
33 self.results = [] | |
34 | |
35 def _PrintPerfResult(self, *args): | |
36 res = perf_tests_helper.PrintPerfResult(*args, print_to_stdout=False) | |
37 self.results.append(res) | |
38 | |
39 class PageBenchmarkResultsTest(unittest.TestCase): | |
40 def test_basic(self): | |
41 test_page_set = _MakePageSet() | |
42 | |
43 benchmark_results = NonPrintingPageBenchmarkResults() | |
44 benchmark_results.WillMeasurePage(test_page_set.pages[0]) | |
45 benchmark_results.Add('a', 'seconds', 3) | |
46 benchmark_results.DidMeasurePage() | |
47 | |
48 benchmark_results.WillMeasurePage(test_page_set.pages[1]) | |
49 benchmark_results.Add('a', 'seconds', 3) | |
50 benchmark_results.DidMeasurePage() | |
51 | |
52 benchmark_results.PrintSummary('trace_tag') | |
53 | |
54 def test_url_is_invalid_value(self): | |
55 test_page_set = _MakePageSet() | |
56 | |
57 benchmark_results = NonPrintingPageBenchmarkResults() | |
58 benchmark_results.WillMeasurePage(test_page_set.pages[0]) | |
59 self.assertRaises( | |
60 AssertionError, | |
61 lambda: benchmark_results.Add('url', 'string', 'foo')) | |
62 | |
63 def test_unit_change(self): | |
64 test_page_set = _MakePageSet() | |
65 | |
66 benchmark_results = NonPrintingPageBenchmarkResults() | |
67 benchmark_results.WillMeasurePage(test_page_set.pages[0]) | |
68 benchmark_results.Add('a', 'seconds', 3) | |
69 benchmark_results.DidMeasurePage() | |
70 | |
71 benchmark_results.WillMeasurePage(test_page_set.pages[1]) | |
72 self.assertRaises( | |
73 AssertionError, | |
74 lambda: benchmark_results.Add('a', 'foobgrobbers', 3)) | |
75 | |
76 def test_type_change(self): | |
77 test_page_set = _MakePageSet() | |
78 | |
79 benchmark_results = NonPrintingPageBenchmarkResults() | |
80 benchmark_results.WillMeasurePage(test_page_set.pages[0]) | |
81 benchmark_results.Add('a', 'seconds', 3) | |
82 benchmark_results.DidMeasurePage() | |
83 | |
84 benchmark_results.WillMeasurePage(test_page_set.pages[1]) | |
85 self.assertRaises( | |
86 AssertionError, | |
87 lambda: benchmark_results.Add('a', 'seconds', 3, data_type='histogram')) | |
88 | |
89 def test_basic_summary(self): | |
90 test_page_set = _MakePageSet() | |
91 | |
92 benchmark_results = SummarySavingPageBenchmarkResults() | |
93 benchmark_results.WillMeasurePage(test_page_set.pages[0]) | |
94 benchmark_results.Add('a', 'seconds', 3) | |
95 benchmark_results.DidMeasurePage() | |
96 | |
97 benchmark_results.WillMeasurePage(test_page_set.pages[1]) | |
98 benchmark_results.Add('a', 'seconds', 7) | |
99 benchmark_results.DidMeasurePage() | |
100 | |
101 benchmark_results.PrintSummary(None) | |
102 expected = ['RESULT a_by_url: http___www.foo.com_= 3 seconds', | |
103 'RESULT a_by_url: http___www.bar.com_= 7 seconds', | |
104 '*RESULT a: a= [3,7] seconds\nAvg a: 5.000000seconds\n' + | |
105 'Sd a: 2.828427seconds'] | |
106 self.assertEquals( | |
107 benchmark_results.results, | |
108 expected) | |
109 | |
110 def test_histogram(self): | |
111 test_page_set = _MakePageSet() | |
112 | |
113 benchmark_results = SummarySavingPageBenchmarkResults() | |
114 benchmark_results.WillMeasurePage(test_page_set.pages[0]) | |
115 benchmark_results.Add('a', '', | |
116 '{"buckets": [{"low": 1, "high": 2, "count": 1}]}', | |
117 data_type='histogram') | |
118 benchmark_results.DidMeasurePage() | |
119 | |
120 benchmark_results.WillMeasurePage(test_page_set.pages[1]) | |
121 benchmark_results.Add('a', '', | |
122 '{"buckets": [{"low": 2, "high": 3, "count": 1}]}', | |
123 data_type='histogram') | |
124 benchmark_results.DidMeasurePage() | |
125 | |
126 benchmark_results.PrintSummary(None) | |
127 | |
128 expected = [ | |
129 '*HISTOGRAM a_by_url.http___www.foo.com_: ' + | |
130 'a_by_url.http___www.foo.com_= ' + | |
131 '{"buckets": [{"low": 1, "high": 2, "count": 1}]}\n' + | |
132 'Avg a_by_url.http___www.foo.com_: 1.500000', | |
133 '*HISTOGRAM a_by_url.http___www.bar.com_: ' + | |
134 'a_by_url.http___www.bar.com_= ' + | |
135 '{"buckets": [{"low": 2, "high": 3, "count": 1}]}\n' + | |
136 'Avg a_by_url.http___www.bar.com_: 2.500000'] | |
137 self.assertEquals(benchmark_results.results, expected) | |
OLD | NEW |