perf_tests_timing.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. from operator import itemgetter, attrgetter
  7. from summary import getSetName, alphanum_keyselector
  8. import re
  9. if __name__ == "__main__":
  10. usage = "%prog <log_name>.xml [...]"
  11. parser = OptionParser(usage = usage)
  12. parser.add_option("-o", "--output", dest = "format",
  13. help = "output results in text format (can be 'txt', 'html' or 'auto' - default)",
  14. metavar = 'FMT', default = 'auto')
  15. parser.add_option("--failed-only", action = "store_true", dest = "failedOnly",
  16. help = "print only failed tests", default = False)
  17. (options, args) = parser.parse_args()
  18. options.generateHtml = detectHtmlOutputType(options.format)
  19. files = []
  20. testsuits = [] # testsuit module, name, time, num, flag for failed tests
  21. overall_time = 0
  22. seen = set()
  23. for arg in args:
  24. if ("*" in arg) or ("?" in arg):
  25. flist = [os.path.abspath(f) for f in glob.glob(arg)]
  26. flist = sorted(flist, key= lambda text: str(text).replace("M", "_"))
  27. files.extend([ x for x in flist if x not in seen and not seen.add(x)])
  28. else:
  29. fname = os.path.abspath(arg)
  30. if fname not in seen and not seen.add(fname):
  31. files.append(fname)
  32. file = os.path.abspath(fname)
  33. if not os.path.isfile(file):
  34. sys.stderr.write("IOError reading \"" + file + "\" - " + str(err) + os.linesep)
  35. parser.print_help()
  36. exit(0)
  37. fname = os.path.basename(fname)
  38. find_module_name = re.search(r'([^_]*)', fname)
  39. module_name = find_module_name.group(0)
  40. test_sets = []
  41. try:
  42. tests = testlog_parser.parseLogFile(file)
  43. if tests:
  44. test_sets.append((os.path.basename(file), tests))
  45. except IOError as err:
  46. sys.stderr.write("IOError reading \"" + file + "\" - " + str(err) + os.linesep)
  47. except xml.parsers.expat.ExpatError as err:
  48. sys.stderr.write("ExpatError reading \"" + file + "\" - " + str(err) + os.linesep)
  49. if not test_sets:
  50. continue
  51. # find matches
  52. setsCount = len(test_sets)
  53. test_cases = {}
  54. name_extractor = lambda name: str(name)
  55. for i in range(setsCount):
  56. for case in test_sets[i][1]:
  57. name = name_extractor(case)
  58. if name not in test_cases:
  59. test_cases[name] = [None] * setsCount
  60. test_cases[name][i] = case
  61. prevGroupName = None
  62. suit_time = 0
  63. suit_num = 0
  64. fails_num = 0
  65. for name in sorted(test_cases.iterkeys(), key=alphanum_keyselector):
  66. cases = test_cases[name]
  67. groupName = next(c for c in cases if c).shortName()
  68. if groupName != prevGroupName:
  69. if prevGroupName != None:
  70. suit_time = suit_time/60 #from seconds to minutes
  71. testsuits.append({'module': module_name, 'name': prevGroupName, \
  72. 'time': suit_time, 'num': suit_num, 'failed': fails_num})
  73. overall_time += suit_time
  74. suit_time = 0
  75. suit_num = 0
  76. fails_num = 0
  77. prevGroupName = groupName
  78. for i in range(setsCount):
  79. case = cases[i]
  80. if not case is None:
  81. suit_num += 1
  82. if case.get('status') == 'run':
  83. suit_time += case.get('time')
  84. if case.get('status') == 'failed':
  85. fails_num += 1
  86. # last testsuit processing
  87. suit_time = suit_time/60
  88. testsuits.append({'module': module_name, 'name': prevGroupName, \
  89. 'time': suit_time, 'num': suit_num, 'failed': fails_num})
  90. overall_time += suit_time
  91. if len(testsuits)==0:
  92. exit(0)
  93. tbl = table()
  94. rows = 0
  95. if not options.failedOnly:
  96. tbl.newColumn('module', 'Module', align = 'left', cssclass = 'col_name')
  97. tbl.newColumn('name', 'Testsuit', align = 'left', cssclass = 'col_name')
  98. tbl.newColumn('time', 'Time (min)', align = 'center', cssclass = 'col_name')
  99. tbl.newColumn('num', 'Num of tests', align = 'center', cssclass = 'col_name')
  100. tbl.newColumn('failed', 'Failed', align = 'center', cssclass = 'col_name')
  101. # rows
  102. for suit in sorted(testsuits, key = lambda suit: suit['time'], reverse = True):
  103. tbl.newRow()
  104. tbl.newCell('module', suit['module'])
  105. tbl.newCell('name', suit['name'])
  106. tbl.newCell('time', formatValue(suit['time'], '', ''), suit['time'])
  107. tbl.newCell('num', suit['num'])
  108. if (suit['failed'] != 0):
  109. tbl.newCell('failed', suit['failed'])
  110. else:
  111. tbl.newCell('failed', ' ')
  112. rows += 1
  113. else:
  114. tbl.newColumn('module', 'Module', align = 'left', cssclass = 'col_name')
  115. tbl.newColumn('name', 'Testsuit', align = 'left', cssclass = 'col_name')
  116. tbl.newColumn('failed', 'Failed', align = 'center', cssclass = 'col_name')
  117. # rows
  118. for suit in sorted(testsuits, key = lambda suit: suit['time'], reverse = True):
  119. if (suit['failed'] != 0):
  120. tbl.newRow()
  121. tbl.newCell('module', suit['module'])
  122. tbl.newCell('name', suit['name'])
  123. tbl.newCell('failed', suit['failed'])
  124. rows += 1
  125. # output table
  126. if rows:
  127. if options.generateHtml:
  128. tbl.htmlPrintTable(sys.stdout)
  129. htmlPrintFooter(sys.stdout)
  130. else:
  131. if not options.failedOnly:
  132. print('\nOverall time: %.2f min\n' % overall_time)
  133. tbl.consolePrintTable(sys.stdout)
  134. print(2 * '\n')