4f022d5b944382c6bfc9195f1a3d89a6d31548ce
[releng.git] / utils / test / result_collection_api / opnfv_testapi / dashboard / yardstick2Dashboard.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 Yardstick project
14 # a new method format_<Test_case>_for_dashboard(results)
15 # v0.1: basic example with methods for Ping, Iperf, Netperf, Pktgen,
16 #       Fio, Lmbench, Perf, Cyclictest.
17 #
18
19
20 def get_yardstick_cases():
21     """
22     get the list of the supported test cases
23     TODO: update the list when adding a new test case for the dashboard
24     """
25     return ["Ping", "Iperf", "Netperf", "Pktgen", "Fio", "Lmbench",
26             "Perf", "Cyclictest"]
27
28
29 def format_yardstick_for_dashboard(case, results):
30     """
31     generic method calling the method corresponding to the test case
32     check that the testcase is properly declared first
33     then build the call to the specific method
34     """
35     if check_yardstick_case_exist(case):
36         cmd = "format_" + case + "_for_dashboard(results)"
37         res = eval(cmd)
38     else:
39         res = []
40         print "Test cases not declared"
41     return res
42
43
44 def check_yardstick_case_exist(case):
45     """
46     check if the testcase exists
47     if the test case is not defined or not declared in the list
48     return False
49     """
50     yardstick_cases = get_yardstick_cases()
51
52     if (case is None or case not in yardstick_cases):
53         return False
54     else:
55         return True
56
57
58 def _get_test_status_bar(results):
59     nbTest = 0
60     nbTestOk = 0
61
62     for data in results:
63         nbTest += 1
64         records = [record for record in data['details']
65                           if "benchmark" in record
66                              and record["benchmark"]["errors"] != ""]
67         if len(records) == 0:
68             nbTestOk += 1
69     return nbTest, nbTestOk
70
71
72 def format_Ping_for_dashboard(results):
73     """
74     Post processing for the Ping test case
75     """
76     test_data = [{'description': 'Ping results for Dashboard'}]
77
78     # Graph 1: Test_Duration = f(time)
79     # ********************************
80     new_element = []
81     for data in results:
82         records = [record["benchmark"]["data"]["rtt"]
83                     for record in data['details']
84                         if "benchmark" in record]
85
86         avg_rtt = sum(records) / len(records)
87         new_element.append({'x': data['start_date'],
88                             'y': avg_rtt})
89
90     test_data.append({'name': "ping duration",
91                       'info': {'type': "graph",
92                                'xlabel': 'time',
93                                'ylabel': 'duration (s)'},
94                       'data_set': new_element})
95
96     # Graph 2: bar
97     # ************
98     nbTest, nbTestOk = _get_test_status_bar(results)
99
100     test_data.append({'name': "ping status",
101                       'info': {"type": "bar"},
102                       'data_set': [{'Nb tests': nbTest,
103                                     'Nb Success': nbTestOk}]})
104
105     return test_data
106
107
108 def format_iperf_for_dashboard(results):
109     """
110     Post processing for the Iperf test case
111     """
112     test_data = [{'description': 'Iperf results for Dashboard'}]
113     return test_data
114
115
116 def format_netperf_for_dashboard(results):
117     """
118     Post processing for the Netperf test case
119     """
120     test_data = [{'description': 'Netperf results for Dashboard'}]
121     return test_data
122
123
124 def format_pktgen_for_dashboard(results):
125     """
126     Post processing for the Pktgen test case
127     """
128     test_data = [{'description': 'Pktgen results for Dashboard'}]
129     return test_data
130
131
132 def format_fio_for_dashboard(results):
133     """
134     Post processing for the Fio test case
135     """
136     test_data = [{'description': 'Fio results for Dashboard'}]
137     return test_data
138
139
140 def format_lmbench_for_dashboard(results):
141     """
142     Post processing for the Lmbench test case
143     """
144     test_data = [{'description': 'Lmbench results for Dashboard'}]
145     return test_data
146
147
148 def format_perf_for_dashboard(results):
149     """
150     Post processing for the Perf test case
151     """
152     test_data = [{'description': 'Perf results for Dashboard'}]
153     return test_data
154
155
156 def format_cyclictest_for_dashboard(results):
157     """
158     Post processing for the Cyclictest test case
159     """
160     test_data = [{'description': 'Cyclictest results for Dashboard'}]
161     return test_data
162
163
164 ############################  For local test  ################################
165 import json
166 import os
167 import requests
168
169 def _read_sample_output(filename):
170     curr_path = os.path.dirname(os.path.abspath(__file__))
171     output = os.path.join(curr_path, filename)
172     with open(output) as f:
173         sample_output = f.read()
174
175     result = json.loads(sample_output)
176     return result
177
178 # Copy form functest/testcases/Dashboard/dashboard_utils.py
179 # and did some minor modification for local test.
180 def _get_results(db_url, test_criteria):
181
182     test_project = test_criteria["project"]
183     testcase = test_criteria["testcase"]
184
185     # Build headers
186     headers = {'Content-Type': 'application/json'}
187
188     # build the request
189     # if criteria is all => remove criteria
190     url = db_url + "/results?project=" + test_project + "&case=" + testcase
191
192     # Send Request to Test DB
193     myData = requests.get(url, headers=headers)
194
195     # Get result as a json object
196     myNewData = json.loads(myData.text)
197
198     # Get results
199     myDataResults = myNewData['test_results']
200
201     return myDataResults
202
203 def _test():
204     db_url = "http://213.77.62.197"
205     result = _get_results(db_url,
206         {"project": "yardstick", "testcase": "Ping"})
207     print format_ping_for_dashboard(result)
208
209 if __name__ == '__main__':
210     _test()