Part II — Intermediate Development · Chapter 10 · Lesson 5 of 6 · 8 min read
Lesson 5: Performance Gates and Regression Prevention
Lesson level: Advanced intermediate
Purpose: Measure the speed and resource use of PeaLang programs repeatably, set defensible thresholds, and stop slowdowns and behavior regressions from reaching a release.
Learning objectives
By the end of this lesson, you should be able to:
- explain why performance needs a gate and not only occasional checks;
- choose metrics that are stable enough to gate on;
- design repeatable benchmarks with warm-up, repetition, and fixed inputs;
- record a performance baseline and set a tolerance;
- use resource budgets as a deterministic complement to timing;
- diagnose a failing gate and decide whether to fix or re-baseline; and
- combine correctness, coverage, and performance gates into one regression policy.
Release note: commands, options, and metric names in this lesson are illustrative. Verify them against the installed v0.63 release and its built-in help before relying on them.
5.1 Why gate performance
A rule that is correct can still become too slow. Slowdowns arrive in small steps, each of which looks harmless, and nobody notices until a deadline is missed in production. A gate turns “it feels slower” into a pass or fail decision made on every change.
PeaLang hosts often run scripts under execution deadlines and instruction budgets. A rule that creeps toward its budget will eventually fail on a slightly larger input, so performance is also a correctness concern.
5.2 What to measure
| Metric | Stability | Best use |
|---|---|---|
| Wall-clock time | Low; varies with machine load | Trend tracking on a quiet, dedicated runner |
| CPU time | Medium | Comparing runs on the same machine |
| Instruction count | High; deterministic for the same input and engine | Primary gate for regressions in rule logic |
| Peak memory | Medium to high | Detecting leaks and oversized collections |
| Allocations or collection sizes | High | Catching accidental copying in loops |
| Output size | High | Bounding logging and printing |
Prefer deterministic measures for gates and use wall-clock time as supporting evidence. A gate that fails randomly teaches people to ignore it.
5.3 Benchmarks versus tests
A functional test asks whether the result is right. A benchmark asks how much work it took to get that result. Run benchmarks separately from the unit suite so that a slow machine cannot make a correctness failure look like a performance problem, and the reverse.
5.4 Designing a repeatable benchmark
- Fixed inputs: a synthetic data set generated from a recorded seed.
- Representative size: small, typical, and near-limit inputs for the order-discount rules.
- Warm-up: discard the first runs so loading and caching do not distort results.
- Repetition: run enough repetitions to see the spread, and record the median and the worst case.
- Isolation: one benchmark at a time, on a runner with no other heavy work.
- Same configuration: the same engine, limits, and capabilities on every run.
pea bench rules.pea --entry decide --input bench/orders_1k.json --warmup 3 --runs 20
5.5 Reading the results
A benchmark result needs more than one number. Record the distribution so a later reader can tell a real change from noise.
case: bulk_orders_1k
instructions: 184,320
median time: 41 ms worst: 57 ms runs: 20
peak memory: 3.1 MB
The figures above are synthetic and illustrative. The instruction count is the figure to gate on. The times show the spread.
5.6 Setting a performance baseline
A performance baseline works like a coverage baseline from Lesson 2: a recorded result for a known-good revision, stored in version control with the revision it describes.
- Run the benchmark suite on a reviewed revision on the standard runner.
- Record the deterministic metrics exactly and the timing metrics as median and spread.
- Store the baseline beside the benchmark inputs.
- Record the engine, host configuration, and limits used.
- Update the baseline only in a reviewed change that explains the reason.
5.7 Thresholds and tolerance
| Metric | Suggested gate | Reason |
|---|---|---|
| Instruction count | Fail on any increase beyond a small stated tolerance, such as 2% | Deterministic, so even small changes are real |
| Peak memory | Fail on an increase beyond about 5% | Moderately stable |
| Median time | Warn at a larger margin, such as 15%, and fail only on a sustained change | Noisy; investigate rather than block on one run |
| Absolute budget | Fail if the benchmark exceeds the deployment deadline or instruction budget | Protects the real production limit |
These percentages are starting points, not rules. Choose tolerances from the observed spread of your own benchmarks and review them periodically.
5.8 Resource budgets as a gate
Chapter 14 covers runtime resource limits in depth. For testing, a budget is a hard, deterministic gate: run the rule under the limits the host will apply, and fail if the budget is exceeded.
pea run rules.pea --max-instructions 250000 --max-memory 8MB
Because a budget failure does not depend on machine speed, it is reliable in CI. Set the test budget with headroom below the production limit so that an input slightly larger than the benchmark still fits.
5.9 Scaling checks
A rule can be fast on small inputs and slow on large ones. Run the same benchmark at several sizes and check how cost grows.
| Input size | Instructions | Ratio to previous |
|---|---|---|
| 100 orders | 18,400 | — |
| 1,000 orders | 184,000 | about 10× |
| 10,000 orders | 1,840,000 | about 10× |
Roughly tenfold growth for tenfold input is linear. If a tenfold input costs a hundredfold, the rule has a quadratic step, often a nested loop over the same collection. The figures above are synthetic.
5.10 Handling a failing gate
- Reproduce: run the benchmark locally under the same configuration.
- Compare: diff the metrics against the baseline and identify which case changed.
- Locate: use profiling and disassembly from Chapter 12 to find the added work.
- Decide: fix the regression, or accept it deliberately.
- Record: if accepted, re-baseline in a reviewed change with the reason, such as a necessary new validation.
Never raise a threshold or delete a benchmark merely to make a build green. An accepted slowdown is a decision someone owns and explains.
5.11 A combined regression policy
| Gate | Catches | Typical stage |
|---|---|---|
| Format and static check | Style drift and source errors | Every commit |
| Unit and boundary tests | Wrong behavior | Every commit |
| Coverage baseline | Untested code | Pull request |
| Differential and conformance tests | Engine disagreement | Pull request and release |
| Fuzzing campaign (seeded, bounded) | Crashes and violated properties | Scheduled and release |
| Performance and budget gates | Slowdowns and resource growth | Pull request and release |
Each gate answers a different question, and none replaces the others. Keep them separate in the pipeline so a failure names the kind of problem it found.
5.12 Common mistakes
| Mistake | Consequence | Correction |
|---|---|---|
| Gating on wall-clock time from a shared runner | Random failures; the gate is ignored | Gate on instruction count and budgets; use time as a trend |
| No warm-up or too few runs | Noisy, misleading results | Warm up and record the median and spread |
| Benchmarking only small inputs | Scaling defects survive | Test several sizes |
| Raising the threshold to pass | Regressions become the baseline | Re-baseline only with a recorded reason |
| Different limits or engine in baseline and run | Spurious differences | Record and match the configuration |
| Benchmarks mixed into the unit suite | Slow machines hide correctness failures | Run benchmarks separately |
5.13 Diagnostic workflow
- Identify which benchmark and which metric failed.
- Confirm the run used the same engine, limits, inputs, and runner as the baseline.
- Rerun to separate a real change from noise, using the deterministic metrics first.
- Compare against the previous passing revision to find the change that introduced it.
- Profile the failing case and inspect the work added.
- Fix, or re-baseline with a recorded reason, then rerun the full gate set.
5.14 Guided practice
- Explain why instruction count is a better gate than wall-clock time.
- List four things a repeatable benchmark must hold constant.
- Given costs of 18,400 and 1,840,000 for 100 and 10,000 orders, classify the growth.
- Choose tolerances for instruction count, memory, and median time, and justify each.
- Describe the steps to accept a slowdown responsibly.
- Name two ways a nested loop over the same list could appear in a discount rule.
5.15 Hands-on lab: Build a performance gate for the order-discount rules
Goal: Add repeatable benchmarks and a gate to the Chapter 9 project, and prove that the gate catches a regression.
- Generate synthetic order sets of 100, 1,000, and 10,000 orders from a recorded seed.
- Write a benchmark for the decide entry point and run it with warm-up and at least 20 repetitions.
- Record instruction count, median and worst time, and peak memory for each size.
- Check that cost grows roughly linearly with input size.
- Store the results as the baseline with the revision identifier, engine, and limits.
- Add a budget run with limits below the production limit and confirm it passes.
- On a scratch copy, introduce a deliberate regression such as a repeated scan of the order list and confirm the gate fails.
- Remove the regression, rerun, and confirm the gate passes.
Deliverable: The benchmark definitions, the baseline file, the scaling table, the gate configuration with tolerances, the failing and passing gate output for the deliberate regression, and a short note explaining each tolerance.
5.16 Review questions
- Why can a correct rule still need a performance gate?
- Which metrics are deterministic and which are noisy?
- What are warm-up and repetition for?
- What should a performance baseline record?
- Why run benchmarks separately from unit tests?
- How does a budget run differ from a timing benchmark?
- How do you recognize super-linear growth?
- When is it acceptable to re-baseline?
5.17 Applied challenge
Design the complete regression policy for a rules library that will run on both engines under a 50-millisecond deadline. Specify the benchmarks and input sizes, the gated and trend-only metrics with tolerances, the budget limits, the stages at which each gate runs, and the procedure for accepting a justified slowdown.
Lesson summary
Performance gates turn gradual slowdowns into visible, owned decisions. Deterministic measures such as instruction count and resource budgets make reliable gates, while timing supports trend tracking. Repeatable benchmarks use fixed inputs, warm-up, repetition, and a recorded baseline, and scaling checks expose super-linear growth. A failing gate is diagnosed, then fixed or deliberately re-baselined with a reason. Together with tests, coverage, differential checks, and fuzzing, these gates form a layered regression policy.
Transition to the Chapter 10 Checkpoint
The checkpoint, a production-grade test suite, combines everything in this chapter: native tests, filtering and coverage baselines, deterministic fixtures and boundary cases, differential and fuzz testing, and performance gates, applied to the Chapter 9 business-rules project.
Lesson status: Ready for review.