xref: /llvm-project/llvm/utils/lit/lit/TestTimes.py (revision b71edfaa4ec3c998aadb35255ce2f60bba2940b0)
1import os
2
3
4def read_test_times(suite):
5    test_times = {}
6    test_times_file = os.path.join(suite.exec_root, ".lit_test_times.txt")
7    if not os.path.exists(test_times_file):
8        test_times_file = os.path.join(suite.source_root, ".lit_test_times.txt")
9    if os.path.exists(test_times_file):
10        with open(test_times_file, "r") as time_file:
11            for line in time_file:
12                time, path = line.split(maxsplit=1)
13                test_times[path.strip("\n")] = float(time)
14    return test_times
15
16
17def record_test_times(tests, lit_config):
18    times_by_suite = {}
19    for t in tests:
20        assert t.suite.test_times is None
21        if t.result.elapsed is None:
22            continue
23        if not t.suite.exec_root in times_by_suite:
24            times_by_suite[t.suite.exec_root] = read_test_times(t.suite)
25        time = -t.result.elapsed if t.isFailure() else t.result.elapsed
26        # The "path" here is only used as a key into a dictionary. It is never
27        # used as an actual path to a filesystem API, therefore we use '/' as
28        # the canonical separator so that Unix and Windows machines can share
29        # timing data.
30        times_by_suite[t.suite.exec_root]["/".join(t.path_in_suite)] = time
31
32    for s, value in times_by_suite.items():
33        try:
34            path = os.path.join(s, ".lit_test_times.txt")
35            with open(path, "w") as time_file:
36                for name, time in value.items():
37                    time_file.write(("%e" % time) + " " + name + "\n")
38        except:
39            lit_config.warning("Could not save test time: " + path)
40            continue
41