Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / tools / bisect-perf-regression_test.py
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.
4
5 import math
6 import unittest
7
8 # Special import necessary because filename contains dash characters.
9 bisect_perf_module = __import__('bisect-perf-regression')
10
11
12 class BisectPerfRegressionTest(unittest.TestCase):
13   """Test case for top-level functions in the bisect-perf-regrssion module."""
14
15   def setUp(self):
16     """Sets up the test environment before each test method."""
17     pass
18
19   def tearDown(self):
20     """Cleans up the test environment after each test method."""
21     pass
22
23   def testParseDEPSStringManually(self):
24     """Tests DEPS parsing."""
25     bisect_options = bisect_perf_module.BisectOptions()
26     bisect_instance = bisect_perf_module.BisectPerformanceMetrics(
27         None, bisect_options)
28
29     deps_file_contents = """
30 vars = {
31     'ffmpeg_hash':
32          '@ac4a9f31fe2610bd146857bbd55d7a260003a888',
33     'webkit_url':
34          'https://chromium.googlesource.com/chromium/blink.git',
35     'git_url':
36          'https://chromium.googlesource.com',
37     'webkit_rev':
38          '@e01ac0a267d1017288bc67fa3c366b10469d8a24',
39     'angle_revision':
40          '74697cf2064c0a2c0d7e1b1b28db439286766a05'
41 }"""
42
43     # Should only expect svn/git revisions to come through, and urls to be
44     # filtered out.
45     expected_vars_dict = {
46         'ffmpeg_hash': '@ac4a9f31fe2610bd146857bbd55d7a260003a888',
47         'webkit_rev': '@e01ac0a267d1017288bc67fa3c366b10469d8a24',
48         'angle_revision': '74697cf2064c0a2c0d7e1b1b28db439286766a05'
49     }
50     vars_dict = bisect_instance._ParseRevisionsFromDEPSFileManually(
51         deps_file_contents)
52     self.assertEqual(vars_dict, expected_vars_dict)
53
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)
58
59   def testCalculateMeanSingleNum(self):
60     """Tests the CalculateMean function with a single number."""
61     self.assertEqual(3.0, bisect_perf_module.CalculateMean([3]))
62
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]))
66
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:
75       self.assertEqual(
76           AlternateMeanFunction(values),
77           bisect_perf_module.CalculateMean(values))
78
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).
84     distance = 6.5 - 1.5
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)))
89     self.assertEqual(
90         expected_confidence,
91         bisect_perf_module.CalculateConfidence(bad_values, good_values))
92
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.
99     self.assertEqual(
100         0, bisect_perf_module.CalculateConfidence(bad_values, good_values))
101
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.
107     self.assertEqual(
108         100, bisect_perf_module.CalculateConfidence(bad_values, good_values))
109
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))
115
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))
120     self.assertTrue(
121         math.isnan(bisect_perf_module.CalculateRelativeChange(0, 1)))
122     self.assertTrue(
123         math.isnan(bisect_perf_module.CalculateRelativeChange(0, -1)))
124
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))
130
131
132 if __name__ == '__main__':
133   unittest.main()