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