diff options
author | Tim Rault <tim.rault@cengn.ca> | 2016-07-12 15:30:51 -0400 |
---|---|---|
committer | Tim Rault <tim.rault@cengn.ca> | 2016-07-14 15:45:46 -0400 |
commit | 8f9351d780beb18aa70e2d84f6e249a0a29489cf (patch) | |
tree | f1cfa086e294c26f3e88d7cd984bd424239daa45 | |
parent | d7d5efb6fe647e117f54ce0923e4966014eaeb9a (diff) |
Add Range function for Steady State detection
Added a range_value function in utilities/math.py able to compute the range
of a series of y values : [y1, y2, ..., yn].
Implemented a test harness for this range_value function in the tests/utilities
section.
Renamed the math_slope.py and math_range.py test files to add _test.py for
Jenkins.
Cleaned up the code so it is compliant to the pep8 rules.
Renamed the previous 'math' modules (storperf/utilities/math.py
and storperf/test/utilities/math.py) as 'math_slope' to be
coherent with the new notation.
Change-Id: I02ccd2b87f0b72e7a28c416b593aae4d8ad97961
JIRA: STORPERF-57
JIRA: STORPERF-58
Signed-off-by: Tim Rault <tim.rault@cengn.ca>
-rw-r--r-- | storperf/tests/utilities/math_range_test.py | 120 | ||||
-rw-r--r-- | storperf/tests/utilities/math_slope_test.py (renamed from storperf/tests/utilities/math.py) | 23 | ||||
-rw-r--r-- | storperf/utilities/math.py | 100 |
3 files changed, 200 insertions, 43 deletions
diff --git a/storperf/tests/utilities/math_range_test.py b/storperf/tests/utilities/math_range_test.py new file mode 100644 index 0000000..6484752 --- /dev/null +++ b/storperf/tests/utilities/math_range_test.py @@ -0,0 +1,120 @@ +############################################################################## +# Copyright (c) 2016 CENGN and others. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## +from random import uniform, randrange +import unittest + +from storperf.utilities import math as math + + +class MathRangeTest(unittest.TestCase): + + def setUp(self): + unittest.TestCase.setUp(self) + + def test_empty_series(self): + expected = 0 + data_series = [] + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_integer_series(self): + expected = 11946 + data_series = [5, 351, 847, 2, 1985, 18, + 96, 389, 687, 1, 11947, 758, 155] + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_float_series_1_decimal(self): + expected = 778595.5 + data_series = [736.4, 9856.4, 684.2, 0.3, 0.9, 778595.8] + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_float_series_2_decimals(self): + expected = 5693.47 + data_series = [51.36, 78.40, 1158.24, 5.50, 0.98, 5694.45] + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_float_series_3_decimals(self): + expected = 992.181 + data_series = [4.562, 12.582, 689.452, + 135.162, 996.743, 65.549, 36.785] + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_float_series_4_decimals(self): + expected = 122985.3241 + data_series = [39.4785, 896.7845, 11956.3654, + 44.2398, 6589.7134, 0.3671, 122985.6912] + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_float_series_5_decimals(self): + expected = 8956208.84494 + data_series = [12.78496, 55.91275, 668.94378, + 550396.5671, 512374.9999, 8956221.6299] + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_float_series_10_decimals(self): + expected = 5984.507397972699 + data_series = [1.1253914785, 5985.6327894512, + 256.1875693287, 995.8497623415] + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_float_mix(self): + expected = 60781.6245372199 + data_series = [60785.9962, 899.4, 78.66, 69.58, 4.93795, + 587.195486, 96.7694536, 5.13755964, + 33.333333334, 60786.5624872199] + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_float_integer_mix(self): + expected = 460781.05825 + data_series = [460785.9962, 845.634, 24.1, 69.58, 89, 4.93795] + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_negative_values(self): + expected = 596.78163 + data_series = [-4.655, -33.3334, -596.78422, -0.00259, -66.785] + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_negative_positive_mix(self): + expected = 58.859500000000004 + data_series = [6.85698, -2.8945, 0, -0.15, 55.965] + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_single_element(self): + expected = 0 + data_series = [2.265] + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_10000_values_processing(self): + expected = 28001.068 + data_series = [uniform(-10000, 10000) for i in xrange(10000)] + data_series.insert(randrange(len(data_series) + 1), 15000.569) + data_series.insert(randrange(len(data_series) + 1), -13000.499) + actual = math.range_value(data_series) + self.assertEqual(expected, actual) + + def test_processing_100_values_100_times(self): + expected = 35911.3134 + for index in range(1, 100): + data_series = [uniform(-10000, 10000) for i in xrange(100)] + data_series.insert(randrange(len(data_series) + 1), 16956.3334) + data_series.insert(randrange(len(data_series) + 1), -18954.98) + actual = math.range_value(data_series) + self.assertEqual(expected, actual) diff --git a/storperf/tests/utilities/math.py b/storperf/tests/utilities/math_slope_test.py index c78538d..a34845b 100644 --- a/storperf/tests/utilities/math.py +++ b/storperf/tests/utilities/math_slope_test.py @@ -7,9 +7,10 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## import unittest -from storperf.utilities.math import math +from storperf.utilities import math as math -class MathTest(unittest.TestCase): + +class MathSlopeTest(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) @@ -22,45 +23,45 @@ class MathTest(unittest.TestCase): def test_slope_integer_series(self): expected = 1.4 - actual = math.slope([[1,6], [2,5], [3,7], [4,10]]) + actual = math.slope([[1, 6], [2, 5], [3, 7], [4, 10]]) self.assertEqual(expected, actual) def test_slope_decimal_series(self): expected = 1.4 - actual = math.slope([[1.0,6.0], [2.0,5.0], [3.0,7.0], [4.0,10.0]]) + actual = math.slope([[1.0, 6.0], [2.0, 5.0], [3.0, 7.0], [4.0, 10.0]]) self.assertEqual(expected, actual) def test_slope_decimal_integer_mix(self): expected = 1.4 - actual = math.slope([[1.0,6], [2,5.0], [3,7], [4.0,10]]) + actual = math.slope([[1.0, 6], [2, 5.0], [3, 7], [4.0, 10]]) self.assertEqual(expected, actual) def test_slope_negative_y_series(self): expected = 2 - actual = math.slope([[1.0,-2], [2,2], [3,2]]) + actual = math.slope([[1.0, -2], [2, 2], [3, 2]]) self.assertEqual(expected, actual) def test_slope_negative_x_series(self): expected = 1.4 - actual = math.slope([[-24,6.0], [-23,5], [-22,7.0], [-21,10]]) + actual = math.slope([[-24, 6.0], [-23, 5], [-22, 7.0], [-21, 10]]) self.assertEqual(expected, actual) def test_slope_out_of_order_series(self): expected = 1.4 - actual = math.slope([[2,5.0], [4,10], [3.0,7], [1,6]]) + actual = math.slope([[2, 5.0], [4, 10], [3.0, 7], [1, 6]]) self.assertEqual(expected, actual) def test_slope_0_in_y(self): expected = -0.5 - actual = math.slope([[15.5,1], [16.5,0], [17.5,0]]) + actual = math.slope([[15.5, 1], [16.5, 0], [17.5, 0]]) self.assertEqual(expected, actual) def test_slope_0_in_x(self): expected = 1.4 - actual = math.slope([[0,6.0], [1,5], [2,7], [3,10]]) + actual = math.slope([[0, 6.0], [1, 5], [2, 7], [3, 10]]) self.assertEqual(expected, actual) def test_slope_0_in_x_and_y(self): expected = 1.5 - actual = math.slope([[0.0,0], [1,1], [2,3]]) + actual = math.slope([[0.0, 0], [1, 1], [2, 3]]) self.assertEqual(expected, actual) diff --git a/storperf/utilities/math.py b/storperf/utilities/math.py index 3b124cd..031fc3e 100644 --- a/storperf/utilities/math.py +++ b/storperf/utilities/math.py @@ -7,46 +7,82 @@ # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################## -class math(object): - @staticmethod - def slope(data_series): +def slope(data_series): + """ + This function implements the linear least squares algorithm described in + the following wikipedia article : + https://en.wikipedia.org/wiki/Linear_least_squares_(mathematics) + in the case of m equations (provided by m data points) and 2 unknown + variables (x and y, which represent the time and the Volume performance + variable being tested e.g. IOPS, latency...). + The data_series is currently assumed to follow the pattern : + [[x1,y1], [x2,y2], ..., [xm,ym]]. + If this data pattern were to change, the data_treatement function + should be adjusted to ensure compatibility with the rest of the + Steady State Dectection module. + """ + + # In the particular case of an empty data series + if len(data_series) == 0: + beta2 = 0 + + else: # The general case + m = len(data_series) + # To make sure at least one element is a float number so the result + # of the algorithm be a float number + data_series[0][0] = float(data_series[0][0]) + """ - This function implements the linear least squares algorithm described in the following wikipedia article - https://en.wikipedia.org/wiki/Linear_least_squares_(mathematics) - in the case of m equations (provided by m data points) and 2 unknown variables (x and - y, which represent the time and the Volume performance variable being - tested e.g. IOPS, latency...) + It consists in solving the normal equations system (2 equations, + 2 unknowns) by calculating the value of beta2 (slope). + The formula of beta1 (the y-intercept) is given as a comment in + case it is needed later. """ + sum_xi = 0 + sum_xi_sq = 0 + sum_yi_xi = 0 + sum_yi = 0 + for i in range(0, m): + xi = data_series[i][0] + yi = data_series[i][1] - if len(data_series)==0: #In the particular case of an empty data series - beta2 = 0 + sum_xi += xi + sum_xi_sq += xi**2 + sum_yi_xi += xi * yi + sum_yi += yi - else: #The general case - m = len(data_series) #given a [[x1,y1], [x2,y2], ..., [xm,ym]] data series - data_series[0][0] = float(data_series[0][0]) #To make sure at least one element is a float number so the result of the algorithm be a float number + beta2 = (sum_yi * sum_xi - m * sum_yi_xi) / \ + (sum_xi**2 - m * sum_xi_sq) # The slope + # beta1 = (sum_yi_xi - beta2*sum_xi_sq)/sum_xi #The y-intercept if + # needed - """ - It consists in solving the normal equations system (2 equations, 2 unknowns) - by calculating the value of beta2 (slope). The formula of beta1 (the y-intercept) - is given as a comment in case it is needed later. - """ - sum_xi = 0 - sum_xi_sq = 0 - sum_yi_xi = 0 - sum_yi = 0 - for i in range(0, m): - xi = data_series[i][0] - yi = data_series[i][1] + return beta2 - sum_xi += xi - sum_xi_sq += xi**2 - sum_yi_xi += xi*yi - sum_yi += yi - beta2 = (sum_yi*sum_xi - m*sum_yi_xi)/(sum_xi**2 - m*sum_xi_sq) #The slope - #beta1 = (sum_yi_xi - beta2*sum_xi_sq)/sum_xi #The y-intercept if needed +def range_value(data_series): + """ + This function implements a range algorithm that returns a float number + representing the range of the data_series that is passed to it. + The data_series being passed is assumed to follow the following data + pattern : [y1, y2, y3, ..., ym] where yi represents the ith + measuring point of the y variable. The y variable represents the + Volume performance being tested (e.g. IOPS, latency...). + If this data pattern were to change, the data_treatment function + should be adjusted to ensure compatibility with the rest of the + Steady State Dectection module. + The conversion of the data series from the original pattern to the + [y1, y2, y3, ..., ym] pattern is done outside this function + so the original pattern can be changed without breaking this function. + """ - return beta2 + # In the particular case of an empty data series + if len(data_series) == 0: + range_value = 0 + else: # The general case + max_value = max(data_series) + min_value = min(data_series) + range_value = max_value - min_value + return range_value |