3 from __future__ import print_function
4 import testlog_parser, sys, os, xml, glob, re
5 from table_formatter import *
6 from optparse import OptionParser
8 numeric_re = re.compile("(\d+)")
9 cvtype_re = re.compile("(8U|8S|16U|16S|32S|32F|64F)C(\d{1,3})")
10 cvtypes = { '8U': 0, '8S': 1, '16U': 2, '16S': 3, '32S': 4, '32F': 5, '64F': 6 }
12 convert = lambda text: int(text) if text.isdigit() else text
13 keyselector = lambda a: cvtype_re.sub(lambda match: " " + str(cvtypes.get(match.group(1), 7) + (int(match.group(2))-1) * 8) + " ", a)
14 alphanum_keyselector = lambda key: [ convert(c) for c in numeric_re.split(keyselector(key)) ]
16 def getSetName(tset, idx, columns, short = True):
17 if columns and len(columns) > idx:
23 name = tset[0].replace(".xml","").replace("_", "\n")
25 return prefix + "\n" + ("-"*int(len(max(prefix.split("\n"), key=len))*1.5)) + "\n" + name
28 if __name__ == "__main__":
30 print("Usage:\n", os.path.basename(sys.argv[0]), "<log_name1>.xml [<log_name2>.xml ...]", file=sys.stderr)
33 parser = OptionParser()
34 parser.add_option("-o", "--output", dest="format", help="output results in text format (can be 'txt', 'html', 'markdown', 'tabs' or 'auto' - default)", metavar="FMT", default="auto")
35 parser.add_option("-m", "--metric", dest="metric", help="output metric", metavar="NAME", default="gmean")
36 parser.add_option("-u", "--units", dest="units", help="units for output values (s, ms (default), us, ns or ticks)", metavar="UNITS", default="ms")
37 parser.add_option("-f", "--filter", dest="filter", help="regex to filter tests", metavar="REGEX", default=None)
38 parser.add_option("", "--module", dest="module", default=None, metavar="NAME", help="module prefix for test names")
39 parser.add_option("", "--columns", dest="columns", default=None, metavar="NAMES", help="comma-separated list of column aliases")
40 parser.add_option("", "--no-relatives", action="store_false", dest="calc_relatives", default=True, help="do not output relative values")
41 parser.add_option("", "--with-cycles-reduction", action="store_true", dest="calc_cr", default=False, help="output cycle reduction percentages")
42 parser.add_option("", "--with-score", action="store_true", dest="calc_score", default=False, help="output automatic classification of speedups")
43 parser.add_option("", "--progress", action="store_true", dest="progress_mode", default=False, help="enable progress mode")
44 parser.add_option("", "--regressions", dest="regressions", default=None, metavar="LIST", help="comma-separated custom regressions map: \"[r][c]#current-#reference\" (indexes of columns are 0-based, \"r\" - reverse flag, \"c\" - color flag for base data)")
45 parser.add_option("", "--show-all", action="store_true", dest="showall", default=False, help="also include empty and \"notrun\" lines")
46 parser.add_option("", "--match", dest="match", default=None)
47 parser.add_option("", "--match-replace", dest="match_replace", default="")
48 parser.add_option("", "--regressions-only", dest="regressionsOnly", default=None, metavar="X-FACTOR", help="show only tests with performance regressions not")
49 parser.add_option("", "--intersect-logs", dest="intersect_logs", default=False, help="show only tests present in all log files")
50 parser.add_option("", "--show_units", action="store_true", dest="show_units", help="append units into table cells")
51 (options, args) = parser.parse_args()
53 options.generateHtml = detectHtmlOutputType(options.format)
54 if options.metric not in metrix_table:
55 options.metric = "gmean"
56 if options.metric.endswith("%") or options.metric.endswith("$"):
57 options.calc_relatives = False
58 options.calc_cr = False
60 options.columns = [s.strip().replace("\\n", "\n") for s in options.columns.split(",")]
62 if options.regressions:
63 assert not options.progress_mode, 'unsupported mode'
65 def parseRegressionColumn(s):
66 """ Format: '[r][c]<uint>-<uint>' """
67 reverse = s.startswith('r')
70 addColor = s.startswith('c')
73 parts = s.split('-', 1)
74 link = (int(parts[0]), int(parts[1]), reverse, addColor)
75 assert link[0] != link[1]
78 options.regressions = [parseRegressionColumn(s) for s in options.regressions.split(',')]
80 show_units = options.units if options.show_units else None
82 # expand wildcards and filter duplicates
86 if ("*" in arg) or ("?" in arg):
87 flist = [os.path.abspath(f) for f in glob.glob(arg)]
88 flist = sorted(flist, key= lambda text: str(text).replace("M", "_"))
89 files.extend([ x for x in flist if x not in seen and not seen.add(x)])
91 fname = os.path.abspath(arg)
92 if fname not in seen and not seen.add(fname):
95 # read all passed files
99 tests = testlog_parser.parseLogFile(arg)
101 expr = re.compile(options.filter)
102 tests = [t for t in tests if expr.search(str(t))]
104 tests = [t for t in tests if t.get("status") != "notrun"]
106 test_sets.append((os.path.basename(arg), tests))
107 except IOError as err:
108 sys.stderr.write("IOError reading \"" + arg + "\" - " + str(err) + os.linesep)
109 except xml.parsers.expat.ExpatError as err:
110 sys.stderr.write("ExpatError reading \"" + arg + "\" - " + str(err) + os.linesep)
113 sys.stderr.write("Error: no test data found" + os.linesep)
116 setsCount = len(test_sets)
118 if options.regressions is None:
119 reference = -1 if options.progress_mode else 0
120 options.regressions = [(i, reference, False, True) for i in range(1, len(test_sets))]
122 for link in options.regressions:
123 (i, ref, reverse, addColor) = link
124 assert i >= 0 and i < setsCount
125 assert ref < setsCount
130 name_extractor = lambda name: str(name)
132 reg = re.compile(options.match)
133 name_extractor = lambda name: reg.sub(options.match_replace, str(name))
135 for i in range(setsCount):
136 for case in test_sets[i][1]:
137 name = name_extractor(case)
139 name = options.module + "::" + name
140 if name not in test_cases:
141 test_cases[name] = [None] * setsCount
142 test_cases[name][i] = case
145 getter = metrix_table[options.metric][1]
146 getter_score = metrix_table["score"][1] if options.calc_score else None
147 getter_p = metrix_table[options.metric + "%"][1] if options.calc_relatives else None
148 getter_cr = metrix_table[options.metric + "$"][1] if options.calc_cr else None
149 tbl = table('%s (%s)' % (metrix_table[options.metric][0], options.units), options.format)
152 tbl.newColumn("name", "Name of Test", align = "left", cssclass = "col_name")
153 for i in range(setsCount):
154 tbl.newColumn(str(i), getSetName(test_sets[i], i, options.columns, False), align = "center")
156 def addHeaderColumns(suffix, description, cssclass):
157 for link in options.regressions:
158 (i, ref, reverse, addColor) = link
161 current_set = test_sets[i]
162 current = getSetName(current_set, i, options.columns)
164 reference_set = test_sets[ref]
165 reference = getSetName(reference_set, ref, options.columns)
167 reference = 'previous'
168 tbl.newColumn(str(i) + '-' + str(ref) + suffix, '%s\nvs\n%s\n(%s)' % (current, reference, description), align='center', cssclass=cssclass)
171 addHeaderColumns(suffix='$', description='cycles reduction', cssclass='col_cr')
172 if options.calc_relatives:
173 addHeaderColumns(suffix='%', description='x-factor', cssclass='col_rel')
174 if options.calc_score:
175 addHeaderColumns(suffix='S', description='score', cssclass='col_name')
181 for name in sorted(test_cases.keys(), key=alphanum_keyselector):
182 cases = test_cases[name]
184 lastRow = tbl.newRow()
185 if not options.showall:
187 tbl.newCell("name", name)
189 groupName = next(c for c in cases if c).shortName()
190 if groupName != prevGroupName:
191 prop = lastRow.props.get("cssclass", "")
192 if "firstingroup" not in prop:
193 lastRow.props["cssclass"] = prop + " firstingroup"
194 prevGroupName = groupName
196 for i in range(setsCount):
199 if options.intersect_logs:
202 tbl.newCell(str(i), "-")
204 status = case.get("status")
206 tbl.newCell(str(i), status, color="red")
208 val = getter(case, cases[0], options.units)
211 tbl.newCell(str(i), formatValue(val, options.metric, show_units), val)
214 for link in options.regressions:
215 (i, reference, reverse, addColor) = link
217 i, reference = reference, i
218 tblCellID = str(i) + '-' + str(reference)
221 if options.calc_relatives:
222 tbl.newCell(tblCellID + "%", "-")
224 tbl.newCell(tblCellID + "$", "-")
225 if options.calc_score:
226 tbl.newCell(tblCellID + "$", "-")
228 status = case.get("status")
230 tbl.newCell(str(i), status, color="red")
231 if status != "notrun":
233 if options.calc_relatives:
234 tbl.newCell(tblCellID + "%", "-", color="red")
236 tbl.newCell(tblCellID + "$", "-", color="red")
237 if options.calc_score:
238 tbl.newCell(tblCellID + "S", "-", color="red")
240 val = getter(case, cases[0], options.units)
241 def getRegression(fn):
243 for j in reversed(range(i)) if reference < 0 else [reference]:
245 if r is not None and r.get("status") == 'run':
246 return fn(case, r, options.units)
247 valp = getRegression(getter_p) if options.calc_relatives or options.progress_mode else None
248 valcr = getRegression(getter_cr) if options.calc_cr else None
249 val_score = getRegression(getter_score) if options.calc_score else None
260 tbl.newCell(str(i), formatValue(val, options.metric, show_units), val, color=color)
263 if r is not None and r.get("status") == 'run':
264 val = getter(r, cases[0], options.units)
265 tbl.newCell(str(reference), formatValue(val, options.metric, show_units), val, color=color)
266 if options.calc_relatives:
267 tbl.newCell(tblCellID + "%", formatValue(valp, "%"), valp, color=color, bold=color)
269 tbl.newCell(tblCellID + "$", formatValue(valcr, "$"), valcr, color=color, bold=color)
270 if options.calc_score:
271 tbl.newCell(tblCellID + "S", formatValue(val_score, "S"), val_score, color = color, bold = color)
276 if options.regressionsOnly:
277 for r in reversed(range(len(tbl.rows))):
278 for i in range(1, len(options.regressions) + 1):
279 val = tbl.rows[r].cells[len(tbl.rows[r].cells) - i].value
280 if val is not None and val < float(options.regressionsOnly):
286 if options.generateHtml:
287 if options.format == "moinwiki":
288 tbl.htmlPrintTable(sys.stdout, True)
290 htmlPrintHeader(sys.stdout, "Summary report for %s tests from %s test logs" % (len(test_cases), setsCount))
291 tbl.htmlPrintTable(sys.stdout)
292 htmlPrintFooter(sys.stdout)
294 tbl.consolePrintTable(sys.stdout)
296 if options.regressionsOnly:
297 sys.exit(len(tbl.rows))