Merge pull request #21267 from mshabunin:fix-kw-2021-12
[platform/upstream/opencv.git] / modules / ts / misc / report.py
1 #!/usr/bin/env python
2
3 from __future__ import print_function
4 import testlog_parser, sys, os, xml, re, glob
5 from table_formatter import *
6 from optparse import OptionParser
7
8 if __name__ == "__main__":
9     parser = OptionParser()
10     parser.add_option("-o", "--output", dest="format", help="output results in text format (can be 'txt', 'html' or 'auto' - default)", metavar="FMT", default="auto")
11     parser.add_option("-u", "--units", dest="units", help="units for output values (s, ms (default), us, ns or ticks)", metavar="UNITS", default="ms")
12     parser.add_option("-c", "--columns", dest="columns", help="comma-separated list of columns to show", metavar="COLS", default="")
13     parser.add_option("-f", "--filter", dest="filter", help="regex to filter tests", metavar="REGEX", default=None)
14     parser.add_option("", "--show-all", action="store_true", dest="showall", default=False, help="also include empty and \"notrun\" lines")
15     (options, args) = parser.parse_args()
16
17     if len(args) < 1:
18         print("Usage:\n", os.path.basename(sys.argv[0]), "<log_name1>.xml", file=sys.stderr)
19         exit(0)
20
21     options.generateHtml = detectHtmlOutputType(options.format)
22
23     # expand wildcards and filter duplicates
24     files = []
25     files1 = []
26     for arg in args:
27         if ("*" in arg) or ("?" in arg):
28             files1.extend([os.path.abspath(f) for f in glob.glob(arg)])
29         else:
30             files.append(os.path.abspath(arg))
31     seen = set()
32     files = [ x for x in files if x not in seen and not seen.add(x)]
33     files.extend((set(files1) - set(files)))
34     args = files
35
36     # load test data
37     tests = []
38     files = []
39     for arg in set(args):
40         try:
41             cases = testlog_parser.parseLogFile(arg)
42             if cases:
43                 files.append(os.path.basename(arg))
44                 tests.extend(cases)
45         except:
46             pass
47
48     if options.filter:
49         expr = re.compile(options.filter)
50         tests = [t for t in tests if expr.search(str(t))]
51
52     tbl = table(", ".join(files))
53     if options.columns:
54         metrics = [s.strip() for s in options.columns.split(",")]
55         metrics = [m for m in metrics if m and not m.endswith("%") and m in metrix_table]
56     else:
57         metrics = None
58     if not metrics:
59         metrics = ["name", "samples", "outliers", "min", "median", "gmean", "mean", "stddev"]
60     if "name" not in metrics:
61         metrics.insert(0, "name")
62
63     for m in metrics:
64         if m == "name":
65             tbl.newColumn(m, metrix_table[m][0])
66         else:
67             tbl.newColumn(m, metrix_table[m][0], align = "center")
68
69     needNewRow = True
70     for case in sorted(tests, key=lambda x: str(x)):
71         if needNewRow:
72             tbl.newRow()
73             if not options.showall:
74                 needNewRow = False
75         status = case.get("status")
76         if status != "run":
77             if status != "notrun":
78                 needNewRow = True
79             for m in metrics:
80                 if m == "name":
81                     tbl.newCell(m, str(case))
82                 else:
83                     tbl.newCell(m, status, color = "red")
84         else:
85             needNewRow = True
86             for m in metrics:
87                 val = metrix_table[m][1](case, None, options.units)
88                 if isinstance(val, float):
89                     tbl.newCell(m, "%.2f %s" % (val, options.units), val)
90                 else:
91                     tbl.newCell(m, val, val)
92     if not needNewRow:
93         tbl.trimLastRow()
94
95     # output table
96     if options.generateHtml:
97         if options.format == "moinwiki":
98             tbl.htmlPrintTable(sys.stdout, True)
99         else:
100             htmlPrintHeader(sys.stdout, "Report %s tests from %s" % (len(tests), ", ".join(files)))
101             tbl.htmlPrintTable(sys.stdout)
102             htmlPrintFooter(sys.stdout)
103     else:
104         tbl.consolePrintTable(sys.stdout)