Skip to content

Commit a3309eb

Browse files
committed
run-tests: optionally parallelize tests
When requested via 'run-tests -j', more than one test will be run at a time. On my system, (i5-3320m with 4 threads / 2 cores), this reduces elapsed time by over 50% when testing pots/unix/micropython. Elapsed time, seconds, best of 3 runs with each -j value: before patchset: 18.1 -j1: 18.1 -j2: 11.3 (-37%) -j4: 8.7 (-52%) -j6: 8.4 (-54%) In all cases the final output is identical: 651 tests performed (18932 individual testcases) 651 tests passed 23 tests skipped: buffered_writer... though the individual pass/fail messages can be different/interleaved.
1 parent b9dd6a5 commit a3309eb

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

tests/run-tests

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import platform
77
import argparse
88
import re
99
import threading
10+
from multiprocessing.pool import ThreadPool
1011
from glob import glob
1112

1213
# Tests require at least CPython 3.3. If your default python3 executable
@@ -213,7 +214,7 @@ class ThreadSafeCounter:
213214
def value(self):
214215
return self._value
215216

216-
def run_tests(pyb, tests, args, base_path="."):
217+
def run_tests(pyb, tests, args, base_path=".", num_threads=1):
217218
test_count = ThreadSafeCounter()
218219
testcase_count = ThreadSafeCounter()
219220
passed_count = ThreadSafeCounter()
@@ -454,8 +455,12 @@ def run_tests(pyb, tests, args, base_path="."):
454455

455456
test_count.add(1)
456457

457-
for test_file in tests:
458-
run_one_test(test_file)
458+
if num_threads > 1:
459+
pool = ThreadPool(num_threads)
460+
pool.map(run_one_test, tests)
461+
else:
462+
for test in tests:
463+
run_one_test(test)
459464

460465
print("{} tests performed ({} individual testcases)".format(test_count.value, testcase_count.value))
461466
print("{} tests passed".format(passed_count.value))
@@ -482,6 +487,7 @@ def main():
482487
cmd_parser.add_argument('--heapsize', help='heapsize to use (use default if not specified)')
483488
cmd_parser.add_argument('--via-mpy', action='store_true', help='compile .py files to .mpy first')
484489
cmd_parser.add_argument('--keep-path', action='store_true', help='do not clear MICROPYPATH when running tests')
490+
cmd_parser.add_argument('-j', '--jobs', default=1, metavar='N', type=int, help='Number of tests to run simultaneously')
485491
cmd_parser.add_argument('files', nargs='*', help='input test files')
486492
args = cmd_parser.parse_args()
487493

@@ -525,7 +531,7 @@ def main():
525531
# run-tests script itself.
526532
base_path = os.path.dirname(sys.argv[0]) or "."
527533
try:
528-
res = run_tests(pyb, tests, args, base_path)
534+
res = run_tests(pyb, tests, args, base_path, args.jobs)
529535
finally:
530536
if pyb:
531537
pyb.close()

0 commit comments

Comments
 (0)