summary.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import testlog_parser, sys, os, xml, glob, re
  4. from table_formatter import *
  5. from optparse import OptionParser
  6. numeric_re = re.compile("(\d+)")
  7. cvtype_re = re.compile("(8U|8S|16U|16S|32S|32F|64F)C(\d{1,3})")
  8. cvtypes = { '8U': 0, '8S': 1, '16U': 2, '16S': 3, '32S': 4, '32F': 5, '64F': 6 }
  9. convert = lambda text: int(text) if text.isdigit() else text
  10. keyselector = lambda a: cvtype_re.sub(lambda match: " " + str(cvtypes.get(match.group(1), 7) + (int(match.group(2))-1) * 8) + " ", a)
  11. alphanum_keyselector = lambda key: [ convert(c) for c in numeric_re.split(keyselector(key)) ]
  12. def getSetName(tset, idx, columns, short = True):
  13. if columns and len(columns) > idx:
  14. prefix = columns[idx]
  15. else:
  16. prefix = None
  17. if short and prefix:
  18. return prefix
  19. name = tset[0].replace(".xml","").replace("_", "\n")
  20. if prefix:
  21. return prefix + "\n" + ("-"*int(len(max(prefix.split("\n"), key=len))*1.5)) + "\n" + name
  22. return name
  23. if __name__ == "__main__":
  24. if len(sys.argv) < 2:
  25. print("Usage:\n", os.path.basename(sys.argv[0]), "<log_name1>.xml [<log_name2>.xml ...]", file=sys.stderr)
  26. exit(0)
  27. parser = OptionParser()
  28. 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")
  29. parser.add_option("-m", "--metric", dest="metric", help="output metric", metavar="NAME", default="gmean")
  30. parser.add_option("-u", "--units", dest="units", help="units for output values (s, ms (default), us, ns or ticks)", metavar="UNITS", default="ms")
  31. parser.add_option("-f", "--filter", dest="filter", help="regex to filter tests", metavar="REGEX", default=None)
  32. parser.add_option("", "--module", dest="module", default=None, metavar="NAME", help="module prefix for test names")
  33. parser.add_option("", "--columns", dest="columns", default=None, metavar="NAMES", help="comma-separated list of column aliases")
  34. parser.add_option("", "--no-relatives", action="store_false", dest="calc_relatives", default=True, help="do not output relative values")
  35. parser.add_option("", "--with-cycles-reduction", action="store_true", dest="calc_cr", default=False, help="output cycle reduction percentages")
  36. parser.add_option("", "--with-score", action="store_true", dest="calc_score", default=False, help="output automatic classification of speedups")
  37. parser.add_option("", "--progress", action="store_true", dest="progress_mode", default=False, help="enable progress mode")
  38. 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)")
  39. parser.add_option("", "--show-all", action="store_true", dest="showall", default=False, help="also include empty and \"notrun\" lines")
  40. parser.add_option("", "--match", dest="match", default=None)
  41. parser.add_option("", "--match-replace", dest="match_replace", default="")
  42. parser.add_option("", "--regressions-only", dest="regressionsOnly", default=None, metavar="X-FACTOR", help="show only tests with performance regressions not")
  43. parser.add_option("", "--intersect-logs", dest="intersect_logs", default=False, help="show only tests present in all log files")
  44. parser.add_option("", "--show_units", action="store_true", dest="show_units", help="append units into table cells")
  45. (options, args) = parser.parse_args()
  46. options.generateHtml = detectHtmlOutputType(options.format)
  47. if options.metric not in metrix_table:
  48. options.metric = "gmean"
  49. if options.metric.endswith("%") or options.metric.endswith("$"):
  50. options.calc_relatives = False
  51. options.calc_cr = False
  52. if options.columns:
  53. options.columns = [s.strip().replace("\\n", "\n") for s in options.columns.split(",")]
  54. if options.regressions:
  55. assert not options.progress_mode, 'unsupported mode'
  56. def parseRegressionColumn(s):
  57. """ Format: '[r][c]<uint>-<uint>' """
  58. reverse = s.startswith('r')
  59. if reverse:
  60. s = s[1:]
  61. addColor = s.startswith('c')
  62. if addColor:
  63. s = s[1:]
  64. parts = s.split('-', 1)
  65. link = (int(parts[0]), int(parts[1]), reverse, addColor)
  66. assert link[0] != link[1]
  67. return link
  68. options.regressions = [parseRegressionColumn(s) for s in options.regressions.split(',')]
  69. show_units = options.units if options.show_units else None
  70. # expand wildcards and filter duplicates
  71. files = []
  72. seen = set()
  73. for arg in args:
  74. if ("*" in arg) or ("?" in arg):
  75. flist = [os.path.abspath(f) for f in glob.glob(arg)]
  76. flist = sorted(flist, key= lambda text: str(text).replace("M", "_"))
  77. files.extend([ x for x in flist if x not in seen and not seen.add(x)])
  78. else:
  79. fname = os.path.abspath(arg)
  80. if fname not in seen and not seen.add(fname):
  81. files.append(fname)
  82. # read all passed files
  83. test_sets = []
  84. for arg in files:
  85. try:
  86. tests = testlog_parser.parseLogFile(arg)
  87. if options.filter:
  88. expr = re.compile(options.filter)
  89. tests = [t for t in tests if expr.search(str(t))]
  90. if options.match:
  91. tests = [t for t in tests if t.get("status") != "notrun"]
  92. if tests:
  93. test_sets.append((os.path.basename(arg), tests))
  94. except IOError as err:
  95. sys.stderr.write("IOError reading \"" + arg + "\" - " + str(err) + os.linesep)
  96. except xml.parsers.expat.ExpatError as err:
  97. sys.stderr.write("ExpatError reading \"" + arg + "\" - " + str(err) + os.linesep)
  98. if not test_sets:
  99. sys.stderr.write("Error: no test data found" + os.linesep)
  100. quit()
  101. setsCount = len(test_sets)
  102. if options.regressions is None:
  103. reference = -1 if options.progress_mode else 0
  104. options.regressions = [(i, reference, False, True) for i in range(1, len(test_sets))]
  105. for link in options.regressions:
  106. (i, ref, reverse, addColor) = link
  107. assert i >= 0 and i < setsCount
  108. assert ref < setsCount
  109. # find matches
  110. test_cases = {}
  111. name_extractor = lambda name: str(name)
  112. if options.match:
  113. reg = re.compile(options.match)
  114. name_extractor = lambda name: reg.sub(options.match_replace, str(name))
  115. for i in range(setsCount):
  116. for case in test_sets[i][1]:
  117. name = name_extractor(case)
  118. if options.module:
  119. name = options.module + "::" + name
  120. if name not in test_cases:
  121. test_cases[name] = [None] * setsCount
  122. test_cases[name][i] = case
  123. # build table
  124. getter = metrix_table[options.metric][1]
  125. getter_score = metrix_table["score"][1] if options.calc_score else None
  126. getter_p = metrix_table[options.metric + "%"][1] if options.calc_relatives else None
  127. getter_cr = metrix_table[options.metric + "$"][1] if options.calc_cr else None
  128. tbl = table('%s (%s)' % (metrix_table[options.metric][0], options.units), options.format)
  129. # header
  130. tbl.newColumn("name", "Name of Test", align = "left", cssclass = "col_name")
  131. for i in range(setsCount):
  132. tbl.newColumn(str(i), getSetName(test_sets[i], i, options.columns, False), align = "center")
  133. def addHeaderColumns(suffix, description, cssclass):
  134. for link in options.regressions:
  135. (i, ref, reverse, addColor) = link
  136. if reverse:
  137. i, ref = ref, i
  138. current_set = test_sets[i]
  139. current = getSetName(current_set, i, options.columns)
  140. if ref >= 0:
  141. reference_set = test_sets[ref]
  142. reference = getSetName(reference_set, ref, options.columns)
  143. else:
  144. reference = 'previous'
  145. tbl.newColumn(str(i) + '-' + str(ref) + suffix, '%s\nvs\n%s\n(%s)' % (current, reference, description), align='center', cssclass=cssclass)
  146. if options.calc_cr:
  147. addHeaderColumns(suffix='$', description='cycles reduction', cssclass='col_cr')
  148. if options.calc_relatives:
  149. addHeaderColumns(suffix='%', description='x-factor', cssclass='col_rel')
  150. if options.calc_score:
  151. addHeaderColumns(suffix='S', description='score', cssclass='col_name')
  152. # rows
  153. prevGroupName = None
  154. needNewRow = True
  155. lastRow = None
  156. for name in sorted(test_cases.keys(), key=alphanum_keyselector):
  157. cases = test_cases[name]
  158. if needNewRow:
  159. lastRow = tbl.newRow()
  160. if not options.showall:
  161. needNewRow = False
  162. tbl.newCell("name", name)
  163. groupName = next(c for c in cases if c).shortName()
  164. if groupName != prevGroupName:
  165. prop = lastRow.props.get("cssclass", "")
  166. if "firstingroup" not in prop:
  167. lastRow.props["cssclass"] = prop + " firstingroup"
  168. prevGroupName = groupName
  169. for i in range(setsCount):
  170. case = cases[i]
  171. if case is None:
  172. if options.intersect_logs:
  173. needNewRow = False
  174. break
  175. tbl.newCell(str(i), "-")
  176. else:
  177. status = case.get("status")
  178. if status != "run":
  179. tbl.newCell(str(i), status, color="red")
  180. else:
  181. val = getter(case, cases[0], options.units)
  182. if val:
  183. needNewRow = True
  184. tbl.newCell(str(i), formatValue(val, options.metric, show_units), val)
  185. if needNewRow:
  186. for link in options.regressions:
  187. (i, reference, reverse, addColor) = link
  188. if reverse:
  189. i, reference = reference, i
  190. tblCellID = str(i) + '-' + str(reference)
  191. case = cases[i]
  192. if case is None:
  193. if options.calc_relatives:
  194. tbl.newCell(tblCellID + "%", "-")
  195. if options.calc_cr:
  196. tbl.newCell(tblCellID + "$", "-")
  197. if options.calc_score:
  198. tbl.newCell(tblCellID + "$", "-")
  199. else:
  200. status = case.get("status")
  201. if status != "run":
  202. tbl.newCell(str(i), status, color="red")
  203. if status != "notrun":
  204. needNewRow = True
  205. if options.calc_relatives:
  206. tbl.newCell(tblCellID + "%", "-", color="red")
  207. if options.calc_cr:
  208. tbl.newCell(tblCellID + "$", "-", color="red")
  209. if options.calc_score:
  210. tbl.newCell(tblCellID + "S", "-", color="red")
  211. else:
  212. val = getter(case, cases[0], options.units)
  213. def getRegression(fn):
  214. if fn and val:
  215. for j in reversed(range(i)) if reference < 0 else [reference]:
  216. r = cases[j]
  217. if r is not None and r.get("status") == 'run':
  218. return fn(case, r, options.units)
  219. valp = getRegression(getter_p) if options.calc_relatives or options.progress_mode else None
  220. valcr = getRegression(getter_cr) if options.calc_cr else None
  221. val_score = getRegression(getter_score) if options.calc_score else None
  222. if not valp:
  223. color = None
  224. elif valp > 1.05:
  225. color = 'green'
  226. elif valp < 0.95:
  227. color = 'red'
  228. else:
  229. color = None
  230. if addColor:
  231. if not reverse:
  232. tbl.newCell(str(i), formatValue(val, options.metric, show_units), val, color=color)
  233. else:
  234. r = cases[reference]
  235. if r is not None and r.get("status") == 'run':
  236. val = getter(r, cases[0], options.units)
  237. tbl.newCell(str(reference), formatValue(val, options.metric, show_units), val, color=color)
  238. if options.calc_relatives:
  239. tbl.newCell(tblCellID + "%", formatValue(valp, "%"), valp, color=color, bold=color)
  240. if options.calc_cr:
  241. tbl.newCell(tblCellID + "$", formatValue(valcr, "$"), valcr, color=color, bold=color)
  242. if options.calc_score:
  243. tbl.newCell(tblCellID + "S", formatValue(val_score, "S"), val_score, color = color, bold = color)
  244. if not needNewRow:
  245. tbl.trimLastRow()
  246. if options.regressionsOnly:
  247. for r in reversed(range(len(tbl.rows))):
  248. for i in range(1, len(options.regressions) + 1):
  249. val = tbl.rows[r].cells[len(tbl.rows[r].cells) - i].value
  250. if val is not None and val < float(options.regressionsOnly):
  251. break
  252. else:
  253. tbl.rows.pop(r)
  254. # output table
  255. if options.generateHtml:
  256. if options.format == "moinwiki":
  257. tbl.htmlPrintTable(sys.stdout, True)
  258. else:
  259. htmlPrintHeader(sys.stdout, "Summary report for %s tests from %s test logs" % (len(test_cases), setsCount))
  260. tbl.htmlPrintTable(sys.stdout)
  261. htmlPrintFooter(sys.stdout)
  262. else:
  263. tbl.consolePrintTable(sys.stdout)
  264. if options.regressionsOnly:
  265. sys.exit(len(tbl.rows))