PeaBox Lab › PeaLang › Learn › Chapter 10 › Lesson 5

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:

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

MetricStabilityBest use
Wall-clock timeLow; varies with machine loadTrend tracking on a quiet, dedicated runner
CPU timeMediumComparing runs on the same machine
Instruction countHigh; deterministic for the same input and enginePrimary gate for regressions in rule logic
Peak memoryMedium to highDetecting leaks and oversized collections
Allocations or collection sizesHighCatching accidental copying in loops
Output sizeHighBounding 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

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.

5.7 Thresholds and tolerance

MetricSuggested gateReason
Instruction countFail on any increase beyond a small stated tolerance, such as 2%Deterministic, so even small changes are real
Peak memoryFail on an increase beyond about 5%Moderately stable
Median timeWarn at a larger margin, such as 15%, and fail only on a sustained changeNoisy; investigate rather than block on one run
Absolute budgetFail if the benchmark exceeds the deployment deadline or instruction budgetProtects 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 sizeInstructionsRatio to previous
100 orders18,400—
1,000 orders184,000about 10×
10,000 orders1,840,000about 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

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

GateCatchesTypical stage
Format and static checkStyle drift and source errorsEvery commit
Unit and boundary testsWrong behaviorEvery commit
Coverage baselineUntested codePull request
Differential and conformance testsEngine disagreementPull request and release
Fuzzing campaign (seeded, bounded)Crashes and violated propertiesScheduled and release
Performance and budget gatesSlowdowns and resource growthPull 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

MistakeConsequenceCorrection
Gating on wall-clock time from a shared runnerRandom failures; the gate is ignoredGate on instruction count and budgets; use time as a trend
No warm-up or too few runsNoisy, misleading resultsWarm up and record the median and spread
Benchmarking only small inputsScaling defects surviveTest several sizes
Raising the threshold to passRegressions become the baselineRe-baseline only with a recorded reason
Different limits or engine in baseline and runSpurious differencesRecord and match the configuration
Benchmarks mixed into the unit suiteSlow machines hide correctness failuresRun benchmarks separately

5.13 Diagnostic workflow

5.14 Guided practice

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.

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

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.

← Previous Next →