summaryrefslogtreecommitdiffstats
path: root/tools/telemetry/telemetry/page/page_benchmark_results_unittest.py
blob: b2edad36aff0fcfa9252c0a018a32a9316b10237 (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Copyright (c) 2012 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 os
import unittest

from telemetry.page import page_benchmark_results
from telemetry.page import page_set
from telemetry.page import perf_tests_helper

def _MakePageSet():
  return page_set.PageSet.FromDict({
      "description": "hello",
      "archive_path": "foo.wpr",
      "pages": [
        {"url": "http://www.foo.com/"},
        {"url": "http://www.bar.com/"}
        ]
      }, os.path.dirname(__file__))

class NonPrintingPageBenchmarkResults(
    page_benchmark_results.PageBenchmarkResults):
  def __init__(self):
    super(NonPrintingPageBenchmarkResults, self).__init__()

  def _PrintPerfResult(self, *args):
    pass

class SummarySavingPageBenchmarkResults(
    page_benchmark_results.PageBenchmarkResults):
  def __init__(self):
    super(SummarySavingPageBenchmarkResults, self).__init__()
    self.results = []

  def _PrintPerfResult(self, *args):
    res = perf_tests_helper.PrintPerfResult(*args, print_to_stdout=False)
    self.results.append(res)

class PageBenchmarkResultsTest(unittest.TestCase):
  def test_basic(self):
    test_page_set = _MakePageSet()

    benchmark_results = NonPrintingPageBenchmarkResults()
    benchmark_results.WillMeasurePage(test_page_set.pages[0])
    benchmark_results.Add('a', 'seconds', 3)
    benchmark_results.DidMeasurePage()

    benchmark_results.WillMeasurePage(test_page_set.pages[1])
    benchmark_results.Add('a', 'seconds', 3)
    benchmark_results.DidMeasurePage()

    benchmark_results.PrintSummary('trace_tag')

  def test_url_is_invalid_value(self):
    test_page_set = _MakePageSet()

    benchmark_results = NonPrintingPageBenchmarkResults()
    benchmark_results.WillMeasurePage(test_page_set.pages[0])
    self.assertRaises(
      AssertionError,
      lambda: benchmark_results.Add('url', 'string', 'foo'))

  def test_unit_change(self):
    test_page_set = _MakePageSet()

    benchmark_results = NonPrintingPageBenchmarkResults()
    benchmark_results.WillMeasurePage(test_page_set.pages[0])
    benchmark_results.Add('a', 'seconds', 3)
    benchmark_results.DidMeasurePage()

    benchmark_results.WillMeasurePage(test_page_set.pages[1])
    self.assertRaises(
      AssertionError,
      lambda: benchmark_results.Add('a', 'foobgrobbers', 3))

  def test_type_change(self):
    test_page_set = _MakePageSet()

    benchmark_results = NonPrintingPageBenchmarkResults()
    benchmark_results.WillMeasurePage(test_page_set.pages[0])
    benchmark_results.Add('a', 'seconds', 3)
    benchmark_results.DidMeasurePage()

    benchmark_results.WillMeasurePage(test_page_set.pages[1])
    self.assertRaises(
      AssertionError,
      lambda: benchmark_results.Add('a', 'seconds', 3, data_type='histogram'))

  def test_basic_summary(self):
    test_page_set = _MakePageSet()

    benchmark_results = SummarySavingPageBenchmarkResults()
    benchmark_results.WillMeasurePage(test_page_set.pages[0])
    benchmark_results.Add('a', 'seconds', 3)
    benchmark_results.DidMeasurePage()

    benchmark_results.WillMeasurePage(test_page_set.pages[1])
    benchmark_results.Add('a', 'seconds', 7)
    benchmark_results.DidMeasurePage()

    benchmark_results.PrintSummary(None)
    expected = ['RESULT a_by_url: http___www.foo.com_= 3 seconds',
                'RESULT a_by_url: http___www.bar.com_= 7 seconds',
                '*RESULT a: a= [3,7] seconds\nAvg a: 5.000000seconds\n' +
                'Sd  a: 2.828427seconds']
    self.assertEquals(
      benchmark_results.results,
      expected)

  def test_repeated_pageset(self):
    test_page_set = _MakePageSet()

    benchmark_results = SummarySavingPageBenchmarkResults()
    benchmark_results.WillMeasurePage(test_page_set.pages[0])
    benchmark_results.Add('a', 'seconds', 3)
    benchmark_results.DidMeasurePage()

    benchmark_results.WillMeasurePage(test_page_set.pages[1])
    benchmark_results.Add('a', 'seconds', 7)
    benchmark_results.DidMeasurePage()

    benchmark_results.WillMeasurePage(test_page_set.pages[0])
    benchmark_results.Add('a', 'seconds', 4)
    benchmark_results.DidMeasurePage()

    benchmark_results.WillMeasurePage(test_page_set.pages[1])
    benchmark_results.Add('a', 'seconds', 8)
    benchmark_results.DidMeasurePage()

    benchmark_results.PrintSummary(None)
    expected = ['RESULT a_by_url: http___www.foo.com_= [3,4] seconds\n' +
                'Avg a_by_url: 3.500000seconds\nSd  a_by_url: 0.707107seconds',
                'RESULT a_by_url: http___www.bar.com_= [7,8] seconds\n' +
                'Avg a_by_url: 7.500000seconds\nSd  a_by_url: 0.707107seconds',
                '*RESULT a: a= [3,7,4,8] seconds\n' +
                'Avg a: 5.500000seconds\nSd  a: 2.380476seconds'
                ]
    self.assertEquals(
      benchmark_results.results,
      expected)

  def test_histogram(self):
    test_page_set = _MakePageSet()

    benchmark_results = SummarySavingPageBenchmarkResults()
    benchmark_results.WillMeasurePage(test_page_set.pages[0])
    benchmark_results.Add('a', '',
                          '{"buckets": [{"low": 1, "high": 2, "count": 1}]}',
                          data_type='histogram')
    benchmark_results.DidMeasurePage()

    benchmark_results.WillMeasurePage(test_page_set.pages[1])
    benchmark_results.Add('a', '',
                          '{"buckets": [{"low": 2, "high": 3, "count": 1}]}',
                          data_type='histogram')
    benchmark_results.DidMeasurePage()

    benchmark_results.PrintSummary(None)

    expected = [
        'HISTOGRAM a_by_url: http___www.foo.com_= ' +
        '{"buckets": [{"low": 1, "high": 2, "count": 1}]}\n' +
        'Avg a_by_url: 1.500000',
        'HISTOGRAM a_by_url: http___www.bar.com_= ' +
        '{"buckets": [{"low": 2, "high": 3, "count": 1}]}\n' +
        'Avg a_by_url: 2.500000']
    self.assertEquals(benchmark_results.results, expected)