Fix import error in mac_platform_backend.py
[chromium-blink-merge.git] / tools / perf / metrics / timeline_unittest.py
blobebc72eb1d8da4e2b22b0a38ce4b51badc7d59c89
1 # Copyright 2013 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.
5 import unittest
7 from metrics import timeline
8 from telemetry.core.timeline import bounds
9 from telemetry.core.timeline import model as model_module
10 from telemetry.page import page as page_module
11 from telemetry.page import page_measurement_results
12 from telemetry.value import scalar
14 class TestPageMeasurementResults(
15 page_measurement_results.PageMeasurementResults):
16 def __init__(self, test):
17 super(TestPageMeasurementResults, self).__init__()
18 self.test = test
19 page = page_module.Page("http://www.google.com", {})
20 self.WillMeasurePage(page)
22 def GetPageSpecificValueNamed(self, name):
23 values = [value for value in self.all_page_specific_values
24 if value.name == name]
25 assert len(values) == 1, 'Could not find value named %s' % name
26 return values[0]
28 def AssertHasPageSpecificScalarValue(self, name, units, expected_value):
29 value = self.GetPageSpecificValueNamed(name)
30 self.test.assertEquals(units, value.units)
31 self.test.assertTrue(isinstance(value, scalar.ScalarValue))
32 self.test.assertEquals(expected_value, value.value)
34 def __str__(self):
35 return '\n'.join([repr(x) for x in self.all_page_specific_values])
37 class LoadTimesTimelineMetric(unittest.TestCase):
38 def GetResultsForModel(self, metric, model):
39 metric.model = model
40 results = TestPageMeasurementResults(self)
41 tab = None
42 metric.AddResults(tab, results)
43 return results
45 def testSanitizing(self):
46 model = model_module.TimelineModel()
47 renderer_main = model.GetOrCreateProcess(1).GetOrCreateThread(2)
48 renderer_main.name = 'CrRendererMain'
50 # [ X ]
51 # [ Y ]
52 renderer_main.BeginSlice('cat1', 'x.y', 10, 0)
53 renderer_main.EndSlice(20, 20)
54 model.FinalizeImport()
56 metric = timeline.LoadTimesTimelineMetric(timeline.TRACING_MODE)
57 metric.renderer_process = renderer_main.parent
58 results = self.GetResultsForModel(metric, model)
59 results.AssertHasPageSpecificScalarValue(
60 'CrRendererMain|x_y', 'ms', 10)
61 results.AssertHasPageSpecificScalarValue(
62 'CrRendererMain|x_y_max', 'ms', 10)
63 results.AssertHasPageSpecificScalarValue(
64 'CrRendererMain|x_y_avg', 'ms', 10)
66 def testCounterSanitizing(self):
67 model = model_module.TimelineModel()
68 renderer_main = model.GetOrCreateProcess(1).GetOrCreateThread(2)
69 renderer_main.name = 'CrRendererMain'
71 x_counter = renderer_main.parent.GetOrCreateCounter('cat', 'x.y')
72 x_counter.samples += [1, 2]
73 x_counter.series_names += ['a']
74 x_counter.timestamps += [0, 1]
75 model.FinalizeImport()
77 metric = timeline.LoadTimesTimelineMetric(timeline.TRACING_MODE)
78 metric.renderer_process = renderer_main.parent
79 results = self.GetResultsForModel(metric, model)
80 results.AssertHasPageSpecificScalarValue(
81 'cat_x_y', 'count', 3)
82 results.AssertHasPageSpecificScalarValue(
83 'cat_x_y_avg', 'count', 1.5)
86 class ThreadTimesTimelineMetricUnittest(unittest.TestCase):
87 def GetResultsForModel(self, metric, model):
88 metric.model = model
89 results = TestPageMeasurementResults(self)
90 tab = None
91 metric.AddResults(tab, results)
92 return results
94 def GetActionRange(self, start, end):
95 action_range = bounds.Bounds()
96 action_range.AddValue(start)
97 action_range.AddValue(end)
98 return action_range
100 def testResults(self):
101 model = model_module.TimelineModel()
102 renderer_main = model.GetOrCreateProcess(1).GetOrCreateThread(2)
103 renderer_main.name = 'CrRendererMain'
105 metric = timeline.ThreadTimesTimelineMetric()
106 metric._action_ranges = [self.GetActionRange(1, 2)]
107 metric.details_to_report = timeline.ReportMainThreadOnly
108 results = self.GetResultsForModel(metric, model)
110 # Test that all result thread categories exist
111 for name in timeline.TimelineThreadCategories.values():
112 results.GetPageSpecificValueNamed(timeline.ThreadTimeResultName(name))
113 results.GetPageSpecificValueNamed(timeline.ThreadCpuTimeResultName(name))
115 def testBasic(self):
116 model = model_module.TimelineModel()
117 renderer_main = model.GetOrCreateProcess(1).GetOrCreateThread(2)
118 renderer_main.name = 'CrRendererMain'
120 # Create two frame swaps (Results times should be divided by two)
121 cc_main = model.GetOrCreateProcess(1).GetOrCreateThread(3)
122 cc_main.name = 'Compositor'
123 cc_main.BeginSlice('cc_cat', timeline.FrameTraceName, 10, 10)
124 cc_main.EndSlice(11, 11)
125 cc_main.BeginSlice('cc_cat', timeline.FrameTraceName, 12, 12)
126 cc_main.EndSlice(13, 13)
128 # [ X ] [ Z ]
129 # [ Y ]
130 renderer_main.BeginSlice('cat1', 'X', 10, 0)
131 renderer_main.BeginSlice('cat2', 'Y', 15, 5)
132 renderer_main.EndSlice(16, 5.5)
133 renderer_main.EndSlice(30, 19.5)
134 renderer_main.BeginSlice('cat1', 'Z', 31, 20)
135 renderer_main.BeginSlice('cat1', 'Z', 33, 21)
136 model.FinalizeImport()
138 # Exclude 'Z' using an action-range.
139 metric = timeline.ThreadTimesTimelineMetric()
140 metric._action_ranges = [self.GetActionRange(10, 30)]
141 metric.details_to_report = timeline.ReportMainThreadOnly
142 results = self.GetResultsForModel(metric, model)
144 # Test a couple specific results.
145 assert_results = {
146 timeline.ThreadTimeResultName('renderer_main') : 10,
147 timeline.ThreadDetailResultName('renderer_main','cat1') : 9.5,
148 timeline.ThreadDetailResultName('renderer_main','cat2') : 0.5,
149 timeline.ThreadDetailResultName('renderer_main','idle') : 0
151 for name, value in assert_results.iteritems():
152 results.AssertHasPageSpecificScalarValue(name, 'ms', value)
154 def testOverheadIsRemoved(self):
155 model = model_module.TimelineModel()
156 renderer_main = model.GetOrCreateProcess(1).GetOrCreateThread(2)
157 renderer_main.name = 'CrRendererMain'
159 # Create one frame swap.
160 cc_main = model.GetOrCreateProcess(1).GetOrCreateThread(3)
161 cc_main.name = 'Compositor'
162 cc_main.BeginSlice('cc_cat', timeline.FrameTraceName, 10, 10)
163 cc_main.EndSlice(11, 11)
165 # [ X ]
166 # [Overhead]
167 overhead_category = timeline.OverheadTraceCategory
168 overhead_name = timeline.OverheadTraceName
169 renderer_main.BeginSlice('cat1', 'X', 10, 0)
170 renderer_main.BeginSlice(overhead_category, overhead_name, 15, 5)
171 renderer_main.EndSlice(16, 6)
172 renderer_main.EndSlice(30, 10)
173 model.FinalizeImport()
175 # Include everything in an action-range.
176 metric = timeline.ThreadTimesTimelineMetric()
177 metric._action_ranges = [self.GetActionRange(10, 30)]
178 metric.details_to_report = timeline.ReportMainThreadOnly
179 results = self.GetResultsForModel(metric, model)
181 # Test a couple specific results.
182 assert_results = {
183 timeline.ThreadTimeResultName('renderer_main') : 19,
184 timeline.ThreadCpuTimeResultName('renderer_main') : 9.0,
186 for name, value in assert_results.iteritems():
187 results.AssertHasPageSpecificScalarValue(name, 'ms', value)