From 0738c2544e3220a3e0fd80a77ab1f2800f90533e Mon Sep 17 00:00:00 2001 From: Emma Foley Date: Thu, 24 Jan 2019 15:19:44 +0000 Subject: Refactor: add _format_datasets JIRA: YARDSTICK-1593 Change-Id: I8d2c8665d767c92da20db8f97690f20da4a68908 Signed-off-by: Emma Foley --- yardstick/benchmark/core/report.py | 72 ++++++++++++---------- yardstick/tests/unit/benchmark/core/test_report.py | 60 ++++++++++++++++-- 2 files changed, 97 insertions(+), 35 deletions(-) (limited to 'yardstick') diff --git a/yardstick/benchmark/core/report.py b/yardstick/benchmark/core/report.py index b7d2fd02b..30ba243c3 100644 --- a/yardstick/benchmark/core/report.py +++ b/yardstick/benchmark/core/report.py @@ -138,6 +138,46 @@ class Report(object): timestamps.append(metric_time) # HH:MM:SS.micros return timestamps + def _format_datasets(self, metric_name, metrics): + values = [] + for metric in metrics: + val = metric.get(metric_name, None) + if val is None: + # keep explicit None or missing entry as is + pass + elif isinstance(val, (int, float)): + # keep plain int or float as is + pass + elif six.PY2 and isinstance(val, + long): # pylint: disable=undefined-variable + # PY2: long value would be rendered with trailing L, + # which JS does not support, so convert it to float + val = float(val) + elif isinstance(val, six.string_types): + s = val + if not isinstance(s, str): + s = s.encode('utf8') # PY2: unicode to str + try: + # convert until failure + val = s + val = float(s) + val = int(s) + if six.PY2 and isinstance(val, + long): # pylint: disable=undefined-variable + val = float(val) # PY2: long to float + except ValueError: + # value may have been converted to a number + pass + finally: + # if val was not converted into a num, then it must be + # text, which shouldn't end up in the report + if isinstance(val, six.string_types): + val = None + else: + raise ValueError("Cannot convert %r" % val) + values.append(val) + return values + @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): @@ -174,37 +214,7 @@ class Report(object): # extract and convert field values for key in field_keys: - values = [] - for metric in db_metrics: - val = metric.get(key, None) - if val is None: - # keep explicit None or missing entry as is - pass - elif isinstance(val, (int, float)): - # keep plain int or float as is - pass - elif six.PY2 and isinstance(val, - long): # pylint: disable=undefined-variable - # PY2: long value would be rendered with trailing L, - # which JS does not support, so convert it to float - val = float(val) - elif isinstance(val, six.string_types): - s = val - if not isinstance(s, str): - s = s.encode('utf8') # PY2: unicode to str - try: - # convert until failure - val = s - val = float(s) - val = int(s) - if six.PY2 and isinstance(val, - long): # pylint: disable=undefined-variable - val = float(val) # PY2: long to float - except ValueError: - pass - else: - raise ValueError("Cannot convert %r" % val) - values.append(val) + values = self._format_datasets(key, db_metrics) datasets.append({'label': key, 'data': values}) table_vals[key] = values diff --git a/yardstick/tests/unit/benchmark/core/test_report.py b/yardstick/tests/unit/benchmark/core/test_report.py index b498299a9..77e949c66 100644 --- a/yardstick/tests/unit/benchmark/core/test_report.py +++ b/yardstick/tests/unit/benchmark/core/test_report.py @@ -77,10 +77,10 @@ MORE_EXPECTED_TABLE_VALS = { 123, 4.56, 9876543210987654321 if six.PY3 else 9.876543210987655e+18, - 'str_str value', - 'str_unicode value', - 'unicode_str value', - 'unicode_unicode value', + None, + None, + None, + None, 7.89, 1011, 9876543210123456789 if six.PY3 else 9.876543210123457e+18, @@ -227,6 +227,58 @@ class ReportTestCase(unittest.TestCase): self.rep._get_timestamps(metrics) ) + def test__format_datasets(self): + metric_name = "free.memory0.used" + metrics = [{ + u'free.memory1.free': u'1958664', + u'free.memory0.used': u'9789560', + }, { + u'free.memory1.free': u'1958228', + u'free.memory0.used': u'9789790', + }, { + u'free.memory1.free': u'1956156', + u'free.memory0.used': u'9791092', + }, { + u'free.memory1.free': u'1956280', + u'free.memory0.used': u'9790796', + }] + self.assertEqual( + [9789560, 9789790, 9791092, 9790796,], + self.rep._format_datasets(metric_name, metrics) + ) + + def test__format_datasets_val_none(self): + metric_name = "free.memory0.used" + metrics = [{ + u'free.memory1.free': u'1958664', + u'free.memory0.used': 9876543109876543210, + }, { + u'free.memory1.free': u'1958228', + }, { + u'free.memory1.free': u'1956156', + u'free.memory0.used': u'9791092', + }, { + u'free.memory1.free': u'1956280', + u'free.memory0.used': u'9790796', + }] + + exp0 = 9876543109876543210 if six.PY3 else 9.876543109876543e+18 + self.assertEqual( + [exp0, None, 9791092, 9790796], + self.rep._format_datasets(metric_name, metrics) + ) + + def test__format_datasets_val_incompatible(self): + metric_name = "free.memory0.used" + metrics = [{ + u'free.memory0.used': "some incompatible value", + }, { + }] + self.assertEqual( + [None, None], + self.rep._format_datasets(metric_name, metrics) + ) + @mock.patch.object(report.Report, '_get_metrics') @mock.patch.object(report.Report, '_get_fieldkeys') def test__generate_common(self, mock_keys, mock_metrics): -- cgit 1.2.3-korg