Skip to content

Commit a3837cf

Browse files
authored
Merge pull request #621 from TypedDevs/perf/runner-optimizations
perf(runner): reduce subprocess overhead in hot paths
2 parents 36e162c + 4af7d09 commit a3837cf

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

src/runner.sh

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@ function bashunit::runner::wait_for_job_slot() {
1818
return 0
1919
fi
2020

21+
# Adaptive backoff: start at 50ms, grow to 200ms to reduce `jobs -r` overhead
22+
# on long-running tests while keeping short tests responsive.
23+
local delay="0.05"
24+
local iterations=0
2125
while true; do
2226
local running_jobs
2327
running_jobs=$(jobs -r | wc -l)
2428
if [ "$running_jobs" -lt "$max_jobs" ]; then
2529
break
2630
fi
27-
sleep 0.05
31+
sleep "$delay"
32+
iterations=$((iterations + 1))
33+
if [ "$iterations" -eq 4 ]; then
34+
delay="0.1"
35+
elif [ "$iterations" -eq 20 ]; then
36+
delay="0.2"
37+
fi
2838
done
2939
}
3040

@@ -259,12 +269,11 @@ function bashunit::runner::parse_data_provider_args() {
259269
local -a args=()
260270
local args_count=0
261271

262-
# Check for shell metacharacters that would break eval or cause globbing
272+
# Check for unescaped shell metacharacters that would break eval or cause
273+
# globbing. Combines the leading-metachar case and the embedded-metachar
274+
# case into a single regex to avoid a second grep subprocess per call.
263275
local has_metachar=false
264-
local _re1='[^\\][\|\&\;\*]'
265-
local _re2='^[\|\&\;\*]'
266-
if [ "$(echo "$input" | "$GREP" -cE "$_re1" || true)" -gt 0 ] \
267-
|| [ "$(echo "$input" | "$GREP" -cE "$_re2" || true)" -gt 0 ]; then
276+
if [ "$(echo "$input" | "$GREP" -cE '(^|[^\])[|&;*]' || true)" -gt 0 ]; then
268277
has_metachar=true
269278
fi
270279

@@ -966,16 +975,32 @@ function bashunit::runner::parse_result_sync() {
966975
local assertions_snapshot=0
967976
local test_exit_code=0
968977

969-
# Extract values using sed instead of BASH_REMATCH for Bash 3.0+ compatibility
970-
# shellcheck disable=SC2001
971-
if [ "$(echo "$result_line" | "$GREP" -cE 'ASSERTIONS_FAILED=[0-9]*##ASSERTIONS_PASSED=[0-9]*' || true)" -gt 0 ]; then
972-
assertions_failed=$(echo "$result_line" | sed 's/.*ASSERTIONS_FAILED=\([0-9]*\)##.*/\1/')
973-
assertions_passed=$(echo "$result_line" | sed 's/.*ASSERTIONS_PASSED=\([0-9]*\)##.*/\1/')
974-
assertions_skipped=$(echo "$result_line" | sed 's/.*ASSERTIONS_SKIPPED=\([0-9]*\)##.*/\1/')
975-
assertions_incomplete=$(echo "$result_line" | sed 's/.*ASSERTIONS_INCOMPLETE=\([0-9]*\)##.*/\1/')
976-
assertions_snapshot=$(echo "$result_line" | sed 's/.*ASSERTIONS_SNAPSHOT=\([0-9]*\)##.*/\1/')
977-
test_exit_code=$(echo "$result_line" | sed 's/.*TEST_EXIT_CODE=\([0-9]*\).*/\1/')
978-
fi
978+
# Extract values using parameter expansion instead of spawning grep/sed subprocesses
979+
case "$result_line" in
980+
*"ASSERTIONS_FAILED="*"##ASSERTIONS_PASSED="*)
981+
local _tail
982+
_tail="${result_line##*ASSERTIONS_FAILED=}"
983+
assertions_failed="${_tail%%##*}"
984+
_tail="${result_line##*ASSERTIONS_PASSED=}"
985+
assertions_passed="${_tail%%##*}"
986+
_tail="${result_line##*ASSERTIONS_SKIPPED=}"
987+
assertions_skipped="${_tail%%##*}"
988+
_tail="${result_line##*ASSERTIONS_INCOMPLETE=}"
989+
assertions_incomplete="${_tail%%##*}"
990+
_tail="${result_line##*ASSERTIONS_SNAPSHOT=}"
991+
assertions_snapshot="${_tail%%##*}"
992+
_tail="${result_line##*TEST_EXIT_CODE=}"
993+
test_exit_code="${_tail%%##*}"
994+
# Strip any trailing non-digit suffix (end of line) from the final field
995+
test_exit_code="${test_exit_code%%[!0-9]*}"
996+
: "${assertions_failed:=0}"
997+
: "${assertions_passed:=0}"
998+
: "${assertions_skipped:=0}"
999+
: "${assertions_incomplete:=0}"
1000+
: "${assertions_snapshot:=0}"
1001+
: "${test_exit_code:=0}"
1002+
;;
1003+
esac
9791004

9801005
bashunit::internal_log "[SYNC]" "fn_name:$fn_name" "execution_result:$execution_result"
9811006

0 commit comments

Comments
 (0)