Refactor xunit test case builder to not use as much str addition

String concatenation in python is slow.  Refactor to not concatenate the
possibly large strings of test output and instead write them directly
to the output file.

llvm-svn: 332064
This commit is contained in:
Chris Matthews 2018-05-11 00:25:42 +00:00
parent a119322c1d
commit 5f74591847
2 changed files with 12 additions and 11 deletions

View File

@ -360,7 +360,8 @@ class Test:
"""
return self.suite.config.is_early
def getJUnitXML(self):
def writeJUnitXML(self, fil):
"""Write the test's report xml representation to a file handle."""
test_name = escape(self.path_in_suite[-1])
test_path = self.path_in_suite[:-1]
safe_test_path = [x.replace(".","_") for x in test_path]
@ -370,14 +371,13 @@ class Test:
class_name = safe_name + "." + "/".join(safe_test_path)
else:
class_name = safe_name + "." + safe_name
xml = "<testcase classname='" + class_name + "' name='" + \
test_name + "'"
xml += " time='{:.2f}'".format(
self.result.elapsed if self.result.elapsed is not None else 0.0)
testcase_template = u"<testcase classname='{class_name}' name='{test_name}' time='{time:.2f}'"
elapsed_time = self.result.elapsed if self.result.elapsed is not None else 0.0
testcase_xml = testcase_template.format(class_name=class_name, test_name=test_name, time=elapsed_time)
fil.write(testcase_xml)
if self.result.code.isFailure:
xml += ">\n\t<failure >\n" + escape(self.result.output)
xml += "\n\t</failure>\n</testcase>"
fil.write(">\n\t<failure >\n")
fil.write(escape(self.result.output))
fil.write("\n\t</failure>\n</testcase>")
else:
xml += "/>"
return xml
fil.write("/>")

View File

@ -614,7 +614,8 @@ def main_with_tmp(builtinParameters):
xunit_output_file.write(" failures='" + str(suite['failures']) +
"'>\n")
for result_test in suite['tests']:
xunit_output_file.write(result_test.getJUnitXML() + "\n")
result_test.writeJUnitXML(xunit_output_file)
xunit_output_file.write("\n")
xunit_output_file.write("</testsuite>\n")
xunit_output_file.write("</testsuites>")
xunit_output_file.close()