[NFC][lit] Extract 'test time' reading/writing into standalone functions
authorRoman Lebedev <lebedev.ri@gmail.com>
Mon, 22 Mar 2021 09:05:04 +0000 (12:05 +0300)
committerRoman Lebedev <lebedev.ri@gmail.com>
Mon, 22 Mar 2021 12:25:32 +0000 (15:25 +0300)
Simply refactor code into reusable functions,
to allow read_test_times() to be reused later.

llvm/utils/lit/lit/Test.py
llvm/utils/lit/lit/TestTimes.py [new file with mode: 0644]
llvm/utils/lit/lit/main.py

index ca71573..7cc610b 100644 (file)
@@ -3,6 +3,7 @@ import os
 from json import JSONEncoder
 
 from lit.BooleanExpression import BooleanExpression
+from lit.TestTimes import read_test_times
 
 # Test result codes.
 
@@ -207,15 +208,7 @@ class TestSuite:
         # The test suite configuration.
         self.config = config
 
-        self.test_times = {}
-        test_times_file = os.path.join(exec_root, '.lit_test_times.txt')
-        if not os.path.exists(test_times_file):
-            test_times_file = os.path.join(source_root, '.lit_test_times.txt')
-        if os.path.exists(test_times_file):
-            with open(test_times_file, 'r') as time_file:
-                for line in time_file:
-                    time, path = line.split(maxsplit=1)
-                    self.test_times[path.strip('\n')] = float(time)
+        self.test_times = read_test_times(self)
 
     def getSourcePath(self, components):
         return os.path.join(self.source_root, *components)
diff --git a/llvm/utils/lit/lit/TestTimes.py b/llvm/utils/lit/lit/TestTimes.py
new file mode 100644 (file)
index 0000000..fe01c3f
--- /dev/null
@@ -0,0 +1,41 @@
+import os
+
+
+def read_test_times(suite):
+    test_times = {}
+    test_times_file = os.path.join(suite.exec_root, '.lit_test_times.txt')
+    if not os.path.exists(test_times_file):
+        test_times_file = os.path.join(
+            suite.source_root, '.lit_test_times.txt')
+    if os.path.exists(test_times_file):
+        with open(test_times_file, 'r') as time_file:
+            for line in time_file:
+                time, path = line.split(maxsplit=1)
+                test_times[path.strip('\n')] = float(time)
+    return test_times
+
+
+def record_test_times(tests, lit_config):
+    times_by_suite = {}
+    for t in tests:
+        if not t.result.elapsed:
+            continue
+        if not t.suite.exec_root in times_by_suite:
+            times_by_suite[t.suite.exec_root] = []
+        time = -t.result.elapsed if t.isFailure() else t.result.elapsed
+        # The "path" here is only used as a key into a dictionary. It is never
+        # used as an actual path to a filesystem API, therefore we use '/' as
+        # the canonical separator so that Unix and Windows machines can share
+        # timing data.
+        times_by_suite[t.suite.exec_root].append(('/'.join(t.path_in_suite),
+                                                  t.result.elapsed))
+
+    for s, value in times_by_suite.items():
+        try:
+            path = os.path.join(s, '.lit_test_times.txt')
+            with open(path, 'w') as time_file:
+                for name, time in value:
+                    time_file.write(("%e" % time) + ' ' + name + '\n')
+        except:
+            lit_config.warning('Could not save test time: ' + path)
+            continue
index 70a3111..e4c3a34 100755 (executable)
@@ -18,6 +18,7 @@ import lit.reports
 import lit.run
 import lit.Test
 import lit.util
+from lit.TestTimes import record_test_times
 
 
 def main(builtin_params={}):
@@ -256,32 +257,6 @@ def execute_in_tmp_dir(run, lit_config):
                 lit_config.warning("Failed to delete temp directory '%s', try upgrading your version of Python to fix this" % tmp_dir)
 
 
-def record_test_times(tests, lit_config):
-    times_by_suite = {}
-    for t in tests:
-        if not t.result.elapsed:
-            continue
-        if not t.suite.exec_root in times_by_suite:
-            times_by_suite[t.suite.exec_root] = []
-        time = -t.result.elapsed if t.isFailure() else t.result.elapsed
-        # The "path" here is only used as a key into a dictionary. It is never
-        # used as an actual path to a filesystem API, therefore we use '/' as
-        # the canonical separator so that Unix and Windows machines can share
-        # timing data.
-        times_by_suite[t.suite.exec_root].append(('/'.join(t.path_in_suite),
-          t.result.elapsed))
-
-    for s, value in times_by_suite.items():
-        try:
-            path = os.path.join(s, '.lit_test_times.txt')
-            with open(path, 'w') as time_file:
-                for name, time in value:
-                    time_file.write(("%e" % time) + ' ' + name + '\n')
-        except:
-            lit_config.warning('Could not save test time: ' + path)
-            continue
-
-
 def print_histogram(tests):
     test_times = [(t.getFullName(), t.result.elapsed)
                   for t in tests if t.result.elapsed]