832d3b3e10db9fd9318d858d5c26d36262b4b89b
[yardstick.git] / yardstick / tests / functional / benchmark / core / test_report.py
1 ##############################################################################
2 # Copyright (c) 2018-2019 Intel Corporation.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import ast
11 import tempfile
12 import unittest
13
14 import mock
15 from six.moves import configparser
16
17 from yardstick.benchmark import core
18 from yardstick.benchmark.core import report
19 from yardstick.cmd.commands import change_osloobj_to_paras
20
21
22 GOOD_YAML_NAME = 'fake_name'
23 GOOD_TASK_ID = "9cbe74b6-df09-4535-8bdc-dc3a43b8a4e2"
24 GOOD_DB_FIELDKEYS = [
25     {u'fieldKey': u'metric1', u'fieldType': u'integer'},
26     {u'fieldKey': u'metric4', u'fieldType': u'integer'},
27     {u'fieldKey': u'metric2', u'fieldType': u'integer'},
28     {u'fieldKey': u'metric3', u'fieldType': u'integer'},
29 ]
30 GOOD_DB_METRICS = [
31     {u'time': u'2018-08-20T16:49:26.372662016Z',
32      u'metric1': 1, u'metric2': 0, u'metric3': 8, u'metric4': 5},
33     {u'time': u'2018-08-20T16:49:27.374208000Z',
34      u'metric1': 1, u'metric2': 1, u'metric3': 5, u'metric4': 4},
35     {u'time': u'2018-08-20T16:49:28.375742976Z',
36      u'metric1': 2, u'metric2': 2, u'metric3': 3, u'metric4': 3},
37     {u'time': u'2018-08-20T16:49:29.377299968Z',
38      u'metric1': 3, u'metric2': 3, u'metric3': 2, u'metric4': 2},
39     {u'time': u'2018-08-20T16:49:30.378252032Z',
40      u'metric1': 5, u'metric2': 4, u'metric3': 1, u'metric4': 1},
41     {u'time': u'2018-08-20T16:49:30.379359421Z',
42      u'metric1': 8, u'metric2': 5, u'metric3': 1, u'metric4': 0},
43 ]
44 GOOD_DB_BARO_METRICS = [
45      {u'value': 324050, u'instance': u'0', u'host': u'myhostname',
46       u'time': u'2018-08-20T16:49:27.383698038Z',
47       u'type_instance': u'user', u'type': u'cpu'},
48      {
49       u'value': 193798, u'instance': u'0', u'host': u'myhostname',
50       u'time': u'2018-12-19T16:49:27.383712594Z',
51       u'type_instance': u'system', u'type': u'cpu'},
52      {
53       u'value': 324051, u'instance': u'0', u'host': u'myhostname',
54       u'time': u'2018-08-20T16:49:28.383696624Z',
55       u'type_instance': u'user', u'type': u'cpu'},
56      {
57       u'value': 193800, u'instance': u'0', u'host': u'myhostname',
58       u'time': u'2018-08-20T16:49:28.383713481Z',
59       u'type_instance': u'system', u'type': u'cpu'},
60      {
61       u'value': 324054, u'instance': u'0', u'host': u'myhostname',
62       u'time': u'2018-08-20T16:49:29.3836966789Z',
63       u'type_instance': u'user', u'type': u'cpu'},
64      {
65       u'value': 193801, u'instance': u'0', u'host': u'myhostname',
66       u'time': u'2018-08-20T16:49:29.383716296Z',
67       u'type_instance': u'system', u'type': u'cpu'}
68 ]
69 TIMESTAMP_START = '2018-08-20T16:49:26.372662016Z'
70 TIMESTAMP_END = '2018-08-20T16:49:30.379359421Z'
71
72 yardstick_config = """
73 [DEFAULT]
74 dispatcher = influxdb
75 """
76
77
78 def my_query(query_sql, db=None):
79     get_fieldkeys_cmd = 'show field keys'
80     get_metrics_cmd = 'select * from'
81     get_start_time_cmd = 'ORDER ASC limit 1'
82     get_end_time_cmd = 'ORDER DESC limit 1'
83     if db:
84         if get_start_time_cmd in query_sql:
85             return TIMESTAMP_START
86         elif get_end_time_cmd in query_sql:
87             return TIMESTAMP_END
88         else:
89             return GOOD_DB_BARO_METRICS
90     elif get_fieldkeys_cmd in query_sql:
91         return GOOD_DB_FIELDKEYS
92     elif get_metrics_cmd in query_sql:
93         return GOOD_DB_METRICS
94     return []
95
96
97 class ReportTestCase(unittest.TestCase):
98
99     @mock.patch.object(report.influx, 'query', new=my_query)
100     @mock.patch.object(configparser.ConfigParser,
101         'read', side_effect=mock.mock_open(read_data=yardstick_config))
102     def test_report_generate_nsb_simple(self, *args):
103         tmpfile = tempfile.NamedTemporaryFile(delete=True)
104
105         args = core.Param({"task_id": [GOOD_TASK_ID], "yaml_name": [GOOD_YAML_NAME]})
106         params = change_osloobj_to_paras(args)
107
108         with mock.patch.object(report.consts, 'DEFAULT_HTML_FILE', tmpfile.name):
109             report.Report().generate_nsb(params)
110
111         data_act = None
112         time_act = None
113         keys_act = None
114         tree_act = None
115         with open(tmpfile.name) as f:
116             for l in f.readlines():
117                  if "var report_data = {" in l:
118                      data_act = ast.literal_eval(l.strip()[18:-1])
119                  elif "var report_time = [" in l:
120                      time_act = ast.literal_eval(l.strip()[18:-1])
121                  elif "var report_keys = [" in l:
122                      keys_act = ast.literal_eval(l.strip()[18:-1])
123                  elif "var report_tree = [" in l:
124                      tree_act = ast.literal_eval(l.strip()[18:-1])
125         data_exp = {
126             'metric1': [
127                 {'x': '16:49:26.372662', 'y': 1},
128                 {'x': '16:49:27.374208', 'y': 1},
129                 {'x': '16:49:28.375742', 'y': 2},
130                 {'x': '16:49:29.377299', 'y': 3},
131                 {'x': '16:49:30.378252', 'y': 5},
132                 {'x': '16:49:30.379359', 'y': 8}],
133             'metric2': [
134                 {'x': '16:49:26.372662', 'y': 0},
135                 {'x': '16:49:27.374208', 'y': 1},
136                 {'x': '16:49:28.375742', 'y': 2},
137                 {'x': '16:49:29.377299', 'y': 3},
138                 {'x': '16:49:30.378252', 'y': 4},
139                 {'x': '16:49:30.379359', 'y': 5}],
140             'metric3': [
141                 {'x': '16:49:26.372662', 'y': 8},
142                 {'x': '16:49:27.374208', 'y': 5},
143                 {'x': '16:49:28.375742', 'y': 3},
144                 {'x': '16:49:29.377299', 'y': 2},
145                 {'x': '16:49:30.378252', 'y': 1},
146                 {'x': '16:49:30.379359', 'y': 1}],
147             'metric4': [
148                 {'x': '16:49:26.372662', 'y': 5},
149                 {'x': '16:49:27.374208', 'y': 4},
150                 {'x': '16:49:28.375742', 'y': 3},
151                 {'x': '16:49:29.377299', 'y': 2},
152                 {'x': '16:49:30.378252', 'y': 1},
153                 {'x': '16:49:30.379359', 'y': 0}],
154             'myhostname.cpu_value.cpu.system.0': [
155                 {'x': '16:49:27.3837', 'y': 193798},
156                 {'x': '16:49:28.3837', 'y': 193800},
157                 {'x': '16:49:29.3837', 'y': 193801}],
158             'myhostname.cpu_value.cpu.user.0': [
159                 {'x': '16:49:27.3836', 'y': 324050},
160                 {'x': '16:49:28.3836', 'y': 324051},
161                 {'x': '16:49:29.3836', 'y': 324054}],
162             'myhostname.cpufreq_value.cpu.system.0': [
163                 {'x': '16:49:27.3837', 'y': 193798},
164                 {'x': '16:49:28.3837', 'y': 193800},
165                 {'x': '16:49:29.3837', 'y': 193801}],
166             'myhostname.cpufreq_value.cpu.user.0': [
167                 {'x': '16:49:27.3836', 'y': 324050},
168                 {'x': '16:49:28.3836', 'y': 324051},
169                 {'x': '16:49:29.3836', 'y': 324054}],
170             'myhostname.intel_pmu_value.cpu.system.0': [
171                 {'x': '16:49:27.3837', 'y': 193798},
172                 {'x': '16:49:28.3837', 'y': 193800},
173                 {'x': '16:49:29.3837', 'y': 193801}],
174             'myhostname.intel_pmu_value.cpu.user.0': [
175                 {'x': '16:49:27.3836', 'y': 324050},
176                 {'x': '16:49:28.3836', 'y': 324051},
177                 {'x': '16:49:29.3836', 'y': 324054}],
178             'myhostname.virt_value.cpu.system.0': [
179                 {'x': '16:49:27.3837', 'y': 193798},
180                 {'x': '16:49:28.3837', 'y': 193800},
181                 {'x': '16:49:29.3837', 'y': 193801}],
182             'myhostname.virt_value.cpu.user.0': [
183                 {'x': '16:49:27.3836', 'y': 324050},
184                 {'x': '16:49:28.3836', 'y': 324051},
185                 {'x': '16:49:29.3836', 'y': 324054}],
186             'myhostname.memory_value.cpu.system.0': [
187                 {'x': '16:49:27.3837', 'y': 193798},
188                 {'x': '16:49:28.3837', 'y': 193800},
189                 {'x': '16:49:29.3837', 'y': 193801}],
190             'myhostname.memory_value.cpu.user.0': [
191                 {'x': '16:49:27.3836', 'y': 324050},
192                 {'x': '16:49:28.3836', 'y': 324051},
193                 {'x': '16:49:29.3836', 'y': 324054}]
194         }
195         time_exp = [
196             '16:49:26.372662', '16:49:27.374208', '16:49:27.3836',
197             '16:49:27.3837', '16:49:28.375742', '16:49:28.3836',
198             '16:49:28.3837', '16:49:29.377299', '16:49:29.3836',
199             '16:49:29.3837', '16:49:30.378252', '16:49:30.379359',
200         ]
201         keys_exp = sorted([
202             'metric1', 'metric2', 'metric3', 'metric4',
203             'myhostname.cpu_value.cpu.system.0',
204             'myhostname.cpu_value.cpu.user.0',
205             'myhostname.cpufreq_value.cpu.system.0',
206             'myhostname.cpufreq_value.cpu.user.0',
207             'myhostname.intel_pmu_value.cpu.system.0',
208             'myhostname.intel_pmu_value.cpu.user.0',
209             'myhostname.virt_value.cpu.system.0',
210             'myhostname.virt_value.cpu.user.0',
211             'myhostname.memory_value.cpu.system.0',
212             'myhostname.memory_value.cpu.user.0',
213         ])
214         tree_exp = [
215             {'parent': '#', 'text': 'metric1', 'id': 'metric1'},
216             {'parent': '#', 'text': 'metric2', 'id': 'metric2'},
217             {'parent': '#', 'text': 'metric3', 'id': 'metric3'},
218             {'parent': '#', 'text': 'metric4', 'id': 'metric4'},
219             {'id': 'myhostname', 'parent': '#', 'text': 'myhostname'},
220             {'id': 'myhostname.cpu_value',
221              'parent': 'myhostname',
222              'text': 'cpu_value'},
223             {'id': 'myhostname.cpu_value.cpu',
224              'parent': 'myhostname.cpu_value',
225              'text': 'cpu'},
226             {'id': 'myhostname.cpu_value.cpu.system',
227              'parent': 'myhostname.cpu_value.cpu',
228              'text': 'system'},
229             {'id': 'myhostname.cpu_value.cpu.system.0',
230              'parent': 'myhostname.cpu_value.cpu.system',
231              'text': '0'},
232             {'id': 'myhostname.cpu_value.cpu.user',
233              'parent': 'myhostname.cpu_value.cpu',
234              'text': 'user'},
235             {'id': 'myhostname.cpu_value.cpu.user.0',
236              'parent': 'myhostname.cpu_value.cpu.user',
237              'text': '0'},
238             {'id': 'myhostname.cpufreq_value',
239              'parent': 'myhostname',
240              'text': 'cpufreq_value'},
241             {'id': 'myhostname.cpufreq_value.cpu',
242              'parent': 'myhostname.cpufreq_value',
243              'text': 'cpu'},
244             {'id': 'myhostname.cpufreq_value.cpu.system',
245              'parent': 'myhostname.cpufreq_value.cpu',
246              'text': 'system'},
247             {'id': 'myhostname.cpufreq_value.cpu.system.0',
248              'parent': 'myhostname.cpufreq_value.cpu.system',
249              'text': '0'},
250             {'id': 'myhostname.cpufreq_value.cpu.user',
251              'parent': 'myhostname.cpufreq_value.cpu',
252              'text': 'user'},
253             {'id': 'myhostname.cpufreq_value.cpu.user.0',
254              'parent': 'myhostname.cpufreq_value.cpu.user',
255              'text': '0'},
256             {'id': 'myhostname.intel_pmu_value',
257              'parent': 'myhostname',
258              'text': 'intel_pmu_value'},
259             {'id': 'myhostname.intel_pmu_value.cpu',
260              'parent': 'myhostname.intel_pmu_value',
261              'text': 'cpu'},
262             {'id': 'myhostname.intel_pmu_value.cpu.system',
263              'parent': 'myhostname.intel_pmu_value.cpu',
264              'text': 'system'},
265             {'id': 'myhostname.intel_pmu_value.cpu.system.0',
266              'parent': 'myhostname.intel_pmu_value.cpu.system',
267              'text': '0'},
268             {'id': 'myhostname.intel_pmu_value.cpu.user',
269              'parent': 'myhostname.intel_pmu_value.cpu',
270              'text': 'user'},
271             {'id': 'myhostname.intel_pmu_value.cpu.user.0',
272              'parent': 'myhostname.intel_pmu_value.cpu.user',
273              'text': '0'},
274             {'id': 'myhostname.memory_value',
275              'parent': 'myhostname',
276              'text': 'memory_value'},
277             {'id': 'myhostname.memory_value.cpu',
278              'parent': 'myhostname.memory_value',
279              'text': 'cpu'},
280             {'id': 'myhostname.memory_value.cpu.system',
281              'parent': 'myhostname.memory_value.cpu',
282              'text': 'system'},
283             {'id': 'myhostname.memory_value.cpu.system.0',
284              'parent': 'myhostname.memory_value.cpu.system',
285              'text': '0'},
286             {'id': 'myhostname.memory_value.cpu.user',
287              'parent': 'myhostname.memory_value.cpu',
288              'text': 'user'},
289             {'id': 'myhostname.memory_value.cpu.user.0',
290              'parent': 'myhostname.memory_value.cpu.user',
291              'text': '0'},
292             {'id': 'myhostname.virt_value', 'parent': 'myhostname',
293              'text': 'virt_value'},
294             {'id': 'myhostname.virt_value.cpu',
295              'parent': 'myhostname.virt_value',
296              'text': 'cpu'},
297             {'id': 'myhostname.virt_value.cpu.system',
298              'parent': 'myhostname.virt_value.cpu',
299              'text': 'system'},
300             {'id': 'myhostname.virt_value.cpu.system.0',
301              'parent': 'myhostname.virt_value.cpu.system',
302              'text': '0'},
303             {'id': 'myhostname.virt_value.cpu.user',
304              'parent': 'myhostname.virt_value.cpu',
305              'text': 'user'},
306             {'id': 'myhostname.virt_value.cpu.user.0',
307              'parent': 'myhostname.virt_value.cpu.user',
308              'text': '0'}
309         ]
310
311         self.assertEqual(data_exp, data_act)
312         self.assertEqual(time_exp, time_act)
313         self.assertEqual(keys_exp, keys_act)
314         self.assertEqual(tree_exp, tree_act)