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