6f7679d6fa1e35096030ae95131f414360887d6f
[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:
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     return test_data
79
80 def format_rubbos_probe_for_dashboard(results):
81     """
82     Post processing for the Rubbos test case of one time
83     """
84     test_data = [{'description': 'Rubbos results'}]
85
86     element = []
87     latest_result = results[-1]["details"]
88     for key in sorted(lastest_result):
89         throughput = latest_result[key]["throughput"]
90         client_num = int(key)
91         element.append({'x': client_num,
92                         'y': throughput})
93     #graph
94     test_data.append({'name': "Rubbos throughput vs client number",
95                       'info': {'type': "graph",
96                       'xlabel': 'client number',
97                       'ylabel': 'throughput'},
98                       'data_set': element})
99
100     return test_data
101
102 def format_tu1_for_dashboard(results):
103     test_data = [{'description': 'Tu-1 performance result'}]
104     line_element = []
105     bar_element = {}
106     last_result = results[-1]["details"]
107     for key in sorted(last_result):
108         bandwith = last_result[key]["Bandwidth"]
109         pktsize = int(key)
110         line_element.append({'x': pktsize,
111                              'y': bandwith * 1000})
112         bar_element[key] = bandwith * 1000
113     # graph1, line
114     test_data.append({'name': "VM2VM max single directional throughput",
115                       'info': {'type': "graph",
116                                'xlabel': 'pktsize',
117                                'ylabel': 'bandwith(kpps)'},
118                       'data_set': line_element})
119     # graph2, bar
120     test_data.append({'name': "VM2VM max single directional throughput",
121                       'info': {"type": "bar"},
122                       'data_set': bar_element})
123     return test_data
124
125
126 def format_tu3_for_dashboard(results):
127     test_data = [{'description': 'Tu-3 performance result'}]
128     new_element = []
129     bar_element = {}
130     last_result = results[-1]["details"]
131     for key in sorted(last_result):
132         bandwith = last_result[key]["Bandwidth"]
133         pktsize = int(key)
134         new_element.append({'x': pktsize,
135                             'y': bandwith * 1000})
136         bar_element[key] = bandwith * 1000
137     # graph1, line
138     test_data.append({'name': "VM2VM max bidirectional throughput",
139                       'info': {'type': "graph",
140                                'xlabel': 'pktsize',
141                                'ylabel': 'bandwith(kpps)'},
142                       'data_set': new_element})
143     # graph2, bar
144     test_data.append({'name': "VM2VM max single directional throughput",
145                       'info': {"type": "bar"},
146                       'data_set': bar_element})
147     return test_data
148
149
150 ############################  For local test  ################################
151
152 def _read_sample_output(filename):
153     curr_path = os.path.dirname(os.path.abspath(__file__))
154     output = os.path.join(curr_path, filename)
155     with open(output) as f:
156         sample_output = f.read()
157
158     result = json.loads(sample_output)
159     return result
160
161
162 # Copy form functest/testcases/Dashboard/dashboard_utils.py
163 # and did some minor modification for local test.
164 def _get_results(db_url, test_criteria):
165     test_project = test_criteria["project"]
166     testcase = test_criteria["testcase"]
167
168     # Build headers
169     headers = {'Content-Type': 'application/json'}
170
171     # build the request
172     # if criteria is all => remove criteria
173     url = db_url + "/results?project=" + test_project + "&case=" + testcase
174
175     # Send Request to Test DB
176     myData = requests.get(url, headers=headers)
177
178     # Get result as a json object
179     myNewData = json.loads(myData.text)
180
181     # Get results
182     myDataResults = myNewData['test_results']
183     return myDataResults
184
185
186 def _test():
187     db_url = "http://213.77.62.197"
188     results = _get_results(db_url, {"project": "bottlenecks", "testcase": "rubbos"})
189     test_result = format_rubbos_probe_for_dashboard(results)
190     print json.dumps(test_result, indent=4)
191
192     results = _get_results(db_url, {"project": "bottlenecks", "testcase": "tu1"})
193     #results = _read_sample_output("sample")
194     #print json.dumps(results, indent=4)
195     test_result = format_tu1_for_dashboard(results)
196     print json.dumps(test_result, indent=4)
197     results = _get_results(db_url, {"project": "bottlenecks", "testcase": "tu3"})
198     test_result = format_tu3_for_dashboard(results)
199     print json.dumps(test_result, indent=4)
200
201
202 if __name__ == '__main__':
203     _test()
204