@@ -75,11 +75,6 @@ run_package_test() {
7575 # Export variables for the duration of this function's sub-processes
7676 export PROJECT_ID GOOGLE_APPLICATION_CREDENTIALS NOX_FILE NOX_SESSION
7777 export GOOGLE_CLOUD_PROJECT=" ${PROJECT_ID} "
78-
79- # Limit pytest-xdist to 4 workers per package. When 3 packages run in parallel,
80- # if each uses -n=auto they will spawn 16 workers each (48 total threads),
81- # completely thrashing the CPU and causing all tests to hang/timeout.
82- export PYTEST_ADDOPTS=" -n 4"
8378
8479 # Isolate PIP cache to prevent concurrent pip file lock deadlocks
8580 export PIP_CACHE_DIR=" /tmpfs/.pip_cache_$( basename ${package_name} ) "
@@ -164,18 +159,56 @@ for path in `find 'packages' \
164159done
165160
166161if [ -n " $PACKAGES_TO_TEST " ]; then
167- mkdir -p .logs
168162 export -f run_package_test
169163 export system_test_script PROJECT_ROOT KOKORO_GFILE_DIR
170-
171- echo " Running system tests in parallel (3 workers)..."
172- # Use timeout to prevent infinite hangs, and < /dev/null to prevent stdin blocks
173- echo " $PACKAGES_TO_TEST " | tr ' ' ' \n' | awk ' NF' | xargs -P 3 -I {} bash -c ' timeout 15m bash -c "run_package_test \"{}\" < /dev/null" > ".logs/{}.log" 2>&1 || touch ".logs/{}.failed"'
174-
175- for failed in .logs/* .failed; do
176- if [ -f " $failed " ]; then
177- echo " --- FAILED: ${failed% .failed} ---"
178- cat " ${failed% .failed} .log"
164+
165+ # 1. DYNAMIC ROUTING: Automatically detect which packages are CPU hogs by checking if they install pytest-xdist or hardcode workers
166+ LIGHT_TO_TEST=" "
167+ HEAVY_TO_TEST=" "
168+ for pkg in $PACKAGES_TO_TEST ; do
169+ if grep -qE " pytest-xdist|-n=auto|-n=[0-9]+" " packages/$pkg /noxfile.py" " packages/$pkg /setup.py" 2> /dev/null; then
170+ HEAVY_TO_TEST=" $HEAVY_TO_TEST $pkg "
171+ else
172+ LIGHT_TO_TEST=" $LIGHT_TO_TEST $pkg "
173+ fi
174+ done
175+
176+ # 2. PARALLEL LANE (Live Streaming): Run light packages with a parallel job queue.
177+ # We prefix every line with the package name so output streams LIVE and remains readable.
178+ if [ -n " $LIGHT_TO_TEST " ]; then
179+ echo " ============================================================"
180+ echo " Running Lightweight Packages in Parallel (4 workers max)"
181+ echo " ============================================================"
182+ for pkg in $LIGHT_TO_TEST ; do
183+ (
184+ timeout 15m bash -c " run_package_test \" $pkg \" < /dev/null" 2>&1 | awk -v prefix=" [$pkg ]" ' {print prefix, $0}'
185+ if [ ${PIPESTATUS[0]} -ne 0 ]; then touch " .failed_$pkg " ; fi
186+ ) &
187+ # Limit parallel background jobs to 4
188+ while [ $( jobs -r | wc -l) -ge 4 ]; do sleep 1; done
189+ done
190+ wait # Wait for all parallel jobs to finish
191+ fi
192+
193+ # 3. SEQUENTIAL VIP LANE: Run heavy packages one-by-one so they have 100% of the VM resources.
194+ if [ -n " $HEAVY_TO_TEST " ]; then
195+ echo " ============================================================"
196+ echo " Running CPU-Intensive Packages Sequentially"
197+ echo " ============================================================"
198+ for pkg in $HEAVY_TO_TEST ; do
199+ if [ -n " $pkg " ]; then
200+ echo " [$pkg ] Starting sequential execution..."
201+ timeout 25m bash -c " run_package_test \" $pkg \" < /dev/null" 2>&1 | awk -v prefix=" [$pkg ]" ' {print prefix, $0}'
202+ if [ ${PIPESTATUS[0]} -ne 0 ]; then touch " .failed_$pkg " ; fi
203+ fi
204+ done
205+ fi
206+
207+ # 4. FAIL STATE EVALUATION
208+ for failed_marker in .failed_* ; do
209+ if [ -f " $failed_marker " ]; then
210+ failed_pkg=" ${failed_marker# .failed_} "
211+ echo " --- FAILED: $failed_pkg ---"
179212 RETVAL=1
180213 fi
181214 done
0 commit comments