1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
10 from vstf.controller.database.dbinterface import DbManage
11 import vstf.common.constants as cst
14 class DataProvider(object):
15 def __init__(self, taskid, dbase):
20 class CommonData(DataProvider):
21 def get_taskname(self):
22 return self._dbase.query_taskname(self._taskid)
24 def get_systeminfo(self):
26 ['Host', 'Server', 'CPU', 'MEM', 'NIC', 'OS'],
28 query = self._dbase.query_task_host_list(self._taskid)
29 query = map(lambda x: list(x), query)
31 # cols = len(zip(*query))
32 # for i in range(rows):
33 # for j in range(cols):
34 # query[i][j] = query[i][j].replace('\",','\"\n')
36 systable = map(lambda x: list(x), zip(*systable))
39 def get_introduct_tabledata(self):
41 ["Type", "Case", "Name", "Direction", "Configure"]
43 query = self._dbase.query_caseinfo()
44 result += map(lambda x: list(x), query)
47 def get_scenariolist(self):
48 query = self._dbase.query_scenariolist(self._taskid)
49 result = map(lambda x: list(x), zip(*query))
55 def is_scenario_start(self):
56 scenarioList = self.get_scenariolist()
57 print "scenarioList: ", scenarioList
62 def get_casename(self, case):
63 return self._dbase.query_casename(case)
65 def get_casefigure(self, case, tools):
66 return self._dbase.query_casefigure(case, tools)
69 class ScenarioData(DataProvider):
70 def __init__(self, taskid, dbase, scenario):
71 print "ScenarioData in"
72 DataProvider.__init__(self, taskid, dbase)
73 self._scenario = scenario
75 def get_test_tools(self, case):
76 query = self._dbase.query_casetools(self._taskid, case)
77 result = map(lambda x: list(x), query)
83 def get_caselist(self):
84 query = self._dbase.query_caselist(self._taskid, self._scenario)
85 result = map(lambda x: list(x), zip(*query))
91 def get_testlist(self):
92 query = self._dbase.query_testlist(self._taskid, self._scenario)
95 result.append(item.__dict__)
98 def is_provider_start(self, case, provider):
99 count = self._dbase.query_case_provider_count(self._taskid, case, provider)
104 def is_type_provider_start(self, case, provider, ptype):
105 count = self._dbase.query_case_type_provider_count(self._taskid, case, provider, ptype)
110 def is_type_start(self, case, ptype):
111 count = self._dbase.query_case_type_count(self._taskid, case, ptype)
116 def is_throughput_start(self, case):
117 test_type = "throughput"
118 return self.is_type_start(case, test_type)
120 def is_frameloss_start(self, case):
121 test_type = "frameloss"
122 return self.is_type_start(case, test_type)
124 def is_latency_start(self, case):
125 test_type = "latency"
126 return self.is_type_start(case, test_type)
128 def get_summary_throughput_data(self, case, provider):
129 test_type = "throughput"
130 return self.get_summary_tabledata(case, provider, test_type)
132 def get_summary_frameLoss_data(self, case, provider):
133 test_type = "frameloss"
134 return self.get_summary_tabledata(case, provider, test_type)
136 def get_summary_tabledata(self, case, provider, test_type, table_type='pdf'):
143 tools = self.get_test_tools(case)
144 if "spirent" in tools:
145 table_body = self._dbase.query_summary_table(self._taskid, case, provider, test_type)
146 if 'pdf' == table_type:
148 ["FrameSize (byte)", test_type, "", "", "", "Latency(uSec)", "", ""],
149 ["", " Mpps ", " " + type_title[test_type] + " (%) ", "CPU Used (%)", " Mpps/Ghz ",
150 " Min ", " Max ", " Avg "]
154 ["FrameSize (byte)", " Mpps ", " " + type_title[test_type] + " (%) ", "CPU Used (%)",
155 " Mpps/Ghz ", "MinLatency(uSec)", "MaxLatency(uSec)", "AvgLatency(uSec)"],
158 table_body = self._dbase.query_summary_simpletable(self._taskid, case, provider, test_type)
159 if 'pdf' == table_type:
161 ["FrameSize (byte)", test_type, "", "", "", "Latency(uSec)"],
162 ["", " Mpps ", " " + type_title[test_type] + " (%)", "CPU Used (%)", " Mpps/Ghz ",
167 ["FrameSize (byte)", " Mpps ", " " + type_title[test_type] + " (%) ", "CPU Used (%)",
168 " Mpps/Ghz ", "AvgLatency(uSec)"],
170 return table_head + table_body
172 def get_ratedata(self, testid, test_type):
174 ["FrameSize (bytes)", "Bandwidth(Mpps)", "Load (%)", "CPU Usage(%)", "Mpps/Ghz", "AvgLatency(uSec)"],
176 query = self._dbase.query_testdata(testid, test_type)
179 table_body.append([item.AvgFrameSize, item.Bandwidth, item.OfferedLoad, item.CPU, item.MppspGhz,
180 item.AverageLatency])
183 result = table_head + table_body
186 def get_tabledata(self, case, test_type, item):
188 "FrameSize": "FrameSize (byte)",
189 "fastlink": "fastlink",
190 "l2switch": "l2switch",
201 line_speed = 20.0 if case in ["Tn-2v", "Tn-2"] else 10.0
203 for provider in cst.PROVIDERS:
204 if self.is_provider_start(case, provider):
205 if item == 'Percent':
206 query = self._dbase.query_load(self._taskid, case, provider, test_type)
208 query = self._dbase.query_bandwidth(self._taskid, case, provider, test_type)
210 query = self._dbase.query_avglatency(self._taskid, case, provider, test_type)
211 query = map(lambda x: list(x), zip(*query))
213 table_head = [[type_dict["FrameSize"]] + map(lambda x: " %4d " % (x), query[0])]
215 data = map(lambda x: item_dict[item] + "%.1f" % x + item_dict[item], query[1])
217 data = map(lambda x: item_dict[item] + "%.2f" % x + item_dict[item], query[1])
219 line_table = map(lambda x: "%.2f" % (line_speed * 1000 / (8 * (x + 20))), query[0])
220 table.append([type_dict[provider]] + data)
223 table.append([type_dict["line"]] + line_table)
224 table = table_head + table
227 def get_frameloss_tabledata(self, case, test_type):
229 table = self.get_tabledata(case, test_type, item)
232 def get_frameloss_chartdata(self, case, test_type):
233 result = self.get_frameloss_tabledata(case, test_type)
234 result = map(list, zip(*result))
237 def get_framerate_tabledata(self, case, test_type):
239 table = self.get_tabledata(case, test_type, item)
242 def get_framerate_chartdata(self, case, test_type):
243 result = self.get_framerate_tabledata(case, test_type)
244 result = map(list, zip(*result))
247 def get_latency_tabledata(self, case):
248 test_type = "latency"
250 table = self.get_tabledata(case, test_type, item)
253 def get_latency_chartdata(self, case):
254 result = self.get_latency_tabledata(case)
255 result = map(list, zip(*result))
258 def get_latency_bardata(self, case):
259 table_data = self.get_latency_tabledata(case)
262 ytitle = "Average Latency (uSec)"
263 category_names = map(lambda x: "FS:%4d" % int(float(x)) + "LOAD:50", table_data[0][1:])
264 bar_ = map(lambda x: x[0], table_data[1:])
265 data = map(lambda x: x[1:], table_data[1:])
266 result = [ytitle, category_names, bar_, data]
269 def get_bardata(self, case, provider, test_type):
270 if test_type == "latency":
271 query = self._dbase.query_avglatency(self._taskid, case, provider, test_type)
274 query = self._dbase.query_load(self._taskid, case, provider, test_type)
278 "Avg": "Latency (uSec)",
279 "Percent": test_type + " (%)"
283 "Percent": " OF:100 "
289 ytitle = title_dict[item]
290 query = map(lambda x: list(x), zip(*query))
293 category_names = map(lambda x: "FS:%4d" % x + name_dict[item], query[0])
295 bar_ = [color_dict[item]]
296 result = [ytitle, category_names, bar_, data]
300 class TaskData(object):
301 def __init__(self, taskid, dbase):
302 self.__common = CommonData(taskid, dbase)
303 scenario_list = self.__common.get_scenariolist()
305 for scenario in scenario_list:
306 scenario_dic[scenario] = ScenarioData(taskid, dbase, scenario)
307 self.__dict__.update(scenario_dic)
314 class HistoryData(DataProvider):
315 def get_data(self, task_list, case, provider, ttype, item):
317 @provider in ["fastlink", "rdp", "l2switch", ""]
318 @ttype in ["throughput", "frameloss", "latency"]
319 @item in ["avg", "ratep", "load"]
325 for taskid in task_list:
327 query = self._dbase.query_bandwidth(taskid, case, provider, ttype)
329 query = self._dbase.query_avglatency(taskid, case, provider, ttype)
333 for size, value in query:
335 sizes.extend(data.keys())
336 sizes = {}.fromkeys(sizes).keys()
338 datas.append({taskid: data})
343 taskid = data.keys()[0]
344 data_th = self._dbase.query_taskdate(taskid)
345 testdata = data[taskid]
348 item.append(str(testdata.get(size, '')))
352 head_th = "FrameSize (byte)"
353 table_head = [[head_th] + map(lambda x: " %4d " % (x), sizes)]
354 table = table_head + result
358 def get_tasklist(self, count=5):
360 query = self._dbase.query_tasklist()
363 if item.TaskID <= self._taskid:
364 task_list.append(item.TaskID)
366 task_list = task_list[-count:]
369 def get_history_info(self, case):
370 provider_dict = {"fastlink": "Fast Link ", "l2switch": "L2Switch ", "rdp": "Kernel RDP "}
372 "throughput": "Throughput Testing ",
373 "frameloss": "Frame Loss Testing ",
374 "latency": "Latency Testing "
378 "ratep": "RX Frame Rate(Mpps) ",
379 "avg": "Average Latency (uSec) "
382 task_list = self.get_tasklist()
385 for ttype in cst.TTYPES:
387 if ttype == "latency":
392 for provider in cst.PROVIDERS:
393 table_data = self.get_data(task_list, case, provider, ttype, item)
396 "title": provider_dict[provider] + items_dict[item],
399 content["title"] = ttype_dict[ttype]
400 content.setdefault("data", [])
401 content["data"].append(data)
403 result.append(content)
410 taskid = dbase.get_last_taskid()
411 hdata = HistoryData(taskid, dbase)
412 task_list = hdata.get_tasklist()
414 cdata = CommonData(taskid, dbase)
415 scenario_list = cdata.get_scenariolist()
419 sdata = ScenarioData(taskid, dbase, scenario)
421 case_list = sdata.get_caselist()
426 ttypes = ["throughput", "frameloss"]
427 items = ["ratep", "load"]
429 for provider in cst.PROVIDERS:
435 print hdata.get_data(task_list, case, provider, ttype, item)
437 hdata.get_history_info(case)
440 if __name__ == '__main__':