bottlenecks: fix rewrite the rubbos graph drawing function
[releng.git] / utils / test / result_collection_api / dashboard / bottlenecks2Dashboard.py
1 #!/usr/bin/python
2 #
3 ##############################################################################
4 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and other.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11 #
12 # This script is used to build dashboard ready json results
13 # It may be used for all the test case of the Bottlenecks project
14 # a new method format_<Test_case>_for_dashboard(results)
15 # v0.1: basic example with methods for Rubbos.
16 #
17 import os
18 import requests
19 import json
20
21
22 def get_bottlenecks_cases():
23     """
24     get the list of the supported test cases
25     TODO: update the list when adding a new test case for the dashboard
26     """
27     return ["rubbos", "tu1", "tu3"]
28
29
30 def check_bottlenecks_case_exist(case):
31     """
32     check if the testcase exists
33     if the test case is not defined or not declared in the list
34     return False
35     """
36     bottlenecks_cases = get_bottlenecks_cases()
37
38     if case is None or case not in bottlenecks_cases:
39         return False
40     else:
41         return True
42
43
44 def format_bottlenecks_for_dashboard(case, results):
45     """
46     generic method calling the method corresponding to the test case
47     check that the testcase is properly declared first
48     then build the call to the specific method
49     """
50     if check_bottlenecks_case_exist(case):
51         cmd = "format_" + case + "_for_dashboard(results)"
52         res = eval(cmd)
53     else:
54         res = []
55         print "Test cases not declared"
56     return res
57
58
59 def format_rubbos_for_dashboard(results):
60     """
61     Post processing for the Rubbos test case
62     """
63     test_data = [{'description': 'Rubbos results'}]
64
65     # Graph 1:Rubbos maximal throughput
66     # ********************************
67     #new_element = []
68     #for each_result in results:
69     #    throughput_data = [record['throughput'] for record in each_result['details']]
70     #    new_element.append({'x': each_result['creation_date'],
71     #                        'y': max(throughput_data)})
72
73     #test_data.append({'name': "Rubbos max throughput",
74     #                  'info': {'type': "graph",
75     #                           'xlabel': 'time',
76     #                           'ylabel': 'maximal throughput'},
77     #                  'data_set': new_element})
78
79     # Graph 2: Rubbos last record
80     # ********************************
81     new_element = []
82     latest_result = results[-1]["details"]
83     for data in latest_result:
84         client_num = int(data["client"])
85         throughput = int(data["throughput"])
86         new_element.append({'x': client_num,
87                             'y': throughput})
88     test_data.append({'name': "Rubbos throughput vs client number",
89                       'info': {'type': "graph",
90                       'xlabel': 'client number',
91                       'ylabel': 'throughput'},
92                       'data_set': new_element})
93
94     return test_data
95
96
97 def format_tu1_for_dashboard(results):
98     test_data = [{'description': 'Tu-1 performance result'}]
99     line_element = []
100     bar_element = {}
101     last_result = results[-1]["details"]
102     for key in sorted(last_result):
103         bandwith = last_result[key]["Bandwidth"]
104         pktsize = int(key)
105         line_element.append({'x': pktsize,
106                              'y': bandwith * 1000})
107         bar_element[key] = bandwith * 1000
108     # graph1, line
109     test_data.append({'name': "VM2VM max single directional throughput",
110                       'info': {'type': "graph",
111                                'xlabel': 'pktsize',
112                                'ylabel': 'bandwith(kpps)'},
113                       'data_set': line_element})
114     # graph2, bar
115     test_data.append({'name': "VM2VM max single directional throughput",
116                       'info': {"type": "bar"},
117                       'data_set': bar_element})
118     return test_data
119
120
121 def format_tu3_for_dashboard(results):
122     test_data = [{'description': 'Tu-3 performance result'}]
123     new_element = []
124     bar_element = {}
125     last_result = results[-1]["details"]
126     for key in sorted(last_result):
127         bandwith = last_result[key]["Bandwidth"]
128         pktsize = int(key)
129         new_element.append({'x': pktsize,
130                             'y': bandwith * 1000})
131         bar_element[key] = bandwith * 1000
132     # graph1, line
133     test_data.append({'name': "VM2VM max bidirectional throughput",
134                       'info': {'type': "graph",
135                                'xlabel': 'pktsize',
136                                'ylabel': 'bandwith(kpps)'},
137                       'data_set': new_element})
138     # graph2, bar
139     test_data.append({'name': "VM2VM max single directional throughput",
140                       'info': {"type": "bar"},
141                       'data_set': bar_element})
142     return test_data
143
144
145 ############################  For local test  ################################
146
147 def _read_sample_output(filename):
148     curr_path = os.path.dirname(os.path.abspath(__file__))
149     output = os.path.join(curr_path, filename)
150     with open(output) as f:
151         sample_output = f.read()
152
153     result = json.loads(sample_output)
154     return result
155
156
157 # Copy form functest/testcases/Dashboard/dashboard_utils.py
158 # and did some minor modification for local test.
159 def _get_results(db_url, test_criteria):
160     test_project = test_criteria["project"]
161     testcase = test_criteria["testcase"]
162
163     # Build headers
164     headers = {'Content-Type': 'application/json'}
165
166     # build the request
167     # if criteria is all => remove criteria
168     url = db_url + "/results?project=" + test_project + "&case=" + testcase
169
170     # Send Request to Test DB
171     myData = requests.get(url, headers=headers)
172
173     # Get result as a json object
174     myNewData = json.loads(myData.text)
175
176     # Get results
177     myDataResults = myNewData['test_results']
178     return myDataResults
179
180 #only for local test
181 def _test():
182     db_url = "http://testresults.opnfv.org/testapi"
183     results = _get_results(db_url, {"project": "bottlenecks", "testcase": "rubbos"})
184     test_result = format_rubbos_for_dashboard(results)
185     print json.dumps(test_result, indent=4)
186
187     results = _get_results(db_url, {"project": "bottlenecks", "testcase": "tu1"})
188     #results = _read_sample_output("sample")
189     #print json.dumps(results, indent=4)
190     test_result = format_tu1_for_dashboard(results)
191     print json.dumps(test_result, indent=4)
192     results = _get_results(db_url, {"project": "bottlenecks", "testcase": "tu3"})
193     test_result = format_tu3_for_dashboard(results)
194     print json.dumps(test_result, indent=4)
195
196
197 if __name__ == '__main__':
198     _test()
199