Skip to content

perf(helpers): cache or cheapen generate_id per test #663

@Chemaclass

Description

@Chemaclass

Context

bashunit::helper::generate_id (src/helpers.sh:440) fires on every test export and every script load:

  • src/runner.sh:29 (per test)
  • src/runner.sh:192 (per file load)
  • src/runner.sh:317 (per bench file load)

Each call invokes bashunit::helper::normalize_variable_name plus, in parallel mode, bashunit::random_str (src/globals.sh:30) which loops $RANDOM 6 times for chars. Cheap per call, but multiplied across 800+ tests it adds noticeable overhead and one subshell capture per use (\$(bashunit::helper::generate_id ...)).

Proposal

  1. Cache normalized script id once per file load — reuse across tests in the same script.
  2. For per-test id, append a monotonic counter to the cached script id instead of recomputing:
    : \$((_BASHUNIT_TEST_ID_SEQ += 1))
    export BASHUNIT_CURRENT_TEST_ID=\"\${BASHUNIT_CURRENT_SCRIPT_ID}_\${_BASHUNIT_TEST_ID_SEQ}\"
  3. For parallel uniqueness, include \$\$ (already done) — counter + PID guarantees uniqueness without random_str.
  4. Eliminate the \$(...) capture at runner.sh:29 by writing directly to the env var.

Acceptance

  • generate_id not called per test in hot path (only per script)
  • Test ids remain unique within and across parallel workers
  • No regression in parallel suite isolation
  • All tests green sync + parallel
  • Bash 3.0+ compat preserved
  • Benchmark before/after on tests/unit/ posted in PR

Risk

  • Test IDs used in temp_file/temp_dir naming (src/globals.sh:41+). Verify no consumer expects random suffix.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestrefactoringRefactoring or cleaning related

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions