Signed-off-by: Jiří Župka <jzu...@redhat.com>
---
 client/shared/base_utils.py          |   34 ++++++++++++++++++++++++++++++++++
 client/shared/base_utils_unittest.py |   19 +++++++++++++++++++
 2 files changed, 53 insertions(+), 0 deletions(-)

diff --git a/client/shared/base_utils.py b/client/shared/base_utils.py
index 8e38413..581aef0 100644
--- a/client/shared/base_utils.py
+++ b/client/shared/base_utils.py
@@ -400,6 +400,40 @@ def matrix_to_string(matrix, header=None):
     return matrix_str
 
 
+class Statistic(object):
+    """
+    Class collect information about migration run.
+    """
+    def __init__(self):
+        self._sum = 0
+        self._count = 0
+        self._max = None
+        self._min = None
+
+    def get_average(self):
+        if self._count != 0:
+            return self._sum / self._count
+        else:
+            return None
+
+    def get_min(self):
+        return self._min
+
+    def get_max(self):
+        return self._max
+
+    def record(self, value):
+        """
+        Record new value to statistic.
+        """
+        self._count += 1
+        self._sum += value
+        if not self._max or self._max < value:
+            self._max = value
+        if not self._min or self._min > value:
+            self._min = value
+
+
 def read_keyval(path):
     """
     Read a key-value pair format file into a dictionary, and return it.
diff --git a/client/shared/base_utils_unittest.py 
b/client/shared/base_utils_unittest.py
index a78cdb6..df4303b 100755
--- a/client/shared/base_utils_unittest.py
+++ b/client/shared/base_utils_unittest.py
@@ -150,6 +150,25 @@ class test_open_write_close(unittest.TestCase):
         self.assertEqual(data, test_file.final_data)
 
 
+class test_Statisctic(unittest.TestCase):
+    def setUp(self):
+        self.god = mock.mock_god(ut=self)
+
+
+    def tearDown(self):
+        self.god.unstub_all()
+
+
+    def test_simple_functionality(self):
+        stat = base_utils.Statistic()
+        stat.record(5)
+        stat.record(15)
+        stat.record(10)
+        self.assertEqual(10, stat.get_average())
+        self.assertEqual(5, stat.get_min())
+        self.assertEqual(15, stat.get_max())
+
+
 class test_read_keyval(unittest.TestCase):
     def setUp(self):
         self.god = mock.mock_god(ut=self)
-- 
1.7.7.6

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to