Refactor: add _get_timestamps() 35/66735/3
authorEmma Foley <emma.l.foley@intel.com>
Thu, 24 Jan 2019 11:35:34 +0000 (11:35 +0000)
committerEmma Foley <emma.l.foley@intel.com>
Wed, 30 Jan 2019 14:58:56 +0000 (14:58 +0000)
JIRA: YARDSTICK-1593
Change-Id: I3ec352dd577c6030fa86a2817e264ba7c80773f1
Signed-off-by: Emma Foley <emma.l.foley@intel.com>
yardstick/benchmark/core/report.py
yardstick/tests/unit/benchmark/core/test_report.py

index 0819cd4..b7d2fd0 100644 (file)
@@ -1,6 +1,6 @@
 ##############################################################################
 # Copyright (c) 2017 Rajesh Kudaka <4k.rajesh@gmail.com>
-# Copyright (c) 2018 Intel Corporation.
+# Copyright (c) 2018-2019 Intel Corporation.
 #
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Apache License, Version 2.0
@@ -121,6 +121,25 @@ class Report(object):
         else:
             raise KeyError("Task ID or Test case not found.")
 
+    def _get_trimmed_timestamp(self, metric_time, resolution=4):
+        if not isinstance(metric_time, str):
+            metric_time = metric_time.encode('utf8') # PY2: unicode to str
+        metric_time = metric_time[11:]               # skip date, keep time
+        head, _, tail = metric_time.partition('.')   # split HH:MM:SS & nsZ
+        metric_time = head + '.' + tail[:resolution] # join HH:MM:SS & .us
+        return metric_time
+
+    def _get_timestamps(self, metrics, resolution=6):
+        # Extract the timestamps from a list of metrics
+        timestamps = []
+        for metric in metrics:
+            metric_time = self._get_trimmed_timestamp(
+                metric['time'], resolution)
+            timestamps.append(metric_time)               # HH:MM:SS.micros
+        return timestamps
+
+    @cliargs("task_id", type=str, help=" task id", nargs=1)
+    @cliargs("yaml_name", type=str, help=" Yaml file Name", nargs=1)
     def _generate_common(self, args):
         """Actions that are common to both report formats.
 
@@ -147,15 +166,7 @@ class Report(object):
                            for field in db_fieldkeys]]
 
         # extract timestamps
-        self.Timestamp = []
-        for metric in db_metrics:
-            metric_time = metric['time']                    # in RFC3339 format
-            if not isinstance(metric_time, str):
-                metric_time = metric_time.encode('utf8')    # PY2: unicode to str
-            metric_time = metric_time[11:]                  # skip date, keep time
-            head, _, tail = metric_time.partition('.')      # split HH:MM:SS and nsZ
-            metric_time = head + '.' + tail[:6]             # join HH:MM:SS and .us
-            self.Timestamp.append(metric_time)              # HH:MM:SS.micros
+        self.Timestamp = self._get_timestamps(db_metrics)
 
         # prepare return values
         datasets = []
index 4683c26..b498299 100644 (file)
@@ -1,6 +1,6 @@
 ##############################################################################
 # Copyright (c) 2017 Rajesh Kudaka.
-# Copyright (c) 2018 Intel Corporation.
+# Copyright (c) 2018-2019 Intel Corporation.
 #
 # All rights reserved. This program and the accompanying materials
 # are made available under the terms of the Apache License, Version 2.0
@@ -219,6 +219,14 @@ class ReportTestCase(unittest.TestCase):
         self.rep.task_id = GOOD_TASK_ID
         six.assertRaisesRegex(self, KeyError, "Task ID", self.rep._get_metrics)
 
+    def test__get_timestamps(self):
+
+        metrics = MORE_DB_METRICS
+        self.assertEqual(
+            MORE_TIMESTAMP,
+            self.rep._get_timestamps(metrics)
+        )
+
     @mock.patch.object(report.Report, '_get_metrics')
     @mock.patch.object(report.Report, '_get_fieldkeys')
     def test__generate_common(self, mock_keys, mock_metrics):