summaryrefslogtreecommitdiff
path: root/tools/bisect-perf-regression_test.py
blob: 16047acc1caaa9fb88d85ddeca8286895e0002c3 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# Copyright 2014 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 math
import unittest

# Special import necessary because filename contains dash characters.
bisect_perf_module = __import__('bisect-perf-regression')


RESULTS_OUTPUT = """RESULT write_operations: write_operations= 23089 count
RESULT read_bytes_gpu: read_bytes_gpu= 35201 kb
RESULT write_bytes_gpu: write_bytes_gpu= 542 kb
RESULT telemetry_page_measurement_results: num_failed= 0 count
RESULT telemetry_page_measurement_results: num_errored= 0 count
*RESULT Total: Total_ref= %(value)s
"""


class BisectPerfRegressionTest(unittest.TestCase):
  """Test case for top-level functions in the bisect-perf-regrssion module."""

  def setUp(self):
    """Sets up the test environment before each test method."""
    pass

  def tearDown(self):
    """Cleans up the test environment after each test method."""
    pass

  def testParseDEPSStringManually(self):
    """Tests DEPS parsing."""
    bisect_options = bisect_perf_module.BisectOptions()
    bisect_instance = bisect_perf_module.BisectPerformanceMetrics(
        None, bisect_options)

    deps_file_contents = """
vars = {
    'ffmpeg_hash':
         '@ac4a9f31fe2610bd146857bbd55d7a260003a888',
    'webkit_url':
         'https://chromium.googlesource.com/chromium/blink.git',
    'git_url':
         'https://chromium.googlesource.com',
    'webkit_rev':
         '@e01ac0a267d1017288bc67fa3c366b10469d8a24',
    'angle_revision':
         '74697cf2064c0a2c0d7e1b1b28db439286766a05'
}"""

    # Should only expect svn/git revisions to come through, and urls to be
    # filtered out.
    expected_vars_dict = {
        'ffmpeg_hash': '@ac4a9f31fe2610bd146857bbd55d7a260003a888',
        'webkit_rev': '@e01ac0a267d1017288bc67fa3c366b10469d8a24',
        'angle_revision': '74697cf2064c0a2c0d7e1b1b28db439286766a05'
    }
    vars_dict = bisect_instance._ParseRevisionsFromDEPSFileManually(
        deps_file_contents)
    self.assertEqual(vars_dict, expected_vars_dict)

  def testCalculateTruncatedMeanRaisesError(self):
    """CalculateTrunctedMean raises an error when passed an empty list."""
    with self.assertRaises(TypeError):
      bisect_perf_module.CalculateTruncatedMean([], 0)

  def testCalculateMeanSingleNum(self):
    """Tests the CalculateMean function with a single number."""
    self.assertEqual(3.0, bisect_perf_module.CalculateMean([3]))

  def testCalculateMeanShortList(self):
    """Tests the CalculateMean function with a short list."""
    self.assertEqual(0.5, bisect_perf_module.CalculateMean([-3, 0, 1, 4]))

  def testCalculateMeanCompareAlternateImplementation(self):
    """Tests CalculateMean by comparing against an alternate implementation."""
    def AlternateMeanFunction(values):
      """Simple arithmetic mean function."""
      return sum(values) / float(len(values))
    test_values_lists = [[1], [5, 6.5, 1.2, 3], [-3, 0, 1, 4],
                         [-3, -1, 0.12, 0.752, 3.33, 8, 16, 32, 439]]
    for values in test_values_lists:
      self.assertEqual(
          AlternateMeanFunction(values),
          bisect_perf_module.CalculateMean(values))

  def testCalculateConfidence(self):
    """Tests the confidence calculation."""
    bad_values = [[0, 1], [1, 2]]
    good_values = [[6, 7], [7, 8]]
    # Closest means are mean(1, 2) and mean(6, 7).
    distance = 6.5 - 1.5
    # Standard deviation of [n-1, n, n, n+1] is 0.8165.
    stddev_sum = 0.8165 + 0.8165
    # Expected confidence is an int in the range [0, 100].
    expected_confidence = min(100, int(100 * distance / float(stddev_sum)))
    self.assertEqual(
        expected_confidence,
        bisect_perf_module.CalculateConfidence(bad_values, good_values))

  def testCalculateConfidence0(self):
    """Tests the confidence calculation when it's expected to be 0."""
    bad_values = [[0, 1], [1, 2], [4, 5], [0, 2]]
    good_values = [[4, 5], [6, 7], [7, 8]]
    # Both groups have value lists with means of 4.5, which means distance
    # between groups is zero, and thus confidence is zero.
    self.assertEqual(
        0, bisect_perf_module.CalculateConfidence(bad_values, good_values))

  def testCalculateConfidence100(self):
    """Tests the confidence calculation when it's expected to be 100."""
    bad_values = [[1, 1], [1, 1]]
    good_values = [[1.2, 1.2], [1.2, 1.2]]
    # Standard deviation in both groups is zero, so confidence is 100.
    self.assertEqual(
        100, bisect_perf_module.CalculateConfidence(bad_values, good_values))

  def testCalculateRelativeChange(self):
    """Tests the common cases for calculating relative change."""
    # The change is relative to the first value, regardless of which is bigger.
    self.assertEqual(0.5, bisect_perf_module.CalculateRelativeChange(1.0, 1.5))
    self.assertEqual(0.5, bisect_perf_module.CalculateRelativeChange(2.0, 1.0))

  def testCalculateRelativeChangeFromZero(self):
    """Tests what happens when relative change from zero is calculated."""
    # If the first number is zero, then the result is not a number.
    self.assertEqual(0, bisect_perf_module.CalculateRelativeChange(0, 0))
    self.assertTrue(
        math.isnan(bisect_perf_module.CalculateRelativeChange(0, 1)))
    self.assertTrue(
        math.isnan(bisect_perf_module.CalculateRelativeChange(0, -1)))

  def testCalculateRelativeChangeWithNegatives(self):
    """Tests that relative change given is always positive."""
    self.assertEqual(3.0, bisect_perf_module.CalculateRelativeChange(-1, 2))
    self.assertEqual(3.0, bisect_perf_module.CalculateRelativeChange(1, -2))
    self.assertEqual(1.0, bisect_perf_module.CalculateRelativeChange(-1, -2))

  def testTryParseResultValuesFromOutputWithSingleValue(self):
    """Tests result pattern <*>RESULT <graph>: <trace>= <value>"""
    bisect_options = bisect_perf_module.BisectOptions()
    bisect_instance = bisect_perf_module.BisectPerformanceMetrics(
        None, bisect_options)
    metrics = ['Total', 'Total_ref']
    self.assertEqual(
        [66.88], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '66.88 kb'}))
    self.assertEqual(
        [66.88], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '66.88kb'}))
    self.assertEqual(
        [66.88], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': ' 66.88 '}))
    self.assertEqual(
        [-66.88], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': ' -66.88 kb'}))
    self.assertEqual(
        [66], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '66 kb'}))
    self.assertEqual(
        [.66], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '.66 kb'}))
    self.assertEqual(
        [], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '. kb'}))
    self.assertEqual(
        [], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': 'aaa kb'}))

  def testTryParseResultValuesFromOutputWithMulitValue(self):
    """Tests result pattern <*>RESULT <graph>: <trace>= [<value>,<value>, ..]"""
    bisect_options = bisect_perf_module.BisectOptions()
    bisect_instance = bisect_perf_module.BisectPerformanceMetrics(
        None, bisect_options)
    metrics = ['Total', 'Total_ref']
    self.assertEqual(
        [66.88], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '[66.88] kb'}))
    self.assertEqual(
        [66.88, 99.44], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '[66.88, 99.44]kb'}))
    self.assertEqual(
        [66.88, 99.44], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '[ 66.88, 99.44 ]'}))
    self.assertEqual(
        [-66.88, 99.44], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '[-66.88,99.44] kb'}))
    self.assertEqual(
        [-66, 99], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '[-66,99] kb'}))
    self.assertEqual(
        [-66, 99], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '[-66,99,] kb'}))
    self.assertEqual(
        [.66, .99], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '[.66,.99] kb'}))
    self.assertEqual(
        [], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '[] kb'}))
    self.assertEqual(
        [], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '[-66,abc] kb'}))

  def testTryParseResultValuesFromOutputWithMeanStd(self):
    """Tests result pattern <*>RESULT <graph>: <trace>= {<mean, std}"""
    bisect_options = bisect_perf_module.BisectOptions()
    bisect_instance = bisect_perf_module.BisectPerformanceMetrics(
        None, bisect_options)
    metrics = ['Total', 'Total_ref']
    self.assertEqual(
        [33.22], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '{33.22, 3.6} kb'}))
    self.assertEqual(
        [33.22], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '{33.22,3.6}kb'}))
    self.assertEqual(
        [33.22], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '{33.22,3.6} kb'}))
    self.assertEqual(
        [33.22], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '{ 33.22,3.6 }kb'}))
    self.assertEqual(
        [-33.22], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '{-33.22,3.6}kb'}))
    self.assertEqual(
        [22], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '{22,6}kb'}))
    self.assertEqual(
        [.22], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '{.22,6}kb'}))
    self.assertEqual(
        [], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '{.22,6, 44}kb'}))
    self.assertEqual(
        [], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '{}kb'}))
    self.assertEqual(
        [], bisect_instance.TryParseResultValuesFromOutput(
            metrics, RESULTS_OUTPUT % {'value': '{XYZ}kb'}))


if __name__ == '__main__':
  unittest.main()