Yardstick: User interface for Yardstick. 31/32431/1
authorrajesh_4k <4k.rajesh@gmail.com>
Mon, 27 Feb 2017 12:39:00 +0000 (18:09 +0530)
committerJing Lu <lvjing5@huawei.com>
Thu, 30 Mar 2017 09:46:32 +0000 (09:46 +0000)
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 <task-ID> <TC-name>" 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>
(cherry picked from commit 25b21add71fcf7c2c795bd950b5117d69fac68fb)

docs/testing/user/userguide/Yardstick_user_interface.rst [new file with mode: 0644]
requirements.txt
tests/unit/benchmark/core/test_report.py [new file with mode: 0644]
yardstick/benchmark/core/__init__.py
yardstick/benchmark/core/report.py [new file with mode: 0644]
yardstick/benchmark/core/task.py
yardstick/cmd/cli.py
yardstick/cmd/commands/report.py [new file with mode: 0644]
yardstick/common/constants.py
yardstick/common/html_template.py [new file with mode: 0644]

diff --git a/docs/testing/user/userguide/Yardstick_user_interface.rst b/docs/testing/user/userguide/Yardstick_user_interface.rst
new file mode 100644 (file)
index 0000000..9058dd4
--- /dev/null
@@ -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 <task-ID> <testcase-filename>
+
+
+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".
index 047ebf8..80547f5 100644 (file)
@@ -14,6 +14,7 @@ cliff==2.3.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 (file)
index 0000000..6954692
--- /dev/null
@@ -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)
index 79ebc73..70036ea 100644 (file)
@@ -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 (file)
index 0000000..997a125
--- /dev/null
@@ -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")
index 47315b5..4012276 100644 (file)
@@ -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):
index 1f8dfee..79f66e5 100644 (file)
@@ -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 (file)
index 0000000..87ae7d5
--- /dev/null
@@ -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)
index 54ddf33..6550cc8 100644 (file)
@@ -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 (file)
index 0000000..4b46e77
--- /dev/null
@@ -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 = """
+<html>
+<body>
+<head>
+<meta charset="utf-8">
+<meta name="viewport" content="width=device-width, initial-scale=1">
+<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7\
+/css/bootstrap.min.css">
+<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1\
+/jquery.min.js"></script>
+<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7\
+/js/bootstrap.min.js"></script>
+<script src="https://code.highcharts.com/highcharts.js"></script>
+<script src="jquery.min.js"></script>
+<script src="highcharts.js"></script>
+</head>
+<style>
+
+table{
+  overflow-y: scroll;
+  height: 360px;
+  display: block;
+  }
+
+ header,h3{
+    font-family:Frutiger;
+    clear: left;
+    text-align: center;
+}
+</style>
+<header class="jumbotron text-center">
+  <h1>Yardstick User Interface</h1>
+  <h4>Report of {{task_id}} Generated</h4>
+</header>
+
+<div class="container">
+  <div class="row">
+    <div class="col-md-4">
+        <div class="table-responsive" >
+        <table  class="table table-hover" > </table>
+        </div>
+    </div>
+    <div class="col-md-8" >
+    <div id="container" ></div>
+   </div>
+  </div>
+</div>
+<script>
+  var arr, tab, th, tr, td, tn, row, col, thead, tbody;
+  arr={{table|safe}}
+  tab = document.getElementsByTagName('table')[0];
+  thead=document.createElement('thead');
+  tr = document.createElement('tr');
+  for(row=0;row<Object.keys(arr).length;row++)
+  {
+      th = document.createElement('th');
+      tn = document.createTextNode(Object.keys(arr).sort()[row]);
+      th.appendChild(tn);
+      tr.appendChild(th);
+          thead.appendChild(tr);
+  }
+  tab.appendChild(thead);
+  tbody=document.createElement('tbody');
+
+  for (col = 0; col < arr[Object.keys(arr)[0]].length; col++){
+  tr = document.createElement('tr');
+  for(row=0;row<Object.keys(arr).length;row++)
+  {
+      td = document.createElement('td');
+      tn = document.createTextNode(arr[Object.keys(arr).sort()[row]][col]);
+      td.appendChild(tn);
+      tr.appendChild(td);
+  }
+    tbody.appendChild(tr);
+        }
+tab.appendChild(tbody);
+
+</script>
+
+<script language="JavaScript">
+
+$(function() {
+  $('#container').highcharts({
+    title: {
+      text: 'Yardstick test results',
+      x: -20 //center
+    },
+    subtitle: {
+      text: 'Report of {{task_id}} Task Generated',
+      x: -20
+    },
+    xAxis: {
+      title: {
+        text: 'Timestamp'
+      },
+      categories:{{Timestamp|safe}}
+    },
+    yAxis: {
+
+      plotLines: [{
+        value: 0,
+        width: 1,
+        color: '#808080'
+      }]
+    },
+    tooltip: {
+      valueSuffix: ''
+    },
+    legend: {
+      layout: 'vertical',
+      align: 'right',
+      verticalAlign: 'middle',
+      borderWidth: 0
+    },
+    series: {{series|safe}}
+  });
+});
+
+</script>
+
+
+</body>
+</html>"""