summaryrefslogtreecommitdiffstats
path: root/tools/perf/metrics/timeline_unittest.py
blob: 6b3c9a00dfd583c408f828462e03e1e7f3cdf52c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import unittest

from metrics import timeline
from telemetry.core.timeline import bounds
from telemetry.core.timeline import model as model_module
from telemetry.page import page as page_module
from telemetry.page import page_measurement_results
from telemetry.value import scalar

class TestPageMeasurementResults(
    page_measurement_results.PageMeasurementResults):
  def __init__(self, test):
    super(TestPageMeasurementResults, self).__init__()
    self.test = test
    page = page_module.Page("http://www.google.com", {})
    self.WillMeasurePage(page)

  def GetPageSpecificValueNamed(self, name):
    values = [value for value in self.all_page_specific_values
         if value.name == name]
    assert len(values) == 1, 'Could not find value named %s' % name
    return values[0]

  def AssertHasPageSpecificScalarValue(self, name, units, expected_value):
    value = self.GetPageSpecificValueNamed(name)
    self.test.assertEquals(units, value.units)
    self.test.assertTrue(isinstance(value, scalar.ScalarValue))
    self.test.assertEquals(expected_value, value.value)

  def __str__(self):
    return '\n'.join([repr(x) for x in self.all_page_specific_values])

class LoadTimesTimelineMetric(unittest.TestCase):
  def GetResultsForModel(self, metric, model):
    metric.model = model
    results = TestPageMeasurementResults(self)
    tab = None
    metric.AddResults(tab, results)
    return results

  def testSanitizing(self):
    model = model_module.TimelineModel()
    renderer_main = model.GetOrCreateProcess(1).GetOrCreateThread(2)
    renderer_main.name = 'CrRendererMain'

    # [      X       ]
    #      [  Y  ]
    renderer_main.BeginSlice('cat1', 'x.y', 10, 0)
    renderer_main.EndSlice(20, 20)
    model.FinalizeImport()

    metric = timeline.LoadTimesTimelineMetric(timeline.TRACING_MODE)
    metric.renderer_process = renderer_main.parent
    results = self.GetResultsForModel(metric, model)
    results.AssertHasPageSpecificScalarValue(
      'CrRendererMain|x_y', 'ms', 10)
    results.AssertHasPageSpecificScalarValue(
      'CrRendererMain|x_y_max', 'ms', 10)
    results.AssertHasPageSpecificScalarValue(
      'CrRendererMain|x_y_avg', 'ms', 10)

  def testCounterSanitizing(self):
    model = model_module.TimelineModel()
    renderer_main = model.GetOrCreateProcess(1).GetOrCreateThread(2)
    renderer_main.name = 'CrRendererMain'

    x_counter = renderer_main.parent.GetOrCreateCounter('cat', 'x.y')
    x_counter.samples += [1, 2]
    x_counter.series_names += ['a']
    x_counter.timestamps += [0, 1]
    model.FinalizeImport()

    metric = timeline.LoadTimesTimelineMetric(timeline.TRACING_MODE)
    metric.renderer_process = renderer_main.parent
    results = self.GetResultsForModel(metric, model)
    results.AssertHasPageSpecificScalarValue(
      'cat_x_y', 'count', 3)
    results.AssertHasPageSpecificScalarValue(
      'cat_x_y_avg', 'count', 1.5)


class ThreadTimesTimelineMetricUnittest(unittest.TestCase):
  def GetResultsForModel(self, metric, model):
    metric.model = model
    results = TestPageMeasurementResults(self)
    tab = None
    metric.AddResults(tab, results)
    return results

  def testBasic(self):
    model = model_module.TimelineModel()
    renderer_main = model.GetOrCreateProcess(1).GetOrCreateThread(2)
    renderer_main.name = 'CrRendererMain'

    # Create two frame swaps (Results times should be divided by two)
    cc_main = model.GetOrCreateProcess(1).GetOrCreateThread(3)
    cc_main.name = 'Compositor'
    cc_main.BeginSlice('cc_cat', timeline.CompositorFrameTraceName, 10, 10)
    cc_main.EndSlice(11, 11)
    cc_main.BeginSlice('cc_cat', timeline.CompositorFrameTraceName, 12, 12)
    cc_main.EndSlice(13, 13)

    # [      X       ]   [ Z ]
    #      [  Y  ]
    renderer_main.BeginSlice('cat1', 'X', 10, 0)
    renderer_main.BeginSlice('cat2', 'Y', 15, 5)
    renderer_main.EndSlice(16, 5.5)
    renderer_main.EndSlice(30, 19.5)
    renderer_main.BeginSlice('cat1', 'Z', 31, 20)
    renderer_main.BeginSlice('cat1', 'Z', 33, 21)
    model.FinalizeImport()

    # Exclude Z using an action-range.
    action_range = bounds.Bounds()
    action_range.AddValue(10)
    action_range.AddValue(30)

    metric = timeline.ThreadTimesTimelineMetric()
    metric._action_ranges = [action_range]
    metric.details_to_report = timeline.MainThread
    results = self.GetResultsForModel(metric, model)

    # Test that all categories exist
    for name in timeline.TimelineThreadCategories.values():
      results.GetPageSpecificValueNamed(timeline.ThreadTimeResultName(name))
      results.GetPageSpecificValueNamed(timeline.ThreadCpuTimeResultName(name))

    # Test a couple specific results.
    assert_results = {
      timeline.ThreadTimeResultName('renderer_main') : 10,
      timeline.ThreadDetailResultName('renderer_main','cat1') : 9.5,
      timeline.ThreadDetailResultName('renderer_main','cat2') : 0.5,
      timeline.ThreadDetailResultName('renderer_main','idle') : 0
    }
    for name, value in assert_results.iteritems():
      results.AssertHasPageSpecificScalarValue(name, 'ms', value)