From 25b21add71fcf7c2c795bd950b5117d69fac68fb Mon Sep 17 00:00:00 2001 From: rajesh_4k <4k.rajesh@gmail.com> Date: Mon, 27 Feb 2017 18:09:00 +0530 Subject: Yardstick: User interface for Yardstick. Currently Yardstick doesnt have any UI which gives detail analysis of the test-results. This commit generates a HTML page after the execution of a command "yardstick report generate " which intern can be executed after the execution of test-case. Used: Highcharts.js for the graphs. JIRA: YARDSTICK-280 Change-Id: Ic98cc348719f3922bff178f52e7944a4a931763a Signed-off-by: Rajesh K <4k.rajesh@gmail.com> --- .../user/userguide/Yardstick_user_interface.rst | 29 +++++ requirements.txt | 1 + tests/unit/benchmark/core/test_report.py | 72 +++++++++++ yardstick/benchmark/core/__init__.py | 2 + yardstick/benchmark/core/report.py | 126 +++++++++++++++++++ yardstick/benchmark/core/task.py | 4 + yardstick/cmd/cli.py | 4 +- yardstick/cmd/commands/report.py | 33 +++++ yardstick/common/constants.py | 2 + yardstick/common/html_template.py | 133 +++++++++++++++++++++ 10 files changed, 405 insertions(+), 1 deletion(-) create mode 100644 docs/testing/user/userguide/Yardstick_user_interface.rst create mode 100644 tests/unit/benchmark/core/test_report.py create mode 100644 yardstick/benchmark/core/report.py create mode 100644 yardstick/cmd/commands/report.py create mode 100644 yardstick/common/html_template.py diff --git a/docs/testing/user/userguide/Yardstick_user_interface.rst b/docs/testing/user/userguide/Yardstick_user_interface.rst new file mode 100644 index 000000000..9058dd46d --- /dev/null +++ b/docs/testing/user/userguide/Yardstick_user_interface.rst @@ -0,0 +1,29 @@ +Yardstick User Interface +======================== + +This interface provides a user to view the test result +in table format and also values pinned on to a graph. + + +Command +------- +:: + + yardstick report generate + + +Description +----------- + +1. When the command is triggered using the task-id and the testcase +name provided the respective values are retrieved from the +database (influxdb in this particular case). + +2. The values are then formatted and then provided to the html +template framed with complete html body using Django Framework. + +3. Then the whole template is written into a html file. + +The graph is framed with Timestamp on x-axis and output values +(differ from testcase to testcase) on y-axis with the help of +"Highcharts". diff --git a/requirements.txt b/requirements.txt index 3549f85e7..6e5792e1f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,6 +14,7 @@ cliff==2.0.0 cmd2==0.6.8 coverage==4.1b2 debtcollector==1.3.0 +django==1.8.17 ecdsa==0.13 extras==0.0.3 fixtures==1.4.0 diff --git a/tests/unit/benchmark/core/test_report.py b/tests/unit/benchmark/core/test_report.py new file mode 100644 index 000000000..69546928c --- /dev/null +++ b/tests/unit/benchmark/core/test_report.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python + +############################################################################## +# Copyright (c) 2017 Rajesh Kudaka. +# +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +# Unittest for yardstick.benchmark.core.report + +from __future__ import print_function + +from __future__ import absolute_import + +import unittest +import uuid + +try: + from unittest import mock +except ImportError: + import mock + +from yardstick.benchmark.core import report +from yardstick.cmd.commands import change_osloobj_to_paras + +FAKE_YAML_NAME = 'fake_name' +FAKE_TASK_ID = str(uuid.uuid4()) +FAKE_DB_FIELDKEYS = [{'fieldKey': 'fake_key'}] +FAKE_TIME = '0000-00-00T00:00:00.000000Z' +FAKE_DB_TASK = [{'fake_key': 0.000, 'time': FAKE_TIME}] +FAKE_TIMESTAMP = ['fake_time'] +DUMMY_TASK_ID = 'aaaaaa-aaaaaaaa-aaaaaaaaaa-aaaaaa' + + +class ReportTestCase(unittest.TestCase): + + def setUp(self): + super(ReportTestCase, self).setUp() + self.param = change_osloobj_to_paras({}) + self.param.yaml_name = [FAKE_YAML_NAME] + self.param.task_id = [FAKE_TASK_ID] + self.rep = report.Report() + + @mock.patch('yardstick.benchmark.core.report.Report._get_tasks') + @mock.patch('yardstick.benchmark.core.report.Report._get_fieldkeys') + @mock.patch('yardstick.benchmark.core.report.Report._validate') + def test_generate_success(self, mock_valid, mock_keys, mock_tasks): + mock_tasks.return_value = FAKE_DB_TASK + mock_keys.return_value = FAKE_DB_FIELDKEYS + self.rep.generate(self.param) + mock_valid.assert_called_once_with(FAKE_YAML_NAME, FAKE_TASK_ID) + self.assertEqual(1, mock_tasks.call_count) + self.assertEqual(1, mock_keys.call_count) + + def test_invalid_yaml_name(self): + self.assertRaisesRegexp(ValueError, "yaml*", self.rep._validate, + 'F@KE_NAME', FAKE_TASK_ID) + + def test_invalid_task_id(self): + self.assertRaisesRegexp(ValueError, "task*", self.rep._validate, + FAKE_YAML_NAME, DUMMY_TASK_ID) + + @mock.patch('api.utils.influx.query') + def test_task_not_found(self, mock_query): + mock_query.return_value = [] + self.rep.yaml_name = FAKE_YAML_NAME + self.rep.task_id = FAKE_TASK_ID + self.assertRaisesRegexp(KeyError, "Task ID", self.rep._get_fieldkeys) + self.assertRaisesRegexp(KeyError, "Task ID", self.rep._get_tasks) diff --git a/yardstick/benchmark/core/__init__.py b/yardstick/benchmark/core/__init__.py index 79ebc732f..70036ea1e 100644 --- a/yardstick/benchmark/core/__init__.py +++ b/yardstick/benchmark/core/__init__.py @@ -20,6 +20,8 @@ class Param(object): self.parse_only = kwargs.get('parse-only') self.output_file = kwargs.get('output-file', '/tmp/yardstick.out') self.suite = kwargs.get('suite') + self.task_id = kwargs.get('task_id') + self.yaml_name = kwargs.get('yaml_name') # list self.input_file = kwargs.get('input_file') diff --git a/yardstick/benchmark/core/report.py b/yardstick/benchmark/core/report.py new file mode 100644 index 000000000..997a125e7 --- /dev/null +++ b/yardstick/benchmark/core/report.py @@ -0,0 +1,126 @@ +############################################################################# +# Copyright (c) 2017 Rajesh Kudaka +# +# Author: Rajesh Kudaka 4k.rajesh@gmail.com +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +""" Handler for yardstick command 'report' """ + +from __future__ import print_function + +from __future__ import absolute_import + +import ast +import re +import uuid + +from api.utils import influx + +from django.conf import settings +from django.template import Context +from django.template import Template + +from oslo_utils import encodeutils +from oslo_utils import uuidutils +from yardstick.common import constants as consts +from yardstick.common.html_template import template +from yardstick.common.utils import cliargs + +settings.configure() + + +class Report(object): + """Report commands. + + Set of commands to manage benchmark tasks. + """ + + def __init__(self): + self.Timestamp = [] + self.yaml_name = "" + self.task_id = "" + + def _validate(self, yaml_name, task_id): + if re.match("^[a-z0-9_-]+$", yaml_name): + self.yaml_name = yaml_name + else: + raise ValueError("invalid yaml_name", yaml_name) + + if uuidutils.is_uuid_like(task_id): + task_id = '{' + task_id + '}' + task_uuid = (uuid.UUID(task_id)) + self.task_id = task_uuid + else: + raise ValueError("invalid task_id", task_id) + + def _get_fieldkeys(self): + fieldkeys_cmd = "show field keys from \"%s\"" + fieldkeys_query = fieldkeys_cmd % (self.yaml_name) + query_exec = influx.query(fieldkeys_query) + if query_exec: + return query_exec + else: + raise KeyError("Task ID or Test case not found..") + + def _get_tasks(self): + task_cmd = "select * from \"%s\" where task_id= '%s'" + task_query = task_cmd % (self.yaml_name, self.task_id) + query_exec = influx.query(task_query) + if query_exec: + return query_exec + else: + raise KeyError("Task ID or Test case not found..") + + @cliargs("task_id", type=str, help=" task id", nargs=1) + @cliargs("yaml_name", type=str, help=" Yaml file Name", nargs=1) + def generate(self, args): + """Start report generation.""" + self._validate(args.yaml_name[0], args.task_id[0]) + + self.db_fieldkeys = self._get_fieldkeys() + + self.db_task = self._get_tasks() + + field_keys = [] + temp_series = [] + table_vals = {} + + field_keys = [encodeutils.to_utf8(field['fieldKey']) + for field in self.db_fieldkeys] + + for key in field_keys: + self.Timestamp = [] + series = {} + values = [] + for task in self.db_task: + task_time = encodeutils.to_utf8(task['time']) + if not isinstance(task_time, str): + task_time = str(task_time, 'utf8') + key = str(key, 'utf8') + task_time = task_time[11:] + head, sep, tail = task_time.partition('.') + task_time = head + "." + tail[:6] + self.Timestamp.append(task_time) + if isinstance(task[key], float) is True: + values.append(task[key]) + else: + values.append(ast.literal_eval(task[key])) + table_vals['Timestamp'] = self.Timestamp + table_vals[key] = values + series['name'] = key + series['data'] = values + temp_series.append(series) + + Template_html = Template(template) + Context_html = Context({"series": temp_series, + "Timestamp": self.Timestamp, + "task_id": self.task_id, + "table": table_vals}) + with open(consts.DEFAULT_HTML_FILE, "w") as file_open: + file_open.write(Template_html.render(Context_html)) + + print("Report generated. View /tmp/yardstick.htm") diff --git a/yardstick/benchmark/core/task.py b/yardstick/benchmark/core/task.py index 2794d84f4..e23db075e 100644 --- a/yardstick/benchmark/core/task.py +++ b/yardstick/benchmark/core/task.py @@ -107,6 +107,10 @@ class Task(object): # pragma: no cover LOG.info("total finished in %d secs", total_end_time - total_start_time) + scenario = scenarios[0] + print("To generate report execute => yardstick report generate ", + scenario['task_id'], scenario['tc']) + print("Done, exiting") def _run(self, scenarios, run_in_parallel, output_file): diff --git a/yardstick/cmd/cli.py b/yardstick/cmd/cli.py index 1f8dfee2d..79f66e574 100644 --- a/yardstick/cmd/cli.py +++ b/yardstick/cmd/cli.py @@ -27,6 +27,7 @@ from yardstick.cmd.commands import scenario from yardstick.cmd.commands import testcase from yardstick.cmd.commands import plugin from yardstick.cmd.commands import env +from yardstick.cmd.commands import report CONF = cfg.CONF cli_opts = [ @@ -62,7 +63,8 @@ class YardstickCLI(): 'scenario': scenario.ScenarioCommands, 'testcase': testcase.TestcaseCommands, 'plugin': plugin.PluginCommands, - 'env': env.EnvCommand + 'env': env.EnvCommand, + 'report': report.ReportCommands } def __init__(self): diff --git a/yardstick/cmd/commands/report.py b/yardstick/cmd/commands/report.py new file mode 100644 index 000000000..87ae7d5f7 --- /dev/null +++ b/yardstick/cmd/commands/report.py @@ -0,0 +1,33 @@ +############################################################################## +# Copyright (c) 2017 Rajesh Kudaka. +# +# Author: Rajesh Kudaka (4k.rajesh@gmail.com) +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################## + +""" Handler for yardstick command 'report' """ + +from __future__ import print_function + +from __future__ import absolute_import + +from yardstick.benchmark.core.report import Report +from yardstick.cmd.commands import change_osloobj_to_paras +from yardstick.common.utils import cliargs + + +class ReportCommands(object): + """Report commands. + + Set of commands to manage benchmark tasks. + """ + + @cliargs("task_id", type=str, help=" task id", nargs=1) + @cliargs("yaml_name", type=str, help=" Yaml file Name", nargs=1) + def do_generate(self, args): + """Start a benchmark scenario.""" + param = change_osloobj_to_paras(args) + Report().generate(param) diff --git a/yardstick/common/constants.py b/yardstick/common/constants.py index 54ddf33dc..6550cc8fd 100644 --- a/yardstick/common/constants.py +++ b/yardstick/common/constants.py @@ -65,3 +65,5 @@ ASYNC_TASK_API = BASE_URL + '/yardstick/asynctask' SQLITE = 'sqlite:////tmp/yardstick.db' DEFAULT_OUTPUT_FILE = '/tmp/yardstick.out' + +DEFAULT_HTML_FILE = '/tmp/yardstick.htm' diff --git a/yardstick/common/html_template.py b/yardstick/common/html_template.py new file mode 100644 index 000000000..4b46e77a0 --- /dev/null +++ b/yardstick/common/html_template.py @@ -0,0 +1,133 @@ +############################################################################# +# Copyright (c) 2017 Rajesh Kudaka +# +# Author: Rajesh Kudaka 4k.rajesh@gmail.com +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Apache License, Version 2.0 +# which accompanies this distribution, and is available at +# http://www.apache.org/licenses/LICENSE-2.0 +############################################################################# + +template = """ + + + + + + + + + + + + + +
+

Yardstick User Interface

+

Report of {{task_id}} Generated

+
+ +
+
+
+
+
+
+
+
+
+
+
+
+ + + + + + +""" -- cgit 1.2.3-korg