ms: Create a cumulative distribution function class
We are using the CDF to decide which percentage of the jobs should
be running at a given point. The x-axis is time and the y-axis the
percentage of how many jobs should be running.
There are three functions to do this. The first one is a constant
which would result in everything being started right now, one to
start them linearly and the last (formula from Qt/3rdparty) to first
accelerate and decelerate slowly.
Change-Id: I9e3064f4c3c4c7af5d3491f850090516e541f4d3
diff --git a/selftest/cdf_test.py b/selftest/cdf_test.py
new file mode 100755
index 0000000..8d837c1
--- /dev/null
+++ b/selftest/cdf_test.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python3
+
+import _prep
+
+from osmo_ms_driver import cdf
+from datetime import timedelta
+
+def print_fuzzy_compare(want, expe, len=3):
+ want_str = str(want)[0:len]
+ expe_str = str(expe)[0:len]
+ print(want_str, expe_str, want_str == expe_str)
+
+
+def check_steps(a, steps, fun):
+ print("Done", a.is_done())
+ for step in steps:
+ # Verify we can step
+
+ # Compare and step once
+ fun(a, step)
+ if a.is_done():
+ break
+ a.step_once()
+ print("Done", a.is_done())
+
+def compare_value(a, step):
+ print_fuzzy_compare(a.current_value(), step)
+
+def compare_scaled_value(a, val):
+ (step, scale) = val
+ print_fuzzy_compare(a.current_value(), step)
+ print_fuzzy_compare(a.current_scaled_value(), scale)
+
+def compare_x_value(a, val):
+ (x, step) = val
+ print(a._x, x, x == a._x)
+ print_fuzzy_compare(a.current_value(), step)
+
+def testImmediate():
+ print("Testing the immediate CDF")
+ a = cdf.immediate()
+ print("Done", a.is_done())
+ print_fuzzy_compare(a.current_value(), 1.0)
+
+
+def testLinearWithDuration():
+ print("Testing linear with duration")
+ a = cdf.linear_with_duration(timedelta(seconds=10), step_size=timedelta(seconds=2))
+ steps = [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]
+ check_steps(a, steps, compare_value)
+
+ print("Testing linear with duration scaled")
+ a = cdf.linear_with_duration(timedelta(seconds=10), step_size=timedelta(seconds=2))
+ a.set_target(1000)
+ steps = [(0.0, 0.0), (0.2, 200), (0.4, 400), (0.6, 600), (0.8, 800), (1.0, 10000)]
+ check_steps(a, steps, compare_scaled_value)
+
+def testInOut():
+ print("Testing in_out")
+ print_fuzzy_compare(cdf._in_out(0.5), 0.5, 3)
+ print_fuzzy_compare(cdf._in_out(0.75), 0.875, 4)
+ print_fuzzy_compare(cdf._in_out(0.8), 0.92, 3)
+ print_fuzzy_compare(cdf._in_out(0.85), 0.955, 4)
+ print_fuzzy_compare(cdf._in_out(1.0), 1.0, 3)
+
+def testEaseInOutDuration():
+ print("Testing ease In and Out")
+ a = cdf.ease_in_out_duration(duration=timedelta(seconds=20), step_size=timedelta(seconds=5))
+ steps = [(0.0, 0.0), (5.0, 0.125), (10.0, 0.5), (15.0, 0.875), (20, 1.0)]
+ check_steps(a, steps, compare_x_value)
+
+testImmediate()
+testLinearWithDuration()
+testInOut()
+testEaseInOutDuration()