OSDN Git Service

Refactor xunit test case builder to not use as much str addition
authorChris Matthews <cmatthews5@apple.com>
Fri, 11 May 2018 00:25:42 +0000 (00:25 +0000)
committerChris Matthews <cmatthews5@apple.com>
Fri, 11 May 2018 00:25:42 +0000 (00:25 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@332064 91177308-0d34-0410-b5e6-96231b3b80d8

utils/lit/lit/Test.py
utils/lit/lit/main.py

index d87906c..c68b6d9 100644 (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("/>")
index 6c16737..d20b881 100755 (executable)
@@ -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()