Commit fea5369d

fea5369dddb0Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

build: bundled-test mechanism (Step 1, 15-test sample)

Per-test cc -c on sp_runtime.h's 1163 lines is ~88% of per-test wall time, and sccache hits only ~7.6% on the test step (every commit that touches spinel_codegen.rb invalidates every test's generated C). Bundling small collision-free tests into a single binary amortizes the cc/link/spawn fixed cost across N tests.

Mechanism:

  • test/bundles/*.bundle lists test paths, one per line, # comments OK.
  • Bundle rule wraps each test in def _t_<name>; ... end; _t_<name> for local-var scope isolation, prepends puts "@@@ <name>", and concatenates the bodies + their .expected files.
  • Compiles + runs the combined .rb once; diffs the segmented output against the concatenated expected.
  • Tests listed in any bundle are filtered out of the individual TEST list so they don't double-execute.
  • Original per-test .rb files stay in test/ for individual debugging.

Constraints on tests in a bundle (enforced by hand for now):

  • no top-level class / module / CONST / @ivar (would collide).
  • no ARGV (sp_argv is declared in main, not in def-wrapped scopes).
  • no top-level def (would collide with t wrappers).

Local measurement (315 tests, -j on 16-core dev box): baseline (all individual): 18.4s, 142s CPU with 15-test bundle: 14.1s, 134s CPU → 23% wall-time reduction, 8s CPU savings, with just one bundle.

Step 1 ships only the array_small bundle (15 tests) to validate the mechanism on CI. Coverage will be expanded in Step 2 once the rule is proven against Windows/macOS runners.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
fea5369dddb0
Parent
84d241a676e0

MakefilemodifiedView file

@@ -193,8 +193,18 @@
193193
194194 # ---- Test ----
195195
196-TESTS := $(wildcard test/*.rb)
197-TEST_TARGETS := $(patsubst test/%.rb,build/test-results/%.ok,$(TESTS))
196+# Bundles: groups of small collision-free tests compiled and run as a
197+# single binary to amortize cc/link/spawn cost (the per-test cc -c on
198+# sp_runtime.h dominates wall time, ~88% of per-test cost; bundling 15
199+# tests turns 15× that fixed cost into 1×). See test/bundles/*.bundle
200+# for the lists and the constraints. Tests listed in any bundle are
201+# excluded from individual runs so we don't double-execute them.
202+BUNDLE_FILES := $(wildcard test/bundles/*.bundle)
203+BUNDLED_TESTS := $(shell cat $(BUNDLE_FILES) 2>/dev/null | sed -e 's/\#.*//' -e 's/[[:space:]]*$$//' -e '/^$$/d')
204+BUNDLE_TARGETS := $(patsubst test/bundles/%.bundle,build/test-results/bundle__%.ok,$(BUNDLE_FILES))
205+
206+TESTS := $(filter-out $(BUNDLED_TESTS),$(wildcard test/*.rb))
207+TEST_TARGETS := $(patsubst test/%.rb,build/test-results/%.ok,$(TESTS)) $(BUNDLE_TARGETS)
198208
199209 test: clean-test-results
200210 @$(MAKE) --no-print-directory test-run
@@ -258,6 +268,54 @@
258268 fi; \
259269 rm -rf "$$tmpdir"
260270
271+# Bundle test rule. Concatenates the listed tests into one .rb (each
272+# wrapped in `def _t_<name>; ... end; _t_<name>` for local-var scope
273+# isolation and a `puts "@@@ <name>"` separator), then compiles + runs
274+# the combined .rb once. Output is split by the markers and diffed
275+# against the concatenation of each test's .expected. The original
276+# per-test .rb files stay in test/ for individual debugging — bundling
277+# is purely a CI / wall-time optimization.
278+build/test-results/bundle__%.ok: test/bundles/%.bundle spinel_parse$(EXE) $(SP_RT_LIB) spinel_codegen$(EXE)
279+ @mkdir -p build/test-results
280+ @tmpdir=$$(mktemp -d /tmp/spinel-bundle.XXXXXX); \
281+ rb=$$tmpdir/bundle.rb; \
282+ exp=$$tmpdir/bundle.expected; \
283+ ast=$$tmpdir/bundle.ast; \
284+ cfile=$$tmpdir/bundle.c; \
285+ bin=$$tmpdir/bundle$(EXE); \
286+ act=$$tmpdir/actual; \
287+ rm -f "[email protected]"; \
288+ : > "$$rb"; : > "$$exp"; \
289+ sed -e 's/#.*//' -e 's/[[:space:]]*$$//' -e '/^$$/d' "$<" | while IFS= read -r tf; do \
290+ name=$$(basename "$$tf" .rb | sed 's/[^a-zA-Z0-9]/_/g'); \
291+ printf 'def _t_%s\n' "$$name" >> "$$rb"; \
292+ cat "$$tf" >> "$$rb"; \
293+ printf '\nend\nputs "@@@ %s"\n_t_%s\n\n' "$$name" "$$name" >> "$$rb"; \
294+ printf '@@@ %s\n' "$$name" >> "$$exp"; \
295+ cat "$$tf.expected" >> "$$exp"; \
296+ done; \
297+ ./spinel_parse$(EXE) "$$rb" "$$ast" 2>/dev/null && \
298+ ./spinel_codegen$(EXE) "$$ast" "$$cfile" 2>/dev/null && \
299+ $(CC) $(CFLAGS) -Werror $(SEC_FLAGS) -Ilib -c "$$cfile" -o "$$cfile.o" 2>/dev/null && \
300+ $(CC) $(CFLAGS) "$$cfile.o" $(SP_RT_LIB) $(LDFLAGS) -lm $(GC_FLAGS) -o "$$bin" 2>/dev/null; \
301+ if [ $$? -eq 0 ]; then \
302+ $(TIMEOUT60) "$$bin" >"$$act" 2>/dev/null; \
303+ LC_ALL=C sed 's/\r$$//' "$$act" >"$$act.n"; \
304+ LC_ALL=C sed 's/\r$$//' "$$exp" >"$$exp.n"; \
305+ if cmp -s "$$exp.n" "$$act.n"; then \
306+ echo PASS > "$@"; \
307+ if [ -t 1 ]; then printf .; fi; \
308+ else \
309+ echo FAIL > "$@"; \
310+ diff -u "$$exp.n" "$$act.n" > "[email protected]" 2>&1 || true; \
311+ if [ -t 1 ]; then printf F; fi; \
312+ fi; \
313+ else \
314+ echo ERR > "$@"; \
315+ if [ -t 1 ]; then printf E; fi; \
316+ fi; \
317+ rm -rf "$$tmpdir"
318+
261319 clean-test-results:
262320 @rm -rf build/test-results
263321