1 # Copyright 2014 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.
8 # Special import necessary because filename contains dash characters.
9 bisect_perf_module = __import__('bisect-perf-regression')
12 class BisectPerfRegressionTest(unittest.TestCase):
13 """Test case for top-level functions in the bisect-perf-regrssion module."""
16 """Sets up the test environment before each test method."""
20 """Cleans up the test environment after each test method."""
23 def testParseDEPSStringManually(self):
24 """Tests DEPS parsing."""
25 bisect_options = bisect_perf_module.BisectOptions()
26 bisect_instance = bisect_perf_module.BisectPerformanceMetrics(
29 deps_file_contents = """
32 '@ac4a9f31fe2610bd146857bbd55d7a260003a888',
34 'https://chromium.googlesource.com/chromium/blink.git',
36 'https://chromium.googlesource.com',
38 '@e01ac0a267d1017288bc67fa3c366b10469d8a24',
40 '74697cf2064c0a2c0d7e1b1b28db439286766a05'
43 # Should only expect svn/git revisions to come through, and urls to be
45 expected_vars_dict = {
46 'ffmpeg_hash': '@ac4a9f31fe2610bd146857bbd55d7a260003a888',
47 'webkit_rev': '@e01ac0a267d1017288bc67fa3c366b10469d8a24',
48 'angle_revision': '74697cf2064c0a2c0d7e1b1b28db439286766a05'
50 vars_dict = bisect_instance._ParseRevisionsFromDEPSFileManually(
52 self.assertEqual(vars_dict, expected_vars_dict)
54 def testCalculateTruncatedMeanRaisesError(self):
55 """CalculateTrunctedMean raises an error when passed an empty list."""
56 with self.assertRaises(TypeError):
57 bisect_perf_module.CalculateTruncatedMean([], 0)
59 def testCalculateMeanSingleNum(self):
60 """Tests the CalculateMean function with a single number."""
61 self.assertEqual(3.0, bisect_perf_module.CalculateMean([3]))
63 def testCalculateMeanShortList(self):
64 """Tests the CalculateMean function with a short list."""
65 self.assertEqual(0.5, bisect_perf_module.CalculateMean([-3, 0, 1, 4]))
67 def testCalculateMeanCompareAlternateImplementation(self):
68 """Tests CalculateMean by comparing against an alternate implementation."""
69 def AlternateMeanFunction(values):
70 """Simple arithmetic mean function."""
71 return sum(values) / float(len(values))
72 test_values_lists = [[1], [5, 6.5, 1.2, 3], [-3, 0, 1, 4],
73 [-3, -1, 0.12, 0.752, 3.33, 8, 16, 32, 439]]
74 for values in test_values_lists:
76 AlternateMeanFunction(values),
77 bisect_perf_module.CalculateMean(values))
79 def testCalculateConfidence(self):
80 """Tests the confidence calculation."""
81 bad_values = [[0, 1], [1, 2]]
82 good_values = [[6, 7], [7, 8]]
83 # Closest means are mean(1, 2) and mean(6, 7).
85 # Standard deviation of [n-1, n, n, n+1] is 0.8165.
86 stddev_sum = 0.8165 + 0.8165
87 # Expected confidence is an int in the range [0, 100].
88 expected_confidence = min(100, int(100 * distance / float(stddev_sum)))
91 bisect_perf_module.CalculateConfidence(bad_values, good_values))
93 def testCalculateConfidence0(self):
94 """Tests the confidence calculation when it's expected to be 0."""
95 bad_values = [[0, 1], [1, 2], [4, 5], [0, 2]]
96 good_values = [[4, 5], [6, 7], [7, 8]]
97 # Both groups have value lists with means of 4.5, which means distance
98 # between groups is zero, and thus confidence is zero.
100 0, bisect_perf_module.CalculateConfidence(bad_values, good_values))
102 def testCalculateConfidence100(self):
103 """Tests the confidence calculation when it's expected to be 100."""
104 bad_values = [[1, 1], [1, 1]]
105 good_values = [[1.2, 1.2], [1.2, 1.2]]
106 # Standard deviation in both groups is zero, so confidence is 100.
108 100, bisect_perf_module.CalculateConfidence(bad_values, good_values))
110 def testCalculateRelativeChange(self):
111 """Tests the common cases for calculating relative change."""
112 # The change is relative to the first value, regardless of which is bigger.
113 self.assertEqual(0.5, bisect_perf_module.CalculateRelativeChange(1.0, 1.5))
114 self.assertEqual(0.5, bisect_perf_module.CalculateRelativeChange(2.0, 1.0))
116 def testCalculateRelativeChangeFromZero(self):
117 """Tests what happens when relative change from zero is calculated."""
118 # If the first number is zero, then the result is not a number.
119 self.assertEqual(0, bisect_perf_module.CalculateRelativeChange(0, 0))
121 math.isnan(bisect_perf_module.CalculateRelativeChange(0, 1)))
123 math.isnan(bisect_perf_module.CalculateRelativeChange(0, -1)))
125 def testCalculateRelativeChangeWithNegatives(self):
126 """Tests that relative change given is always positive."""
127 self.assertEqual(3.0, bisect_perf_module.CalculateRelativeChange(-1, 2))
128 self.assertEqual(3.0, bisect_perf_module.CalculateRelativeChange(1, -2))
129 self.assertEqual(1.0, bisect_perf_module.CalculateRelativeChange(-1, -2))
132 if __name__ == '__main__':