@@ -34,9 +34,9 @@ function show_usage(): void
3434 php run-tests.php [options] [files] [directories]
3535
3636Options:
37- -j<workers> Run up to <workers> simultaneous testing processes in parallel for
38- quicker testing on systems with multiple logical processors.
39- Note that this is experimental feature .
37+ -j<workers> Run up to <workers> simultaneous testing processes. By default,
38+ the worker count is detected automatically. Use -j1 to run
39+ tests sequentially .
4040
4141 -l <file> Read the testfiles to be executed from <file>. After the test
4242 has finished all failed tests are written to the same <file>.
@@ -351,6 +351,7 @@ function main(): void
351351 $ shuffle = false ;
352352 $ bless = false ;
353353 $ workers = null ;
354+ $ workersExplicit = false ;
354355 $ context_line_count = 3 ;
355356 $ num_repeats = 1 ;
356357 $ show_progress = true ;
@@ -412,6 +413,7 @@ function main(): void
412413
413414 switch ($ switch ) {
414415 case 'j ' :
416+ $ workersExplicit = true ;
415417 $ workers = substr ($ argv [$ i ], 2 );
416418 if ($ workers == 0 || !preg_match ('/^\d+$/ ' , $ workers )) {
417419 error ("' $ workers' is not a valid number of workers, try e.g. -j16 for 16 workers " );
@@ -641,6 +643,19 @@ function main(): void
641643 }
642644 }
643645
646+ if (!$ workersExplicit && (!$ selected_tests || count ($ test_files ) > 1 )) {
647+ $ workers = get_default_worker_count ();
648+ if (
649+ $ workers !== null
650+ && ($ valgrind !== null || isset ($ environment ['SKIP_ASAN ' ]))
651+ ) {
652+ $ workers = min ($ workers , 2 );
653+ }
654+ if ($ workers !== null && !can_create_parallel_worker_socket ()) {
655+ $ workers = null ;
656+ }
657+ }
658+
644659 if ($ online === null && !isset ($ environment ['SKIP_ONLINE_TESTS ' ])) {
645660 $ online = false ;
646661 }
@@ -800,6 +815,53 @@ function main(): void
800815 }
801816}
802817
818+ function get_default_worker_count (): ?int
819+ {
820+ if (IS_WINDOWS ) {
821+ $ workerCount = getenv ('NUMBER_OF_PROCESSORS ' );
822+ return is_string ($ workerCount ) ? parse_default_worker_count ($ workerCount ) : null ;
823+ }
824+
825+ $ commands = [
826+ 'nproc 2>/dev/null ' ,
827+ 'getconf _NPROCESSORS_ONLN 2>/dev/null ' ,
828+ 'getconf NPROCESSORS_ONLN 2>/dev/null ' ,
829+ 'sysctl -n hw.logicalcpu 2>/dev/null ' ,
830+ 'sysctl -n hw.ncpu 2>/dev/null ' ,
831+ ];
832+ foreach ($ commands as $ command ) {
833+ $ workerCount = shell_exec ($ command );
834+ if (
835+ is_string ($ workerCount )
836+ && ($ workerCount = parse_default_worker_count ($ workerCount )) !== null
837+ ) {
838+ return $ workerCount ;
839+ }
840+ }
841+
842+ return null ;
843+ }
844+
845+ function parse_default_worker_count (string $ workerCount ): ?int
846+ {
847+ $ workerCount = filter_var (trim ($ workerCount ), FILTER_VALIDATE_INT , [
848+ 'options ' => ['min_range ' => 2 ],
849+ ]);
850+
851+ return $ workerCount !== false ? min ($ workerCount , 10 ) : null ;
852+ }
853+
854+ function can_create_parallel_worker_socket (): bool
855+ {
856+ $ socket = @stream_socket_server ('tcp://127.0.0.1:0 ' );
857+ if ($ socket === false ) {
858+ return false ;
859+ }
860+
861+ fclose ($ socket );
862+ return true ;
863+ }
864+
803865function verify_config (string $ php ): void
804866{
805867 if (empty ($ php ) || !file_exists ($ php )) {
0 commit comments