chart.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #!/usr/bin/env python
  2. """ OpenCV performance test results charts generator.
  3. This script formats results of a performance test as a table or a series of tables according to test
  4. parameters.
  5. ### Description
  6. Performance data is stored in the GTest log file created by performance tests. Default name is
  7. `test_details.xml`. It can be changed with the `--gtest_output=xml:<location>/<filename>.xml` test
  8. option. See https://github.com/opencv/opencv/wiki/HowToUsePerfTests for more details.
  9. Script accepts an XML with performance test results as an input. Only one test (aka testsuite)
  10. containing multiple cases (aka testcase) with different parameters can be used. Test should have 2
  11. or more parameters, for example resolution (640x480), data type (8UC1), mode (NORM_TYPE), etc.
  12. Parameters #2 and #1 will be used as table row and column by default, this mapping can be changed
  13. with `-x` and `-y` options. Parameter combination besides the two selected for row and column will
  14. be represented as a separate table. I.e. one table (RES x TYPE) for `NORM_L1`, another for
  15. `NORM_L2`, etc.
  16. Test can be selected either by using `--gtest_filter` option when running the test, or by using the
  17. `--filter` script option.
  18. ### Options:
  19. -f REGEX, --filter=REGEX - regular expression used to select a test
  20. -x ROW, -y COL - choose different parameters for rows and columns
  21. -u UNITS, --units=UNITS - units for output values (s, ms (default), us, ns or ticks)
  22. -m NAME, --metric=NAME - output metric (mean, median, stddev, etc.)
  23. -o FMT, --output=FMT - output format ('txt', 'html' or 'auto')
  24. ### Example:
  25. ./chart.py -f sum opencv_perf_core.xml
  26. Geometric mean for
  27. sum::Size_MatType::(Y, X)
  28. X\Y 127x61 640x480 1280x720 1920x1080
  29. 8UC1 0.03 ms 1.21 ms 3.61 ms 8.11 ms
  30. 8UC4 0.10 ms 3.56 ms 10.67 ms 23.90 ms
  31. 32FC1 0.05 ms 1.77 ms 5.23 ms 11.72 ms
  32. """
  33. from __future__ import print_function
  34. import testlog_parser, sys, os, xml, re
  35. from table_formatter import *
  36. from optparse import OptionParser
  37. cvsize_re = re.compile("^\d+x\d+$")
  38. cvtype_re = re.compile("^(CV_)(8U|8S|16U|16S|32S|32F|64F)(C\d{1,3})?$")
  39. def keyselector(a):
  40. if cvsize_re.match(a):
  41. size = [int(d) for d in a.split('x')]
  42. return size[0] * size[1]
  43. elif cvtype_re.match(a):
  44. if a.startswith("CV_"):
  45. a = a[3:]
  46. depth = 7
  47. if a[0] == '8':
  48. depth = (0, 1) [a[1] == 'S']
  49. elif a[0] == '1':
  50. depth = (2, 3) [a[2] == 'S']
  51. elif a[2] == 'S':
  52. depth = 4
  53. elif a[0] == '3':
  54. depth = 5
  55. elif a[0] == '6':
  56. depth = 6
  57. cidx = a.find('C')
  58. if cidx < 0:
  59. channels = 1
  60. else:
  61. channels = int(a[a.index('C') + 1:])
  62. #return (depth & 7) + ((channels - 1) << 3)
  63. return ((channels-1) & 511) + (depth << 9)
  64. return a
  65. convert = lambda text: int(text) if text.isdigit() else text
  66. alphanum_keyselector = lambda key: [ convert(c) for c in re.split('([0-9]+)', str(keyselector(key))) ]
  67. def getValueParams(test):
  68. param = test.get("value_param")
  69. if not param:
  70. return []
  71. if param.startswith("("):
  72. param = param[1:]
  73. if param.endswith(")"):
  74. param = param[:-1]
  75. args = []
  76. prev_pos = 0
  77. start = 0
  78. balance = 0
  79. while True:
  80. idx = param.find(",", prev_pos)
  81. if idx < 0:
  82. break
  83. idxlb = param.find("(", prev_pos, idx)
  84. while idxlb >= 0:
  85. balance += 1
  86. idxlb = param.find("(", idxlb+1, idx)
  87. idxrb = param.find(")", prev_pos, idx)
  88. while idxrb >= 0:
  89. balance -= 1
  90. idxrb = param.find(")", idxrb+1, idx)
  91. assert(balance >= 0)
  92. if balance == 0:
  93. args.append(param[start:idx].strip())
  94. start = idx + 1
  95. prev_pos = idx + 1
  96. args.append(param[start:].strip())
  97. return args
  98. #return [p.strip() for p in param.split(",")]
  99. def nextPermutation(indexes, lists, x, y):
  100. idx = len(indexes)-1
  101. while idx >= 0:
  102. while idx == x or idx == y:
  103. idx -= 1
  104. if idx < 0:
  105. return False
  106. v = indexes[idx] + 1
  107. if v < len(lists[idx]):
  108. indexes[idx] = v;
  109. return True;
  110. else:
  111. indexes[idx] = 0;
  112. idx -= 1
  113. return False
  114. def getTestWideName(sname, indexes, lists, x, y):
  115. name = sname + "::("
  116. for i in range(len(indexes)):
  117. if i > 0:
  118. name += ", "
  119. if i == x:
  120. name += "X"
  121. elif i == y:
  122. name += "Y"
  123. else:
  124. name += lists[i][indexes[i]]
  125. return str(name + ")")
  126. def getTest(stests, x, y, row, col):
  127. for pair in stests:
  128. if pair[1][x] == row and pair[1][y] == col:
  129. return pair[0]
  130. return None
  131. if __name__ == "__main__":
  132. parser = OptionParser()
  133. parser.add_option("-o", "--output", dest="format", help="output results in text format (can be 'txt', 'html' or 'auto' - default)", metavar="FMT", default="auto")
  134. parser.add_option("-u", "--units", dest="units", help="units for output values (s, ms (default), us, ns or ticks)", metavar="UNITS", default="ms")
  135. parser.add_option("-m", "--metric", dest="metric", help="output metric", metavar="NAME", default="gmean")
  136. parser.add_option("-x", "", dest="x", help="argument number for rows", metavar="ROW", default=1)
  137. parser.add_option("-y", "", dest="y", help="argument number for columns", metavar="COL", default=0)
  138. parser.add_option("-f", "--filter", dest="filter", help="regex to filter tests", metavar="REGEX", default=None)
  139. (options, args) = parser.parse_args()
  140. if len(args) != 1:
  141. print("Usage:\n", os.path.basename(sys.argv[0]), "<log_name1>.xml", file=sys.stderr)
  142. exit(1)
  143. options.generateHtml = detectHtmlOutputType(options.format)
  144. if options.metric not in metrix_table:
  145. options.metric = "gmean"
  146. if options.metric.endswith("%"):
  147. options.metric = options.metric[:-1]
  148. getter = metrix_table[options.metric][1]
  149. tests = testlog_parser.parseLogFile(args[0])
  150. if options.filter:
  151. expr = re.compile(options.filter)
  152. tests = [(t,getValueParams(t)) for t in tests if expr.search(str(t))]
  153. else:
  154. tests = [(t,getValueParams(t)) for t in tests]
  155. args[0] = os.path.basename(args[0])
  156. if not tests:
  157. print("Error - no tests matched", file=sys.stderr)
  158. exit(1)
  159. argsnum = len(tests[0][1])
  160. sname = tests[0][0].shortName()
  161. arglists = []
  162. for i in range(argsnum):
  163. arglists.append({})
  164. names = set()
  165. names1 = set()
  166. for pair in tests:
  167. sn = pair[0].shortName()
  168. if len(pair[1]) > 1:
  169. names.add(sn)
  170. else:
  171. names1.add(sn)
  172. if sn == sname:
  173. if len(pair[1]) != argsnum:
  174. print("Error - unable to create chart tables for functions having different argument numbers", file=sys.stderr)
  175. sys.exit(1)
  176. for i in range(argsnum):
  177. arglists[i][pair[1][i]] = 1
  178. if names1 or len(names) != 1:
  179. print("Error - unable to create tables for functions from different test suits:", file=sys.stderr)
  180. i = 1
  181. for name in sorted(names):
  182. print("%4s: %s" % (i, name), file=sys.stderr)
  183. i += 1
  184. if names1:
  185. print("Other suits in this log (can not be chosen):", file=sys.stderr)
  186. for name in sorted(names1):
  187. print("%4s: %s" % (i, name), file=sys.stderr)
  188. i += 1
  189. sys.exit(1)
  190. if argsnum < 2:
  191. print("Error - tests from %s have less than 2 parameters" % sname, file=sys.stderr)
  192. exit(1)
  193. for i in range(argsnum):
  194. arglists[i] = sorted([str(key) for key in arglists[i].keys()], key=alphanum_keyselector)
  195. if options.generateHtml and options.format != "moinwiki":
  196. htmlPrintHeader(sys.stdout, "Report %s for %s" % (args[0], sname))
  197. indexes = [0] * argsnum
  198. x = int(options.x)
  199. y = int(options.y)
  200. if x == y or x < 0 or y < 0 or x >= argsnum or y >= argsnum:
  201. x = 1
  202. y = 0
  203. while True:
  204. stests = []
  205. for pair in tests:
  206. t = pair[0]
  207. v = pair[1]
  208. for i in range(argsnum):
  209. if i != x and i != y:
  210. if v[i] != arglists[i][indexes[i]]:
  211. t = None
  212. break
  213. if t:
  214. stests.append(pair)
  215. tbl = table(metrix_table[options.metric][0] + " for\n" + getTestWideName(sname, indexes, arglists, x, y))
  216. tbl.newColumn("x", "X\Y")
  217. for col in arglists[y]:
  218. tbl.newColumn(col, col, align="center")
  219. for row in arglists[x]:
  220. tbl.newRow()
  221. tbl.newCell("x", row)
  222. for col in arglists[y]:
  223. case = getTest(stests, x, y, row, col)
  224. if case:
  225. status = case.get("status")
  226. if status != "run":
  227. tbl.newCell(col, status, color = "red")
  228. else:
  229. val = getter(case, None, options.units)
  230. if isinstance(val, float):
  231. tbl.newCell(col, "%.2f %s" % (val, options.units), val)
  232. else:
  233. tbl.newCell(col, val, val)
  234. else:
  235. tbl.newCell(col, "-")
  236. if options.generateHtml:
  237. tbl.htmlPrintTable(sys.stdout, options.format == "moinwiki")
  238. else:
  239. tbl.consolePrintTable(sys.stdout)
  240. if not nextPermutation(indexes, arglists, x, y):
  241. break
  242. if options.generateHtml and options.format != "moinwiki":
  243. htmlPrintFooter(sys.stdout)