report.py 3.8 KB

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