testlog_parser.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import collections
  4. import re
  5. import os.path
  6. import sys
  7. from xml.dom.minidom import parse
  8. if sys.version_info > (3,):
  9. long = int
  10. def cmp(a, b): return (a>b)-(a<b)
  11. class TestInfo(object):
  12. def __init__(self, xmlnode):
  13. self.fixture = xmlnode.getAttribute("classname")
  14. self.name = xmlnode.getAttribute("name")
  15. self.value_param = xmlnode.getAttribute("value_param")
  16. self.type_param = xmlnode.getAttribute("type_param")
  17. custom_status = xmlnode.getAttribute("custom_status")
  18. failures = xmlnode.getElementsByTagName("failure")
  19. if len(custom_status) > 0:
  20. self.status = custom_status
  21. elif len(failures) > 0:
  22. self.status = "failed"
  23. else:
  24. self.status = xmlnode.getAttribute("status")
  25. if self.name.startswith("DISABLED_"):
  26. if self.status == 'notrun':
  27. self.status = "disabled"
  28. self.fixture = self.fixture.replace("DISABLED_", "")
  29. self.name = self.name.replace("DISABLED_", "")
  30. self.properties = {
  31. prop.getAttribute("name") : prop.getAttribute("value")
  32. for prop in xmlnode.getElementsByTagName("property")
  33. if prop.hasAttribute("name") and prop.hasAttribute("value")
  34. }
  35. self.metrix = {}
  36. self.parseLongMetric(xmlnode, "bytesIn");
  37. self.parseLongMetric(xmlnode, "bytesOut");
  38. self.parseIntMetric(xmlnode, "samples");
  39. self.parseIntMetric(xmlnode, "outliers");
  40. self.parseFloatMetric(xmlnode, "frequency", 1);
  41. self.parseLongMetric(xmlnode, "min");
  42. self.parseLongMetric(xmlnode, "median");
  43. self.parseLongMetric(xmlnode, "gmean");
  44. self.parseLongMetric(xmlnode, "mean");
  45. self.parseLongMetric(xmlnode, "stddev");
  46. self.parseFloatMetric(xmlnode, "gstddev");
  47. self.parseFloatMetric(xmlnode, "time");
  48. self.parseLongMetric(xmlnode, "total_memory_usage");
  49. def parseLongMetric(self, xmlnode, name, default = 0):
  50. if name in self.properties:
  51. self.metrix[name] = long(self.properties[name])
  52. elif xmlnode.hasAttribute(name):
  53. self.metrix[name] = long(xmlnode.getAttribute(name))
  54. else:
  55. self.metrix[name] = default
  56. def parseIntMetric(self, xmlnode, name, default = 0):
  57. if name in self.properties:
  58. self.metrix[name] = int(self.properties[name])
  59. elif xmlnode.hasAttribute(name):
  60. self.metrix[name] = int(xmlnode.getAttribute(name))
  61. else:
  62. self.metrix[name] = default
  63. def parseFloatMetric(self, xmlnode, name, default = 0):
  64. if name in self.properties:
  65. self.metrix[name] = float(self.properties[name])
  66. elif xmlnode.hasAttribute(name):
  67. self.metrix[name] = float(xmlnode.getAttribute(name))
  68. else:
  69. self.metrix[name] = default
  70. def parseStringMetric(self, xmlnode, name, default = None):
  71. if name in self.properties:
  72. self.metrix[name] = self.properties[name].strip()
  73. elif xmlnode.hasAttribute(name):
  74. self.metrix[name] = xmlnode.getAttribute(name).strip()
  75. else:
  76. self.metrix[name] = default
  77. def get(self, name, units="ms"):
  78. if name == "classname":
  79. return self.fixture
  80. if name == "name":
  81. return self.name
  82. if name == "fullname":
  83. return self.__str__()
  84. if name == "value_param":
  85. return self.value_param
  86. if name == "type_param":
  87. return self.type_param
  88. if name == "status":
  89. return self.status
  90. val = self.metrix.get(name, None)
  91. if not val:
  92. return val
  93. if name == "time":
  94. return self.metrix.get("time")
  95. if name in ["gmean", "min", "mean", "median", "stddev"]:
  96. scale = 1.0
  97. frequency = self.metrix.get("frequency", 1.0) or 1.0
  98. if units == "ms":
  99. scale = 1000.0
  100. if units == "us" or units == "mks": # mks is typo error for microsecond (<= OpenCV 3.4)
  101. scale = 1000000.0
  102. if units == "ns":
  103. scale = 1000000000.0
  104. if units == "ticks":
  105. frequency = long(1)
  106. scale = long(1)
  107. return val * scale / frequency
  108. return val
  109. def dump(self, units="ms"):
  110. print("%s ->\t\033[1;31m%s\033[0m = \t%.2f%s" % (str(self), self.status, self.get("gmean", units), units))
  111. def getName(self):
  112. pos = self.name.find("/")
  113. if pos > 0:
  114. return self.name[:pos]
  115. return self.name
  116. def getFixture(self):
  117. if self.fixture.endswith(self.getName()):
  118. fixture = self.fixture[:-len(self.getName())]
  119. else:
  120. fixture = self.fixture
  121. if fixture.endswith("_"):
  122. fixture = fixture[:-1]
  123. return fixture
  124. def param(self):
  125. return '::'.join(filter(None, [self.type_param, self.value_param]))
  126. def shortName(self):
  127. name = self.getName()
  128. fixture = self.getFixture()
  129. return '::'.join(filter(None, [name, fixture]))
  130. def __str__(self):
  131. name = self.getName()
  132. fixture = self.getFixture()
  133. return '::'.join(filter(None, [name, fixture, self.type_param, self.value_param]))
  134. def __cmp__(self, other):
  135. r = cmp(self.fixture, other.fixture);
  136. if r != 0:
  137. return r
  138. if self.type_param:
  139. if other.type_param:
  140. r = cmp(self.type_param, other.type_param);
  141. if r != 0:
  142. return r
  143. else:
  144. return -1
  145. else:
  146. if other.type_param:
  147. return 1
  148. if self.value_param:
  149. if other.value_param:
  150. r = cmp(self.value_param, other.value_param);
  151. if r != 0:
  152. return r
  153. else:
  154. return -1
  155. else:
  156. if other.value_param:
  157. return 1
  158. return 0
  159. def __lt__(self, other):
  160. return self.__cmp__(other) == -1
  161. # This is a Sequence for compatibility with old scripts,
  162. # which treat parseLogFile's return value as a list.
  163. class TestRunInfo(object):
  164. def __init__(self, properties, tests):
  165. self.properties = properties
  166. self.tests = tests
  167. def __len__(self):
  168. return len(self.tests)
  169. def __getitem__(self, key):
  170. return self.tests[key]
  171. def parseLogFile(filename):
  172. log = parse(filename)
  173. properties = {
  174. attr_name[3:]: attr_value
  175. for (attr_name, attr_value) in log.documentElement.attributes.items()
  176. if attr_name.startswith('cv_')
  177. }
  178. tests = list(map(TestInfo, log.getElementsByTagName("testcase")))
  179. return TestRunInfo(properties, tests)
  180. if __name__ == "__main__":
  181. if len(sys.argv) < 2:
  182. print("Usage:\n", os.path.basename(sys.argv[0]), "<log_name>.xml")
  183. exit(0)
  184. for arg in sys.argv[1:]:
  185. print("Processing {}...".format(arg))
  186. run = parseLogFile(arg)
  187. print("Properties:")
  188. for (prop_name, prop_value) in run.properties.items():
  189. print("\t{} = {}".format(prop_name, prop_value))
  190. print("Tests:")
  191. for t in sorted(run.tests):
  192. t.dump()
  193. print()