Skip to content

Commit f66f11f

Browse files
authored
Tests: run self check and lint in parallel (#8189)
I've seen this speed up `runtests.py` by about 20s when multiple cores are available.
1 parent 61da677 commit f66f11f

File tree

1 file changed

+64
-16
lines changed

1 file changed

+64
-16
lines changed

runtests.py

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/usr/bin/env python3
2+
import subprocess
3+
from subprocess import Popen
24
from os import system
35
from sys import argv, exit, platform, executable, version_info
46

5-
prog, *args = argv
6-
77

88
# Use the Python provided to execute the script, or fall back to a sane default
99
if version_info >= (3, 5, 0):
@@ -63,23 +63,71 @@
6363

6464
assert all(cmd in cmds for cmd in FAST_FAIL)
6565

66-
if not set(args).issubset(cmds):
67-
print("usage:", prog, " ".join('[%s]' % k for k in cmds))
68-
exit(1)
69-
70-
if not args:
71-
args = list(cmds)
72-
73-
status = 0
7466

75-
for arg in args:
76-
cmd = cmds[arg]
77-
print('run %s: %s' % (arg, cmd))
67+
def run_cmd(name: str) -> int:
68+
status = 0
69+
cmd = cmds[name]
70+
print('run %s: %s' % (name, cmd))
7871
res = (system(cmd) & 0x7F00) >> 8
7972
if res:
80-
print('\nFAILED: %s' % arg)
73+
print('\nFAILED: %s' % name)
8174
status = res
82-
if arg in FAST_FAIL:
75+
if name in FAST_FAIL:
8376
exit(status)
77+
return status
78+
79+
80+
def start_background_cmd(name: str) -> Popen:
81+
cmd = cmds[name]
82+
proc = subprocess.Popen(cmd,
83+
shell=True,
84+
stderr=subprocess.STDOUT,
85+
stdout=subprocess.PIPE)
86+
return proc
87+
88+
89+
def wait_background_cmd(name: str, proc: Popen) -> int:
90+
output = proc.communicate()[0]
91+
status = proc.returncode
92+
print('run %s: %s' % (name, cmds[name]))
93+
if status:
94+
print(output.decode().rstrip())
95+
print('\nFAILED: %s' % name)
96+
if name in FAST_FAIL:
97+
exit(status)
98+
return status
99+
100+
101+
def main() -> None:
102+
prog, *args = argv
103+
104+
if not set(args).issubset(cmds):
105+
print("usage:", prog, " ".join('[%s]' % k for k in cmds))
106+
exit(1)
107+
108+
if not args:
109+
args = list(cmds)
110+
111+
status = 0
112+
113+
if 'self' in args and 'lint' in args:
114+
# Perform lint and self check in parallel as it's faster.
115+
proc = start_background_cmd('lint')
116+
cmd_status = run_cmd('self')
117+
if cmd_status:
118+
status = cmd_status
119+
cmd_status = wait_background_cmd('lint', proc)
120+
if cmd_status:
121+
status = cmd_status
122+
args = [arg for arg in args if arg not in ('self', 'lint')]
123+
124+
for arg in args:
125+
cmd_status = run_cmd(arg)
126+
if cmd_status:
127+
status = cmd_status
128+
129+
exit(status)
130+
84131

85-
exit(status)
132+
if __name__ == '__main__':
133+
main()

0 commit comments

Comments
 (0)