OLD | NEW |
---|---|
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
tonyg
2014/03/12 15:28:38
This file needs to move along with statistics.py.
| |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import unittest | 5 import unittest |
6 import random | 6 import random |
7 | 7 |
8 from metrics import statistics | 8 from telemetry.util import statistics |
9 | 9 |
10 | 10 |
11 def Relax(samples, iterations=10): | 11 def Relax(samples, iterations=10): |
12 """Lloyd relaxation in 1D. | 12 """Lloyd relaxation in 1D. |
13 | 13 |
14 Keeps the position of the first and last sample. | 14 Keeps the position of the first and last sample. |
15 """ | 15 """ |
16 for _ in xrange(0, iterations): | 16 for _ in xrange(0, iterations): |
17 voronoi_boundaries = [] | 17 voronoi_boundaries = [] |
18 for i in xrange(1, len(samples)): | 18 for i in xrange(1, len(samples)): |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
151 # When the given percentage is very low, the lowest value is given. | 151 # When the given percentage is very low, the lowest value is given. |
152 self.assertEquals(1, statistics.Percentile([2, 1, 5, 4, 3], 5)) | 152 self.assertEquals(1, statistics.Percentile([2, 1, 5, 4, 3], 5)) |
153 # When the given percentage is very high, the highest value is given. | 153 # When the given percentage is very high, the highest value is given. |
154 self.assertEquals(5, statistics.Percentile([5, 2, 4, 1, 3], 95)) | 154 self.assertEquals(5, statistics.Percentile([5, 2, 4, 1, 3], 95)) |
155 # Linear interpolation between closest ranks is used. Using the example | 155 # Linear interpolation between closest ranks is used. Using the example |
156 # from <http://en.wikipedia.org/wiki/Percentile>: | 156 # from <http://en.wikipedia.org/wiki/Percentile>: |
157 self.assertEquals(27.5, statistics.Percentile([15, 20, 35, 40, 50], 40)) | 157 self.assertEquals(27.5, statistics.Percentile([15, 20, 35, 40, 50], 40)) |
158 | 158 |
159 def testArithmeticMean(self): | 159 def testArithmeticMean(self): |
160 # The ArithmeticMean function computes the simple average. | 160 # The ArithmeticMean function computes the simple average. |
161 self.assertAlmostEquals(40/3.0, statistics.ArithmeticMean([10, 10, 20], 3)) | 161 self.assertAlmostEquals(40/3.0, statistics.ArithmeticMean([10, 10, 20])) |
162 self.assertAlmostEquals(15.0, statistics.ArithmeticMean([10, 20], 2)) | 162 self.assertAlmostEquals(15.0, statistics.ArithmeticMean([10, 20])) |
163 # Both lists of values or single values can be given for either argument. | |
164 self.assertAlmostEquals(40/3.0, statistics.ArithmeticMean(40, [1, 1, 1])) | |
165 # If the 'count' is zero, then zero is returned. | 163 # If the 'count' is zero, then zero is returned. |
166 self.assertEquals(0, statistics.ArithmeticMean(4.0, 0)) | 164 self.assertEquals(0, statistics.ArithmeticMean([])) |
167 self.assertEquals(0, statistics.ArithmeticMean(4.0, [])) | |
168 | 165 |
169 def testDurationsDiscrepancy(self): | 166 def testDurationsDiscrepancy(self): |
170 durations = [] | 167 durations = [] |
171 d = statistics.DurationsDiscrepancy(durations) | 168 d = statistics.DurationsDiscrepancy(durations) |
172 self.assertEquals(d, 1.0) | 169 self.assertEquals(d, 1.0) |
173 | 170 |
174 durations = [4] | 171 durations = [4] |
175 d = statistics.DurationsDiscrepancy(durations) | 172 d = statistics.DurationsDiscrepancy(durations) |
176 self.assertEquals(d, 1.0) | 173 self.assertEquals(d, 1.0) |
177 | 174 |
178 durations_a = [1, 1, 1, 1, 1] | 175 durations_a = [1, 1, 1, 1, 1] |
179 durations_b = [1, 1, 2, 1, 1] | 176 durations_b = [1, 1, 2, 1, 1] |
180 durations_c = [1, 2, 1, 2, 1] | 177 durations_c = [1, 2, 1, 2, 1] |
181 | 178 |
182 d_a = statistics.DurationsDiscrepancy(durations_a) | 179 d_a = statistics.DurationsDiscrepancy(durations_a) |
183 d_b = statistics.DurationsDiscrepancy(durations_b) | 180 d_b = statistics.DurationsDiscrepancy(durations_b) |
184 d_c = statistics.DurationsDiscrepancy(durations_c) | 181 d_c = statistics.DurationsDiscrepancy(durations_c) |
185 | 182 |
186 self.assertTrue(d_a < d_b < d_c) | 183 self.assertTrue(d_a < d_b < d_c) |
OLD | NEW |