Debian Codegraph @

tests/data/extract_tests_durations.py

raw
1 #!/usr/bin/env python3
2
3 # Script which takes log files from jenkins' build, and returns a list
4 # of \n-separated `testname`=`duration of that test on that build`.
5 #
6 # ********************************* USAGE *******************************
7 # ./extract_test_durations.py TestSuite PathToLog [PathToLog ...]
8 # TestSuite - name of test_suite, required to be one of options defined
9 # a few lines below (case insensitive)
10 # PathToLog - if there are multiple, script will output average durations
11 # on those multiple builds.
12
13 import os
14 import re
15 import sys
16 from enum import Enum
17 from typing import Dict, List, TextIO, Tuple
18
19
20 SANITY_NAMES = ["SanityChecks", "Sanity"]
21 SHORT_NAMES = ["ShortSystemTests", "Shorty", "ShortTests", "Short", "Shorts"]
22 LONG_NAMES = ["LongSystemTests", "Longi", "Long", "LongTests"]
23
24
25 class TestSuite(Enum):
26 SANITY = SANITY_NAMES[0]
27 SHORT = SHORT_NAMES[0]
28 LONG = LONG_NAMES[0]
29
30
31 def create_test_suite_enum(test_suite_arg: str) -> TestSuite:
32 for n in SANITY_NAMES:
33 if test_suite_arg.lower() == n.lower():
34 return TestSuite.SANITY
35 for n in SHORT_NAMES:
36 if test_suite_arg.lower() == n.lower():
37 return TestSuite.SHORT
38 for n in LONG_NAMES:
39 if test_suite_arg.lower() == n.lower():
40 return TestSuite.LONG
41
42 raise Exception("Wrong test_suite argument. Try sanity/shorts/longs.")
43
44
45 def process_one_build(path_to_log: str, test_suite: TestSuite) -> List[Tuple[str, int]]:
46 """If test_suite == Sanity: just processes one given log
47 Else: process multiple logs from ConcurrentJobs"""
48 tests = []
49 with open(path_to_log, "r") as log:
50 if test_suite == TestSuite.SANITY:
51 tests = parse_logfile_for_tests(log, test_suite)
52 else:
53 subjobs = get_subjobs_from_mainjob_log(log)
54 paths_to_logs = [
55 convert_to_correct_subjob_path(sj[0], sj[1], path_to_log)
56 for sj in subjobs
57 ]
58 for p in paths_to_logs:
59 try:
60 with open(p, "r") as log:
61 tests.extend(parse_logfile_for_tests(log, test_suite))
62 except FileNotFoundError:
63 print(
64 "Error: log for subjob build {} not found (main_job log: {}). Not counting this build at all.".format(
65 p, path_to_log
66 ),
67 file=sys.stderr,
68 )
69 return []
70 if len(tests) == 0:
71 print("Error: Empty or wrong logfile: {}".format(path_to_log), file=sys.stderr)
72 return tests
73
74
75 def parse_logfile_for_tests(
76 log_file: TextIO, test_type: TestSuite
77 ) -> List[Tuple[str, int]]:
78 result = []
79 pattern = test_type.value + "\.(\w+) \((\d+)"
80 for line in log_file:
81 match = re.search(pattern, line)
82 if match:
83 test_name = match.group(1)
84 test_time = int(match.group(2))
85 result.append((test_name, test_time))
86 return result
87
88
89 def get_subjobs_from_mainjob_log(log: TextIO) -> List[Tuple[str, str]]:
90 JOB_NAME = "saunafs.ConcurrentJob"
91 concurrent_job_re = re.compile(
92 "Finished Build.*#(\d+).*{}.*with status".format(JOB_NAME)
93 )
94 concurrent_jobs = []
95 for line in log:
96 m = concurrent_job_re.search(line)
97 if not m:
98 continue
99 concurrent_jobs.append((JOB_NAME, m.group(1)))
100 return concurrent_jobs
101
102
103 def convert_to_correct_subjob_path(
104 job_name: str, build_nr: str, path_to_mainjob_log: str
105 ) -> str:
106 return (
107 os.path.dirname(path_to_mainjob_log)
108 + "/../../../"
109 + job_name
110 + "/builds/"
111 + build_nr
112 + "/log"
113 )
114
115
116 def print_result(tests: Dict[str, List[int]]) -> None:
117 for name in sorted(tests):
118 durations = tests[name]
119 avg_duration = sum(durations) / len(durations)
120 avg_duration /= 1000
121 avg_duration -= avg_duration % 10
122 if avg_duration == 0:
123 avg_duration = 5
124 print("{}={}".format(name, int(avg_duration)))
125
126
127 def print_help_message() -> None:
128 print(
129 "*********************** USAGE ***********************\n"
130 + "./extract_test_durations.py TestSuite PathToLog [PathToLog ...],\n"
131 + "e.g. ./extract_test_durations.py sanity 123456/log 123457/log",
132 file=sys.stderr,
133 )
134
135
136 def main() -> None:
137 if len(sys.argv) < 3:
138 print("Wrong number of arguments.", file=sys.stderr)
139 print_help_message()
140 return
141
142 test_suite_name = sys.argv[1]
143 test_suite = create_test_suite_enum(test_suite_name)
144 paths_to_logs = sys.argv[2:]
145
146 tests = {}
147 for path in paths_to_logs:
148 cur_tests = process_one_build(path, test_suite)
149 for name, duration in cur_tests:
150 if name not in tests:
151 tests[name] = [duration]
152 else:
153 tests[name].append(duration)
154 print_result(tests)
155
156
157 if __name__ == "__main__":
158 main()