[Autotest][PATCH 2/7] shared: Adds class for collecting a simple statistics.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Jiří Župka <jzupka@xxxxxxxxxx>
---
 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 majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux