From c82686a9dc598fe2d677ce6222d876f6e3506cc8 Mon Sep 17 00:00:00 2001 From: Mats Wichmann Date: Fri, 4 Feb 2022 09:35:12 -0700 Subject: test runner: accept -j 0 to mean detect cpu count Signed-off-by: Mats Wichmann --- CHANGES.txt | 2 ++ runtest.py | 31 +++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 005b0a4..8d3af21 100755 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -46,6 +46,8 @@ RELEASE VERSION/DATE TO BE FILLED IN LATER Calls now look like 'debug("template %s", text)' rather than 'debug("template %s" % text)' so the logging system does the interpolation only when/if needed (was a pylint warning). + - runtest.py now accepts -j 0 to auto-detect number of usable + processors for testing threads. RELEASE 4.3.0 - Tue, 16 Nov 2021 18:12:46 -0700 diff --git a/runtest.py b/runtest.py index 386e73a..c05e1c1 100755 --- a/runtest.py +++ b/runtest.py @@ -43,7 +43,6 @@ suppress_output = False script = os.path.basename(sys.argv[0]) usagestr = """\ %(script)s [OPTIONS] [TEST ...] - %(script)s -h|--help """ % locals() epilogstr = """\ @@ -86,7 +85,7 @@ parser.add_argument('-D', '--devmode', action='store_true', parser.add_argument('-e', '--external', action='store_true', help="Run the script in external mode (for external Tools)") parser.add_argument('-j', '--jobs', metavar='JOBS', default=1, type=int, - help="Run tests in JOBS parallel jobs.") + help="Run tests in JOBS parallel jobs (0 for cpu_count).") parser.add_argument('-l', '--list', action='store_true', dest='list_only', help="List available tests and exit.") parser.add_argument('-n', '--no-exec', action='store_false', @@ -136,10 +135,10 @@ outctl.add_argument('-s', '--short-progress', action='store_true', outctl.add_argument('-t', '--time', action='store_true', dest='print_times', help="Print test execution time.") outctl.add_argument('--verbose', metavar='LEVEL', type=int, choices=range(1, 4), - help="""Set verbose level: - 1 = print executed commands, - 2 = print commands and non-zero output, - 3 = print commands and all output.""") + help="""Set verbose level + (1=print executed commands, + 2=print commands and non-zero output, + 3=print commands and all output).""") # maybe add? # outctl.add_argument('--version', action='version', version='%s 1.0' % script) @@ -175,7 +174,7 @@ if args.testlistfile: except FileNotFoundError: sys.stderr.write( parser.format_usage() - + "error: -f/--file testlist file \"%s\" not found\n" % p + + 'error: -f/--file testlist file "%s" not found\n' % p ) sys.exit(1) @@ -191,10 +190,26 @@ if args.excludelistfile: except FileNotFoundError: sys.stderr.write( parser.format_usage() - + "error: --exclude-list file \"%s\" not found\n" % p + + 'error: --exclude-list file "%s" not found\n' % p ) sys.exit(1) +if args.jobs == 0: + try: + # on Linux, check available rather then physical CPUs + args.jobs = len(os.sched_getaffinity(0)) + except AttributeError: + # Windows + args.jobs = os.cpu_count() + +# sanity check +if args.jobs == 0: + sys.stderr.write( + parser.format_usage() + + "Unable to detect CPU count, give -j a non-zero value\n" + ) + sys.exit(1) + if args.jobs > 1 or args.output: # 1. don't let tests write stdout/stderr directly if multi-job, # else outputs will interleave and be hard to read. -- cgit v0.12