CSIT-1488: Add data to the Report 1904
[csit.git] / resources / tools / presentation / generator_tables.py
1 # Copyright (c) 2019 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Algorithms to generate tables.
15 """
16
17
18 import logging
19 import csv
20 import re
21
22 from string import replace
23 from collections import OrderedDict
24 from numpy import nan, isnan
25 from xml.etree import ElementTree as ET
26 from datetime import datetime as dt
27 from datetime import timedelta
28
29 from utils import mean, stdev, relative_change, classify_anomalies, \
30     convert_csv_to_pretty_txt
31
32
33 REGEX_NIC = re.compile(r'\d*ge\dp\d\D*\d*')
34
35
36 def generate_tables(spec, data):
37     """Generate all tables specified in the specification file.
38
39     :param spec: Specification read from the specification file.
40     :param data: Data to process.
41     :type spec: Specification
42     :type data: InputData
43     """
44
45     logging.info("Generating the tables ...")
46     for table in spec.tables:
47         try:
48             eval(table["algorithm"])(table, data)
49         except NameError as err:
50             logging.error("Probably algorithm '{alg}' is not defined: {err}".
51                           format(alg=table["algorithm"], err=repr(err)))
52     logging.info("Done.")
53
54
55 def table_details(table, input_data):
56     """Generate the table(s) with algorithm: table_detailed_test_results
57     specified in the specification file.
58
59     :param table: Table to generate.
60     :param input_data: Data to process.
61     :type table: pandas.Series
62     :type input_data: InputData
63     """
64
65     logging.info("  Generating the table {0} ...".
66                  format(table.get("title", "")))
67
68     # Transform the data
69     logging.info("    Creating the data set for the {0} '{1}'.".
70                  format(table.get("type", ""), table.get("title", "")))
71     data = input_data.filter_data(table)
72
73     # Prepare the header of the tables
74     header = list()
75     for column in table["columns"]:
76         header.append('"{0}"'.format(str(column["title"]).replace('"', '""')))
77
78     # Generate the data for the table according to the model in the table
79     # specification
80     job = table["data"].keys()[0]
81     build = str(table["data"][job][0])
82     try:
83         suites = input_data.suites(job, build)
84     except KeyError:
85         logging.error("    No data available. The table will not be generated.")
86         return
87
88     for suite_longname, suite in suites.iteritems():
89         # Generate data
90         suite_name = suite["name"]
91         table_lst = list()
92         for test in data[job][build].keys():
93             if data[job][build][test]["parent"] in suite_name:
94                 row_lst = list()
95                 for column in table["columns"]:
96                     try:
97                         col_data = str(data[job][build][test][column["data"].
98                                        split(" ")[1]]).replace('"', '""')
99                         if column["data"].split(" ")[1] in ("conf-history",
100                                                             "show-run"):
101                             col_data = replace(col_data, " |br| ", "",
102                                                maxreplace=1)
103                             col_data = " |prein| {0} |preout| ".\
104                                 format(col_data[:-5])
105                         row_lst.append('"{0}"'.format(col_data))
106                     except KeyError:
107                         row_lst.append("No data")
108                 table_lst.append(row_lst)
109
110         # Write the data to file
111         if table_lst:
112             file_name = "{0}_{1}{2}".format(table["output-file"], suite_name,
113                                             table["output-file-ext"])
114             logging.info("      Writing file: '{}'".format(file_name))
115             with open(file_name, "w") as file_handler:
116                 file_handler.write(",".join(header) + "\n")
117                 for item in table_lst:
118                     file_handler.write(",".join(item) + "\n")
119
120     logging.info("  Done.")
121
122
123 def table_merged_details(table, input_data):
124     """Generate the table(s) with algorithm: table_merged_details
125     specified in the specification file.
126
127     :param table: Table to generate.
128     :param input_data: Data to process.
129     :type table: pandas.Series
130     :type input_data: InputData
131     """
132
133     logging.info("  Generating the table {0} ...".
134                  format(table.get("title", "")))
135
136     # Transform the data
137     logging.info("    Creating the data set for the {0} '{1}'.".
138                  format(table.get("type", ""), table.get("title", "")))
139     data = input_data.filter_data(table)
140     data = input_data.merge_data(data)
141     data.sort_index(inplace=True)
142
143     logging.info("    Creating the data set for the {0} '{1}'.".
144                  format(table.get("type", ""), table.get("title", "")))
145     suites = input_data.filter_data(table, data_set="suites")
146     suites = input_data.merge_data(suites)
147
148     # Prepare the header of the tables
149     header = list()
150     for column in table["columns"]:
151         header.append('"{0}"'.format(str(column["title"]).replace('"', '""')))
152
153     for _, suite in suites.iteritems():
154         # Generate data
155         suite_name = suite["name"]
156         table_lst = list()
157         for test in data.keys():
158             if data[test]["parent"] in suite_name:
159                 row_lst = list()
160                 for column in table["columns"]:
161                     try:
162                         col_data = str(data[test][column["data"].
163                                        split(" ")[1]]).replace('"', '""')
164                         col_data = replace(col_data, "No Data",
165                                            "Not Captured     ")
166                         if column["data"].split(" ")[1] in ("conf-history",
167                                                             "show-run"):
168                             col_data = replace(col_data, " |br| ", "",
169                                                maxreplace=1)
170                             col_data = " |prein| {0} |preout| ".\
171                                 format(col_data[:-5])
172                         row_lst.append('"{0}"'.format(col_data))
173                     except KeyError:
174                         row_lst.append('"Not captured"')
175                 table_lst.append(row_lst)
176
177         # Write the data to file
178         if table_lst:
179             file_name = "{0}_{1}{2}".format(table["output-file"], suite_name,
180                                             table["output-file-ext"])
181             logging.info("      Writing file: '{}'".format(file_name))
182             with open(file_name, "w") as file_handler:
183                 file_handler.write(",".join(header) + "\n")
184                 for item in table_lst:
185                     file_handler.write(",".join(item) + "\n")
186
187     logging.info("  Done.")
188
189
190 def table_performance_comparison(table, input_data):
191     """Generate the table(s) with algorithm: table_performance_comparison
192     specified in the specification file.
193
194     :param table: Table to generate.
195     :param input_data: Data to process.
196     :type table: pandas.Series
197     :type input_data: InputData
198     """
199
200     logging.info("  Generating the table {0} ...".
201                  format(table.get("title", "")))
202
203     # Transform the data
204     logging.info("    Creating the data set for the {0} '{1}'.".
205                  format(table.get("type", ""), table.get("title", "")))
206     data = input_data.filter_data(table, continue_on_error=True)
207
208     # Prepare the header of the tables
209     try:
210         header = ["Test case", ]
211
212         if table["include-tests"] == "MRR":
213             hdr_param = "Receive Rate"
214         else:
215             hdr_param = "Throughput"
216
217         history = table.get("history", None)
218         if history:
219             for item in history:
220                 header.extend(
221                     ["{0} {1} [Mpps]".format(item["title"], hdr_param),
222                      "{0} Stdev [Mpps]".format(item["title"])])
223         header.extend(
224             ["{0} {1} [Mpps]".format(table["reference"]["title"], hdr_param),
225              "{0} Stdev [Mpps]".format(table["reference"]["title"]),
226              "{0} {1} [Mpps]".format(table["compare"]["title"], hdr_param),
227              "{0} Stdev [Mpps]".format(table["compare"]["title"]),
228              "Delta [%]"])
229         header_str = ",".join(header) + "\n"
230     except (AttributeError, KeyError) as err:
231         logging.error("The model is invalid, missing parameter: {0}".
232                       format(err))
233         return
234
235     # Prepare data to the table:
236     tbl_dict = dict()
237     for job, builds in table["reference"]["data"].items():
238         for build in builds:
239             for tst_name, tst_data in data[job][str(build)].iteritems():
240                 tst_name_mod = tst_name.replace("-ndrpdrdisc", "").\
241                     replace("-ndrpdr", "").replace("-pdrdisc", "").\
242                     replace("-ndrdisc", "").replace("-pdr", "").\
243                     replace("-ndr", "").\
244                     replace("1t1c", "1c").replace("2t1c", "1c").\
245                     replace("2t2c", "2c").replace("4t2c", "2c").\
246                     replace("4t4c", "4c").replace("8t4c", "4c")
247                 if "across topologies" in table["title"].lower():
248                     tst_name_mod = tst_name_mod.replace("2n1l-", "")
249                 if tbl_dict.get(tst_name_mod, None) is None:
250                     groups = re.search(REGEX_NIC, tst_data["parent"])
251                     nic = groups.group(0) if groups else ""
252                     name = "{0}-{1}".format(nic, "-".join(tst_data["name"].
253                                                           split("-")[:-1]))
254                     if "across testbeds" in table["title"].lower() or \
255                             "across topologies" in table["title"].lower():
256                         name = name.\
257                             replace("1t1c", "1c").replace("2t1c", "1c").\
258                             replace("2t2c", "2c").replace("4t2c", "2c").\
259                             replace("4t4c", "4c").replace("8t4c", "4c")
260                     tbl_dict[tst_name_mod] = {"name": name,
261                                               "ref-data": list(),
262                                               "cmp-data": list()}
263                 try:
264                     # TODO: Re-work when NDRPDRDISC tests are not used
265                     if table["include-tests"] == "MRR":
266                         tbl_dict[tst_name_mod]["ref-data"]. \
267                             append(tst_data["result"]["receive-rate"].avg)
268                     elif table["include-tests"] == "PDR":
269                         if tst_data["type"] == "PDR":
270                             tbl_dict[tst_name_mod]["ref-data"]. \
271                                 append(tst_data["throughput"]["value"])
272                         elif tst_data["type"] == "NDRPDR":
273                             tbl_dict[tst_name_mod]["ref-data"].append(
274                                 tst_data["throughput"]["PDR"]["LOWER"])
275                     elif table["include-tests"] == "NDR":
276                         if tst_data["type"] == "NDR":
277                             tbl_dict[tst_name_mod]["ref-data"]. \
278                                 append(tst_data["throughput"]["value"])
279                         elif tst_data["type"] == "NDRPDR":
280                             tbl_dict[tst_name_mod]["ref-data"].append(
281                                 tst_data["throughput"]["NDR"]["LOWER"])
282                     else:
283                         continue
284                 except TypeError:
285                     pass  # No data in output.xml for this test
286
287     for job, builds in table["compare"]["data"].items():
288         for build in builds:
289             for tst_name, tst_data in data[job][str(build)].iteritems():
290                 tst_name_mod = tst_name.replace("-ndrpdrdisc", ""). \
291                     replace("-ndrpdr", "").replace("-pdrdisc", ""). \
292                     replace("-ndrdisc", "").replace("-pdr", ""). \
293                     replace("-ndr", "").\
294                     replace("1t1c", "1c").replace("2t1c", "1c").\
295                     replace("2t2c", "2c").replace("4t2c", "2c").\
296                     replace("4t4c", "4c").replace("8t4c", "4c")
297                 if "across topologies" in table["title"].lower():
298                     tst_name_mod = tst_name_mod.replace("2n1l-", "")
299                 try:
300                     # TODO: Re-work when NDRPDRDISC tests are not used
301                     if table["include-tests"] == "MRR":
302                         tbl_dict[tst_name_mod]["cmp-data"]. \
303                             append(tst_data["result"]["receive-rate"].avg)
304                     elif table["include-tests"] == "PDR":
305                         if tst_data["type"] == "PDR":
306                             tbl_dict[tst_name_mod]["cmp-data"]. \
307                                 append(tst_data["throughput"]["value"])
308                         elif tst_data["type"] == "NDRPDR":
309                             tbl_dict[tst_name_mod]["cmp-data"].append(
310                                 tst_data["throughput"]["PDR"]["LOWER"])
311                     elif table["include-tests"] == "NDR":
312                         if tst_data["type"] == "NDR":
313                             tbl_dict[tst_name_mod]["cmp-data"]. \
314                                 append(tst_data["throughput"]["value"])
315                         elif tst_data["type"] == "NDRPDR":
316                             tbl_dict[tst_name_mod]["cmp-data"].append(
317                                 tst_data["throughput"]["NDR"]["LOWER"])
318                     else:
319                         continue
320                 except KeyError:
321                     pass
322                 except TypeError:
323                     tbl_dict.pop(tst_name_mod, None)
324     if history:
325         for item in history:
326             for job, builds in item["data"].items():
327                 for build in builds:
328                     for tst_name, tst_data in data[job][str(build)].iteritems():
329                         tst_name_mod = tst_name.replace("-ndrpdrdisc", ""). \
330                             replace("-ndrpdr", "").replace("-pdrdisc", ""). \
331                             replace("-ndrdisc", "").replace("-pdr", ""). \
332                             replace("-ndr", "").\
333                             replace("1t1c", "1c").replace("2t1c", "1c").\
334                             replace("2t2c", "2c").replace("4t2c", "2c").\
335                             replace("4t4c", "4c").replace("8t4c", "4c")
336                         if "across topologies" in table["title"].lower():
337                             tst_name_mod = tst_name_mod.replace("2n1l-", "")
338                         if tbl_dict.get(tst_name_mod, None) is None:
339                             continue
340                         if tbl_dict[tst_name_mod].get("history", None) is None:
341                             tbl_dict[tst_name_mod]["history"] = OrderedDict()
342                         if tbl_dict[tst_name_mod]["history"].get(item["title"],
343                                                              None) is None:
344                             tbl_dict[tst_name_mod]["history"][item["title"]] = \
345                                 list()
346                         try:
347                             # TODO: Re-work when NDRPDRDISC tests are not used
348                             if table["include-tests"] == "MRR":
349                                 tbl_dict[tst_name_mod]["history"][item["title"
350                                 ]].append(tst_data["result"]["receive-rate"].
351                                           avg)
352                             elif table["include-tests"] == "PDR":
353                                 if tst_data["type"] == "PDR":
354                                     tbl_dict[tst_name_mod]["history"][
355                                         item["title"]].\
356                                         append(tst_data["throughput"]["value"])
357                                 elif tst_data["type"] == "NDRPDR":
358                                     tbl_dict[tst_name_mod]["history"][item[
359                                         "title"]].append(tst_data["throughput"][
360                                         "PDR"]["LOWER"])
361                             elif table["include-tests"] == "NDR":
362                                 if tst_data["type"] == "NDR":
363                                     tbl_dict[tst_name_mod]["history"][
364                                         item["title"]].\
365                                         append(tst_data["throughput"]["value"])
366                                 elif tst_data["type"] == "NDRPDR":
367                                     tbl_dict[tst_name_mod]["history"][item[
368                                         "title"]].append(tst_data["throughput"][
369                                         "NDR"]["LOWER"])
370                             else:
371                                 continue
372                         except (TypeError, KeyError):
373                             pass
374
375     tbl_lst = list()
376     for tst_name in tbl_dict.keys():
377         item = [tbl_dict[tst_name]["name"], ]
378         if history:
379             if tbl_dict[tst_name].get("history", None) is not None:
380                 for hist_data in tbl_dict[tst_name]["history"].values():
381                     if hist_data:
382                         item.append(round(mean(hist_data) / 1000000, 2))
383                         item.append(round(stdev(hist_data) / 1000000, 2))
384                     else:
385                         item.extend([None, None])
386             else:
387                 item.extend([None, None])
388         data_t = tbl_dict[tst_name]["ref-data"]
389         if data_t:
390             item.append(round(mean(data_t) / 1000000, 2))
391             item.append(round(stdev(data_t) / 1000000, 2))
392         else:
393             item.extend([None, None])
394         data_t = tbl_dict[tst_name]["cmp-data"]
395         if data_t:
396             item.append(round(mean(data_t) / 1000000, 2))
397             item.append(round(stdev(data_t) / 1000000, 2))
398         else:
399             item.extend([None, None])
400         if item[-4] is not None and item[-2] is not None and item[-4] != 0:
401             item.append(int(relative_change(float(item[-4]), float(item[-2]))))
402         if len(item) == len(header):
403             tbl_lst.append(item)
404
405     # Sort the table according to the relative change
406     tbl_lst.sort(key=lambda rel: rel[-1], reverse=True)
407
408     # Generate csv tables:
409     csv_file = "{0}.csv".format(table["output-file"])
410     with open(csv_file, "w") as file_handler:
411         file_handler.write(header_str)
412         for test in tbl_lst:
413             file_handler.write(",".join([str(item) for item in test]) + "\n")
414
415     convert_csv_to_pretty_txt(csv_file, "{0}.txt".format(table["output-file"]))
416
417
418 def table_nics_comparison(table, input_data):
419     """Generate the table(s) with algorithm: table_nics_comparison
420     specified in the specification file.
421
422     :param table: Table to generate.
423     :param input_data: Data to process.
424     :type table: pandas.Series
425     :type input_data: InputData
426     """
427
428     logging.info("  Generating the table {0} ...".
429                  format(table.get("title", "")))
430
431     # Transform the data
432     logging.info("    Creating the data set for the {0} '{1}'.".
433                  format(table.get("type", ""), table.get("title", "")))
434     data = input_data.filter_data(table, continue_on_error=True)
435
436     # Prepare the header of the tables
437     try:
438         header = ["Test case", ]
439
440         if table["include-tests"] == "MRR":
441             hdr_param = "Receive Rate"
442         else:
443             hdr_param = "Throughput"
444
445         header.extend(
446             ["{0} {1} [Mpps]".format(table["reference"]["title"], hdr_param),
447              "{0} Stdev [Mpps]".format(table["reference"]["title"]),
448              "{0} {1} [Mpps]".format(table["compare"]["title"], hdr_param),
449              "{0} Stdev [Mpps]".format(table["compare"]["title"]),
450              "Delta [%]"])
451         header_str = ",".join(header) + "\n"
452     except (AttributeError, KeyError) as err:
453         logging.error("The model is invalid, missing parameter: {0}".
454                       format(err))
455         return
456
457     # Prepare data to the table:
458     tbl_dict = dict()
459     for job, builds in table["data"].items():
460         for build in builds:
461             for tst_name, tst_data in data[job][str(build)].iteritems():
462                 tst_name_mod = tst_name.replace("-ndrpdrdisc", "").\
463                     replace("-ndrpdr", "").replace("-pdrdisc", "").\
464                     replace("-ndrdisc", "").replace("-pdr", "").\
465                     replace("-ndr", "").\
466                     replace("1t1c", "1c").replace("2t1c", "1c").\
467                     replace("2t2c", "2c").replace("4t2c", "2c").\
468                     replace("4t4c", "4c").replace("8t4c", "4c")
469                 tst_name_mod = re.sub(REGEX_NIC, "", tst_name_mod)
470                 if tbl_dict.get(tst_name_mod, None) is None:
471                     name = "-".join(tst_data["name"].split("-")[:-1])
472                     tbl_dict[tst_name_mod] = {"name": name,
473                                               "ref-data": list(),
474                                               "cmp-data": list()}
475                 try:
476                     if table["include-tests"] == "MRR":
477                         result = tst_data["result"]["receive-rate"].avg
478                     elif table["include-tests"] == "PDR":
479                         result = tst_data["throughput"]["PDR"]["LOWER"]
480                     elif table["include-tests"] == "NDR":
481                         result = tst_data["throughput"]["NDR"]["LOWER"]
482                     else:
483                         result = None
484
485                     if result:
486                         if table["reference"]["nic"] in tst_data["tags"]:
487                             tbl_dict[tst_name_mod]["ref-data"].append(result)
488                         elif table["compare"]["nic"] in tst_data["tags"]:
489                             tbl_dict[tst_name_mod]["cmp-data"].append(result)
490                 except (TypeError, KeyError) as err:
491                     logging.debug("No data for {0}".format(tst_name))
492                     logging.debug(repr(err))
493                     # No data in output.xml for this test
494
495     tbl_lst = list()
496     for tst_name in tbl_dict.keys():
497         item = [tbl_dict[tst_name]["name"], ]
498         data_t = tbl_dict[tst_name]["ref-data"]
499         if data_t:
500             item.append(round(mean(data_t) / 1000000, 2))
501             item.append(round(stdev(data_t) / 1000000, 2))
502         else:
503             item.extend([None, None])
504         data_t = tbl_dict[tst_name]["cmp-data"]
505         if data_t:
506             item.append(round(mean(data_t) / 1000000, 2))
507             item.append(round(stdev(data_t) / 1000000, 2))
508         else:
509             item.extend([None, None])
510         if item[-4] is not None and item[-2] is not None and item[-4] != 0:
511             item.append(int(relative_change(float(item[-4]), float(item[-2]))))
512         if len(item) == len(header):
513             tbl_lst.append(item)
514
515     # Sort the table according to the relative change
516     tbl_lst.sort(key=lambda rel: rel[-1], reverse=True)
517
518     # Generate csv tables:
519     csv_file = "{0}.csv".format(table["output-file"])
520     with open(csv_file, "w") as file_handler:
521         file_handler.write(header_str)
522         for test in tbl_lst:
523             file_handler.write(",".join([str(item) for item in test]) + "\n")
524
525     convert_csv_to_pretty_txt(csv_file, "{0}.txt".format(table["output-file"]))
526
527
528 def table_soak_vs_ndr(table, input_data):
529     """Generate the table(s) with algorithm: table_soak_vs_ndr
530     specified in the specification file.
531
532     :param table: Table to generate.
533     :param input_data: Data to process.
534     :type table: pandas.Series
535     :type input_data: InputData
536     """
537
538     logging.info("  Generating the table {0} ...".
539                  format(table.get("title", "")))
540
541     # Transform the data
542     logging.info("    Creating the data set for the {0} '{1}'.".
543                  format(table.get("type", ""), table.get("title", "")))
544     data = input_data.filter_data(table, continue_on_error=True)
545
546     # Prepare the header of the table
547     try:
548         header = [
549             "Test case",
550             "{0} Throughput [Mpps]".format(table["reference"]["title"]),
551             "{0} Stdev [Mpps]".format(table["reference"]["title"]),
552             "{0} Throughput [Mpps]".format(table["compare"]["title"]),
553             "{0} Stdev [Mpps]".format(table["compare"]["title"]),
554             "Delta [%]", "Stdev of delta [%]"]
555         header_str = ",".join(header) + "\n"
556     except (AttributeError, KeyError) as err:
557         logging.error("The model is invalid, missing parameter: {0}".
558                       format(err))
559         return
560
561     # Create a list of available SOAK test results:
562     tbl_dict = dict()
563     for job, builds in table["compare"]["data"].items():
564         for build in builds:
565             for tst_name, tst_data in data[job][str(build)].iteritems():
566                 if tst_data["type"] == "SOAK":
567                     tst_name_mod = tst_name.replace("-soak", "")
568                     if tbl_dict.get(tst_name_mod, None) is None:
569                         groups = re.search(REGEX_NIC, tst_data["parent"])
570                         nic = groups.group(0) if groups else ""
571                         name = "{0}-{1}".format(nic, "-".join(tst_data["name"].
572                                                               split("-")[:-1]))
573                         tbl_dict[tst_name_mod] = {
574                             "name": name,
575                             "ref-data": list(),
576                             "cmp-data": list()
577                         }
578                     try:
579                         tbl_dict[tst_name_mod]["cmp-data"].append(
580                             tst_data["throughput"]["LOWER"])
581                     except (KeyError, TypeError):
582                         pass
583     tests_lst = tbl_dict.keys()
584
585     # Add corresponding NDR test results:
586     for job, builds in table["reference"]["data"].items():
587         for build in builds:
588             for tst_name, tst_data in data[job][str(build)].iteritems():
589                 tst_name_mod = tst_name.replace("-ndrpdr", "").\
590                     replace("-mrr", "")
591                 if tst_name_mod in tests_lst:
592                     try:
593                         if tst_data["type"] in ("NDRPDR", "MRR", "BMRR"):
594                             if table["include-tests"] == "MRR":
595                                 result = tst_data["result"]["receive-rate"].avg
596                             elif table["include-tests"] == "PDR":
597                                 result = tst_data["throughput"]["PDR"]["LOWER"]
598                             elif table["include-tests"] == "NDR":
599                                 result = tst_data["throughput"]["NDR"]["LOWER"]
600                             else:
601                                 result = None
602                             if result is not None:
603                                 tbl_dict[tst_name_mod]["ref-data"].append(
604                                     result)
605                     except (KeyError, TypeError):
606                         continue
607
608     tbl_lst = list()
609     for tst_name in tbl_dict.keys():
610         item = [tbl_dict[tst_name]["name"], ]
611         data_r = tbl_dict[tst_name]["ref-data"]
612         if data_r:
613             data_r_mean = mean(data_r)
614             item.append(round(data_r_mean / 1000000, 2))
615             data_r_stdev = stdev(data_r)
616             item.append(round(data_r_stdev / 1000000, 2))
617         else:
618             data_r_mean = None
619             item.extend([None, None])
620         data_c = tbl_dict[tst_name]["cmp-data"]
621         if data_c:
622             data_c_mean = mean(data_c)
623             item.append(round(data_c_mean / 1000000, 2))
624             data_c_stdev = stdev(data_c)
625             item.append(round(data_c_stdev / 1000000, 2))
626         else:
627             data_c_mean = None
628             item.extend([None, None])
629         if data_r_mean and data_c_mean is not None:
630             item.append(round(relative_change(data_r_mean, data_c_mean), 2))
631             delta, d_stdev = relative_change_stdev(
632                 data_r_mean, data_c_mean, data_r_stdev, data_c_stdev)
633             item.append(round(delta, 2))
634             item.append(round(d_stdev, 2))
635             tbl_lst.append(item)
636
637     # Sort the table according to the relative change
638     tbl_lst.sort(key=lambda rel: rel[-1], reverse=True)
639
640     # Generate csv tables:
641     csv_file = "{0}.csv".format(table["output-file"])
642     with open(csv_file, "w") as file_handler:
643         file_handler.write(header_str)
644         for test in tbl_lst:
645             file_handler.write(",".join([str(item) for item in test]) + "\n")
646
647     convert_csv_to_pretty_txt(csv_file, "{0}.txt".format(table["output-file"]))
648
649
650 def table_performance_trending_dashboard(table, input_data):
651     """Generate the table(s) with algorithm:
652     table_performance_trending_dashboard
653     specified in the specification file.
654
655     :param table: Table to generate.
656     :param input_data: Data to process.
657     :type table: pandas.Series
658     :type input_data: InputData
659     """
660
661     logging.info("  Generating the table {0} ...".
662                  format(table.get("title", "")))
663
664     # Transform the data
665     logging.info("    Creating the data set for the {0} '{1}'.".
666                  format(table.get("type", ""), table.get("title", "")))
667     data = input_data.filter_data(table, continue_on_error=True)
668
669     # Prepare the header of the tables
670     header = ["Test Case",
671               "Trend [Mpps]",
672               "Short-Term Change [%]",
673               "Long-Term Change [%]",
674               "Regressions [#]",
675               "Progressions [#]"
676               ]
677     header_str = ",".join(header) + "\n"
678
679     # Prepare data to the table:
680     tbl_dict = dict()
681     for job, builds in table["data"].items():
682         for build in builds:
683             for tst_name, tst_data in data[job][str(build)].iteritems():
684                 if tst_name.lower() in table["ignore-list"]:
685                     continue
686                 if tbl_dict.get(tst_name, None) is None:
687                     groups = re.search(REGEX_NIC, tst_data["parent"])
688                     if not groups:
689                         continue
690                     nic = groups.group(0)
691                     tbl_dict[tst_name] = {
692                         "name": "{0}-{1}".format(nic, tst_data["name"]),
693                         "data": OrderedDict()}
694                 try:
695                     tbl_dict[tst_name]["data"][str(build)] = \
696                         tst_data["result"]["receive-rate"]
697                 except (TypeError, KeyError):
698                     pass  # No data in output.xml for this test
699
700     tbl_lst = list()
701     for tst_name in tbl_dict.keys():
702         data_t = tbl_dict[tst_name]["data"]
703         if len(data_t) < 2:
704             continue
705
706         classification_lst, avgs = classify_anomalies(data_t)
707
708         win_size = min(len(data_t), table["window"])
709         long_win_size = min(len(data_t), table["long-trend-window"])
710
711         try:
712             max_long_avg = max(
713                 [x for x in avgs[-long_win_size:-win_size]
714                  if not isnan(x)])
715         except ValueError:
716             max_long_avg = nan
717         last_avg = avgs[-1]
718         avg_week_ago = avgs[max(-win_size, -len(avgs))]
719
720         if isnan(last_avg) or isnan(avg_week_ago) or avg_week_ago == 0.0:
721             rel_change_last = nan
722         else:
723             rel_change_last = round(
724                 ((last_avg - avg_week_ago) / avg_week_ago) * 100, 2)
725
726         if isnan(max_long_avg) or isnan(last_avg) or max_long_avg == 0.0:
727             rel_change_long = nan
728         else:
729             rel_change_long = round(
730                 ((last_avg - max_long_avg) / max_long_avg) * 100, 2)
731
732         if classification_lst:
733             if isnan(rel_change_last) and isnan(rel_change_long):
734                 continue
735             if (isnan(last_avg) or
736                 isnan(rel_change_last) or
737                 isnan(rel_change_long)):
738                 continue
739             tbl_lst.append(
740                 [tbl_dict[tst_name]["name"],
741                  round(last_avg / 1000000, 2),
742                  rel_change_last,
743                  rel_change_long,
744                  classification_lst[-win_size:].count("regression"),
745                  classification_lst[-win_size:].count("progression")])
746
747     tbl_lst.sort(key=lambda rel: rel[0])
748
749     tbl_sorted = list()
750     for nrr in range(table["window"], -1, -1):
751         tbl_reg = [item for item in tbl_lst if item[4] == nrr]
752         for nrp in range(table["window"], -1, -1):
753             tbl_out = [item for item in tbl_reg if item[5] == nrp]
754             tbl_out.sort(key=lambda rel: rel[2])
755             tbl_sorted.extend(tbl_out)
756
757     file_name = "{0}{1}".format(table["output-file"], table["output-file-ext"])
758
759     logging.info("    Writing file: '{0}'".format(file_name))
760     with open(file_name, "w") as file_handler:
761         file_handler.write(header_str)
762         for test in tbl_sorted:
763             file_handler.write(",".join([str(item) for item in test]) + '\n')
764
765     txt_file_name = "{0}.txt".format(table["output-file"])
766     logging.info("    Writing file: '{0}'".format(txt_file_name))
767     convert_csv_to_pretty_txt(file_name, txt_file_name)
768
769
770 def _generate_url(base, testbed, test_name):
771     """Generate URL to a trending plot from the name of the test case.
772
773     :param base: The base part of URL common to all test cases.
774     :param testbed: The testbed used for testing.
775     :param test_name: The name of the test case.
776     :type base: str
777     :type testbed: str
778     :type test_name: str
779     :returns: The URL to the plot with the trending data for the given test
780         case.
781     :rtype str
782     """
783
784     url = base
785     file_name = ""
786     anchor = ".html#"
787     feature = ""
788
789     if "lbdpdk" in test_name or "lbvpp" in test_name:
790         file_name = "link_bonding"
791
792     elif "114b" in test_name and "vhost" in test_name:
793         file_name = "vts"
794
795     elif "testpmd" in test_name or "l3fwd" in test_name:
796         file_name = "dpdk"
797
798     elif "memif" in test_name:
799         file_name = "container_memif"
800         feature = "-base"
801
802     elif "srv6" in test_name:
803         file_name = "srv6"
804
805     elif "vhost" in test_name:
806         if "l2xcbase" in test_name or "l2bdbasemaclrn" in test_name:
807             file_name = "vm_vhost_l2"
808             if "114b" in test_name:
809                 feature = ""
810             elif "l2xcbase" in test_name and "x520" in test_name:
811                 feature = "-base-l2xc"
812             elif "l2bdbasemaclrn" in test_name and "x520" in test_name:
813                 feature = "-base-l2bd"
814             else:
815                 feature = "-base"
816         elif "ip4base" in test_name:
817             file_name = "vm_vhost_ip4"
818             feature = "-base"
819
820     elif "ipsecbasetnlsw" in test_name:
821         file_name = "ipsecsw"
822         feature = "-base-scale"
823
824     elif "ipsec" in test_name:
825         file_name = "ipsec"
826         feature = "-base-scale"
827
828     elif "ethip4lispip" in test_name or "ethip4vxlan" in test_name:
829         file_name = "ip4_tunnels"
830         feature = "-base"
831
832     elif "ip4base" in test_name or "ip4scale" in test_name:
833         file_name = "ip4"
834         if "xl710" in test_name:
835             feature = "-base-scale-features"
836         elif "iacl" in test_name:
837             feature = "-features-iacl"
838         elif "oacl" in test_name:
839             feature = "-features-oacl"
840         elif "snat" in test_name or "cop" in test_name:
841             feature = "-features"
842         else:
843             feature = "-base-scale"
844
845     elif "ip6base" in test_name or "ip6scale" in test_name:
846         file_name = "ip6"
847         feature = "-base-scale"
848
849     elif "l2xcbase" in test_name or "l2xcscale" in test_name \
850             or "l2bdbasemaclrn" in test_name or "l2bdscale" in test_name \
851             or "l2dbbasemaclrn" in test_name or "l2dbscale" in test_name:
852         file_name = "l2"
853         if "macip" in test_name:
854             feature = "-features-macip"
855         elif "iacl" in test_name:
856             feature = "-features-iacl"
857         elif "oacl" in test_name:
858             feature = "-features-oacl"
859         else:
860             feature = "-base-scale"
861
862     if "x520" in test_name:
863         nic = "x520-"
864     elif "x710" in test_name:
865         nic = "x710-"
866     elif "xl710" in test_name:
867         nic = "xl710-"
868     elif "xxv710" in test_name:
869         nic = "xxv710-"
870     elif "vic1227" in test_name:
871         nic = "vic1227-"
872     elif "vic1385" in test_name:
873         nic = "vic1385-"
874     else:
875         nic = ""
876     anchor += nic
877
878     if "64b" in test_name:
879         framesize = "64b"
880     elif "78b" in test_name:
881         framesize = "78b"
882     elif "imix" in test_name:
883         framesize = "imix"
884     elif "9000b" in test_name:
885         framesize = "9000b"
886     elif "1518b" in test_name:
887         framesize = "1518b"
888     elif "114b" in test_name:
889         framesize = "114b"
890     else:
891         framesize = ""
892     anchor += framesize + '-'
893
894     if "1t1c" in test_name:
895         anchor += "1t1c"
896     elif "2t2c" in test_name:
897         anchor += "2t2c"
898     elif "4t4c" in test_name:
899         anchor += "4t4c"
900     elif "2t1c" in test_name:
901         anchor += "2t1c"
902     elif "4t2c" in test_name:
903         anchor += "4t2c"
904     elif "8t4c" in test_name:
905         anchor += "8t4c"
906
907     return url + file_name + '-' + testbed + '-' + nic + framesize + feature + \
908            anchor + feature
909
910
911 def table_performance_trending_dashboard_html(table, input_data):
912     """Generate the table(s) with algorithm:
913     table_performance_trending_dashboard_html specified in the specification
914     file.
915
916     :param table: Table to generate.
917     :param input_data: Data to process.
918     :type table: dict
919     :type input_data: InputData
920     """
921
922     testbed = table.get("testbed", None)
923     if testbed is None:
924         logging.error("The testbed is not defined for the table '{0}'.".
925                       format(table.get("title", "")))
926         return
927
928     logging.info("  Generating the table {0} ...".
929                  format(table.get("title", "")))
930
931     try:
932         with open(table["input-file"], 'rb') as csv_file:
933             csv_content = csv.reader(csv_file, delimiter=',', quotechar='"')
934             csv_lst = [item for item in csv_content]
935     except KeyError:
936         logging.warning("The input file is not defined.")
937         return
938     except csv.Error as err:
939         logging.warning("Not possible to process the file '{0}'.\n{1}".
940                         format(table["input-file"], err))
941         return
942
943     # Table:
944     dashboard = ET.Element("table", attrib=dict(width="100%", border='0'))
945
946     # Table header:
947     tr = ET.SubElement(dashboard, "tr", attrib=dict(bgcolor="#7eade7"))
948     for idx, item in enumerate(csv_lst[0]):
949         alignment = "left" if idx == 0 else "center"
950         th = ET.SubElement(tr, "th", attrib=dict(align=alignment))
951         th.text = item
952
953     # Rows:
954     colors = {"regression": ("#ffcccc", "#ff9999"),
955               "progression": ("#c6ecc6", "#9fdf9f"),
956               "normal": ("#e9f1fb", "#d4e4f7")}
957     for r_idx, row in enumerate(csv_lst[1:]):
958         if int(row[4]):
959             color = "regression"
960         elif int(row[5]):
961             color = "progression"
962         else:
963             color = "normal"
964         background = colors[color][r_idx % 2]
965         tr = ET.SubElement(dashboard, "tr", attrib=dict(bgcolor=background))
966
967         # Columns:
968         for c_idx, item in enumerate(row):
969             alignment = "left" if c_idx == 0 else "center"
970             td = ET.SubElement(tr, "td", attrib=dict(align=alignment))
971             # Name:
972             if c_idx == 0:
973                 url = _generate_url("../trending/", testbed, item)
974                 ref = ET.SubElement(td, "a", attrib=dict(href=url))
975                 ref.text = item
976             else:
977                 td.text = item
978     try:
979         with open(table["output-file"], 'w') as html_file:
980             logging.info("    Writing file: '{0}'".format(table["output-file"]))
981             html_file.write(".. raw:: html\n\n\t")
982             html_file.write(ET.tostring(dashboard))
983             html_file.write("\n\t<p><br><br></p>\n")
984     except KeyError:
985         logging.warning("The output file is not defined.")
986         return
987
988
989 def table_last_failed_tests(table, input_data):
990     """Generate the table(s) with algorithm: table_last_failed_tests
991     specified in the specification file.
992
993     :param table: Table to generate.
994     :param input_data: Data to process.
995     :type table: pandas.Series
996     :type input_data: InputData
997     """
998
999     logging.info("  Generating the table {0} ...".
1000                  format(table.get("title", "")))
1001
1002     # Transform the data
1003     logging.info("    Creating the data set for the {0} '{1}'.".
1004                  format(table.get("type", ""), table.get("title", "")))
1005     data = input_data.filter_data(table, continue_on_error=True)
1006
1007     if data is None or data.empty:
1008         logging.warn("    No data for the {0} '{1}'.".
1009                      format(table.get("type", ""), table.get("title", "")))
1010         return
1011
1012     tbl_list = list()
1013     for job, builds in table["data"].items():
1014         for build in builds:
1015             build = str(build)
1016             try:
1017                 version = input_data.metadata(job, build).get("version", "")
1018             except KeyError:
1019                 logging.error("Data for {job}: {build} is not present.".
1020                               format(job=job, build=build))
1021                 return
1022             tbl_list.append(build)
1023             tbl_list.append(version)
1024             for tst_name, tst_data in data[job][build].iteritems():
1025                 if tst_data["status"] != "FAIL":
1026                     continue
1027                 groups = re.search(REGEX_NIC, tst_data["parent"])
1028                 if not groups:
1029                     continue
1030                 nic = groups.group(0)
1031                 tbl_list.append("{0}-{1}".format(nic, tst_data["name"]))
1032
1033     file_name = "{0}{1}".format(table["output-file"], table["output-file-ext"])
1034     logging.info("    Writing file: '{0}'".format(file_name))
1035     with open(file_name, "w") as file_handler:
1036         for test in tbl_list:
1037             file_handler.write(test + '\n')
1038
1039
1040 def table_failed_tests(table, input_data):
1041     """Generate the table(s) with algorithm: table_failed_tests
1042     specified in the specification file.
1043
1044     :param table: Table to generate.
1045     :param input_data: Data to process.
1046     :type table: pandas.Series
1047     :type input_data: InputData
1048     """
1049
1050     logging.info("  Generating the table {0} ...".
1051                  format(table.get("title", "")))
1052
1053     # Transform the data
1054     logging.info("    Creating the data set for the {0} '{1}'.".
1055                  format(table.get("type", ""), table.get("title", "")))
1056     data = input_data.filter_data(table, continue_on_error=True)
1057
1058     # Prepare the header of the tables
1059     header = ["Test Case",
1060               "Failures [#]",
1061               "Last Failure [Time]",
1062               "Last Failure [VPP-Build-Id]",
1063               "Last Failure [CSIT-Job-Build-Id]"]
1064
1065     # Generate the data for the table according to the model in the table
1066     # specification
1067
1068     now = dt.utcnow()
1069     timeperiod = timedelta(int(table.get("window", 7)))
1070
1071     tbl_dict = dict()
1072     for job, builds in table["data"].items():
1073         for build in builds:
1074             build = str(build)
1075             for tst_name, tst_data in data[job][build].iteritems():
1076                 if tst_name.lower() in table["ignore-list"]:
1077                     continue
1078                 if tbl_dict.get(tst_name, None) is None:
1079                     groups = re.search(REGEX_NIC, tst_data["parent"])
1080                     if not groups:
1081                         continue
1082                     nic = groups.group(0)
1083                     tbl_dict[tst_name] = {
1084                         "name": "{0}-{1}".format(nic, tst_data["name"]),
1085                         "data": OrderedDict()}
1086                 try:
1087                     generated = input_data.metadata(job, build).\
1088                         get("generated", "")
1089                     if not generated:
1090                         continue
1091                     then = dt.strptime(generated, "%Y%m%d %H:%M")
1092                     if (now - then) <= timeperiod:
1093                         tbl_dict[tst_name]["data"][build] = (
1094                             tst_data["status"],
1095                             generated,
1096                             input_data.metadata(job, build).get("version", ""),
1097                             build)
1098                 except (TypeError, KeyError) as err:
1099                     logging.warning("tst_name: {} - err: {}".
1100                                     format(tst_name, repr(err)))
1101
1102     max_fails = 0
1103     tbl_lst = list()
1104     for tst_data in tbl_dict.values():
1105         fails_nr = 0
1106         for val in tst_data["data"].values():
1107             if val[0] == "FAIL":
1108                 fails_nr += 1
1109                 fails_last_date = val[1]
1110                 fails_last_vpp = val[2]
1111                 fails_last_csit = val[3]
1112         if fails_nr:
1113             max_fails = fails_nr if fails_nr > max_fails else max_fails
1114             tbl_lst.append([tst_data["name"],
1115                             fails_nr,
1116                             fails_last_date,
1117                             fails_last_vpp,
1118                             "mrr-daily-build-{0}".format(fails_last_csit)])
1119
1120     tbl_lst.sort(key=lambda rel: rel[2], reverse=True)
1121     tbl_sorted = list()
1122     for nrf in range(max_fails, -1, -1):
1123         tbl_fails = [item for item in tbl_lst if item[1] == nrf]
1124         tbl_sorted.extend(tbl_fails)
1125     file_name = "{0}{1}".format(table["output-file"], table["output-file-ext"])
1126
1127     logging.info("    Writing file: '{0}'".format(file_name))
1128     with open(file_name, "w") as file_handler:
1129         file_handler.write(",".join(header) + "\n")
1130         for test in tbl_sorted:
1131             file_handler.write(",".join([str(item) for item in test]) + '\n')
1132
1133     txt_file_name = "{0}.txt".format(table["output-file"])
1134     logging.info("    Writing file: '{0}'".format(txt_file_name))
1135     convert_csv_to_pretty_txt(file_name, txt_file_name)
1136
1137
1138 def table_failed_tests_html(table, input_data):
1139     """Generate the table(s) with algorithm: table_failed_tests_html
1140     specified in the specification file.
1141
1142     :param table: Table to generate.
1143     :param input_data: Data to process.
1144     :type table: pandas.Series
1145     :type input_data: InputData
1146     """
1147
1148     testbed = table.get("testbed", None)
1149     if testbed is None:
1150         logging.error("The testbed is not defined for the table '{0}'.".
1151                       format(table.get("title", "")))
1152         return
1153
1154     logging.info("  Generating the table {0} ...".
1155                  format(table.get("title", "")))
1156
1157     try:
1158         with open(table["input-file"], 'rb') as csv_file:
1159             csv_content = csv.reader(csv_file, delimiter=',', quotechar='"')
1160             csv_lst = [item for item in csv_content]
1161     except KeyError:
1162         logging.warning("The input file is not defined.")
1163         return
1164     except csv.Error as err:
1165         logging.warning("Not possible to process the file '{0}'.\n{1}".
1166                         format(table["input-file"], err))
1167         return
1168
1169     # Table:
1170     failed_tests = ET.Element("table", attrib=dict(width="100%", border='0'))
1171
1172     # Table header:
1173     tr = ET.SubElement(failed_tests, "tr", attrib=dict(bgcolor="#7eade7"))
1174     for idx, item in enumerate(csv_lst[0]):
1175         alignment = "left" if idx == 0 else "center"
1176         th = ET.SubElement(tr, "th", attrib=dict(align=alignment))
1177         th.text = item
1178
1179     # Rows:
1180     colors = ("#e9f1fb", "#d4e4f7")
1181     for r_idx, row in enumerate(csv_lst[1:]):
1182         background = colors[r_idx % 2]
1183         tr = ET.SubElement(failed_tests, "tr", attrib=dict(bgcolor=background))
1184
1185         # Columns:
1186         for c_idx, item in enumerate(row):
1187             alignment = "left" if c_idx == 0 else "center"
1188             td = ET.SubElement(tr, "td", attrib=dict(align=alignment))
1189             # Name:
1190             if c_idx == 0:
1191                 url = _generate_url("../trending/", testbed, item)
1192                 ref = ET.SubElement(td, "a", attrib=dict(href=url))
1193                 ref.text = item
1194             else:
1195                 td.text = item
1196     try:
1197         with open(table["output-file"], 'w') as html_file:
1198             logging.info("    Writing file: '{0}'".format(table["output-file"]))
1199             html_file.write(".. raw:: html\n\n\t")
1200             html_file.write(ET.tostring(failed_tests))
1201             html_file.write("\n\t<p><br><br></p>\n")
1202     except KeyError:
1203         logging.warning("The output file is not defined.")
1204         return