clay-borg/games/ground/benches/synthetic.rs
tegwick 72c594ee49 CI: enforce every gate; close the silent-skip holes
The gates existed; CI ran half of them and tolerated the failure case.

- cb-sim no longer has a "tolerable" non-zero exit. An unregistered game
  prefix is a failure, and a run in which nothing executed is a failure.
  Previously CI carried `|| test $? -eq 2`, so renaming a scenario prefix
  would have skipped every scenario while the pipeline stayed green.
  Verified with a negative control.
- CI now runs make coverage (AM-1) and make dep-weight (AM-4), both
  added after CI was written and neither enforced until now.
- dep-weight enforces its targets instead of only reporting them.
- CI lints the shipped-runtime configuration separately, so the feature
  split cannot rot unnoticed.
- Dropped the stale `make deps` target, which still measured the retired
  crate-count metric.

The positive-control rule is now executable: CI runs
`cargo bench -- --test`, which executes every benchmark once, so a
workload that stalls fails the build.

That step immediately found a fourth instance of the error class it was
written for. The committed replay benchmark was the broken version — an
earlier patch never applied, leaving a command sequence that omits
Resolve, so every round produced nothing and the log-building loop spun
forever. It had never run to completion; the reported AM-7 replay
numbers came from a probe test instead. Fixed, given the same positive
control as the round loop, and re-measured from the benchmark: 100k
events fold in 2.18ms (95% CI 2.14-2.23), against a 5s budget.

Evidence now reports confidence intervals rather than point estimates,
so the 3% regression rule in MetricsAndScenarios is enforceable.

The finding worth carrying: writing the positive-control rule into
InnerLoop v1.0 did not prevent the next instance. Making it a CI step
did.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-31 04:02:33 +02:00

233 lines
8.2 KiB
Rust

//! AM-6/AM-7 benchmarks (GameKernel §4): the real GROUND aggregate under
//! the CB-RES-0001 synthetic workload (3-player commit/reveal rounds).
//!
//! The baseline is boardgame.io, recorded in
//! research/CB-RES-0001-harness/boardgame-io/results-260731.json. That
//! harness measured moves/second *while history grew*, and its headline
//! finding was that throughput halved as history doubled. AM-7 exists
//! because of that finding, so the same workload sizes are run here:
//! what matters is the shape of the curve, not only the peak number.
use cb_events::state_hash_hex;
use cb_game_runtime::{ScenarioGame, Setup};
use cb_kernel::{Actor, Aggregate, PlayerId};
use criterion::{criterion_group, criterion_main, BatchSize, Criterion, Throughput};
use games_ground::{Action, GroundCommand, GroundMode, GroundState};
use std::collections::BTreeMap;
fn setup(seed: u64) -> GroundState {
GroundState::setup(
&Setup {
players: 3,
preset: "standard-3p".to_string(),
patch: BTreeMap::new(),
},
seed,
)
.expect("standard-3p setup")
}
fn apply(state: &mut GroundState, actor: Actor, command: &GroundCommand) -> usize {
match state.validate(actor, command) {
Ok(events) => {
for event in &events {
state.fold(event);
}
events.len()
}
Err(_) => 0,
}
}
/// One full round for three players: three Selects, Reveal, the GROUND
/// mode choice, Resolve, End. Returns the number of events applied.
fn play_round(state: &mut GroundState) -> usize {
let picks = [
(
PlayerId(0),
GroundCommand::SelectAction {
action: Action::Attack,
target: Some(PlayerId(1)),
problem: None,
},
),
(
PlayerId(2),
GroundCommand::SelectAction {
action: Action::Support,
target: Some(PlayerId(1)),
problem: None,
},
),
(
PlayerId(1),
GroundCommand::SelectAction {
action: Action::Ground,
target: None,
problem: None,
},
),
];
let mut applied = 0;
for (seat, command) in picks {
applied += apply(state, Actor::Player(seat), &command);
}
applied += apply(state, Actor::System, &GroundCommand::Reveal);
// GR-R05: the GROUND player picks a mode before resolution.
applied += apply(
state,
Actor::Player(PlayerId(1)),
&GroundCommand::ChooseGroundMode {
mode: GroundMode::Gr,
choice: None,
},
);
applied += apply(state, Actor::System, &GroundCommand::Resolve);
applied += apply(state, Actor::System, &GroundCommand::EndRound);
applied
}
/// A game ends after Round 5 (GR-R09), so a long run starts a fresh game
/// rather than idling on a finished one. Setup cost is therefore part of
/// the measurement, at one setup per five rounds.
fn run_rounds(rounds: usize) -> usize {
let mut applied = 0;
let mut state = setup(42);
for round in 0..rounds {
if state.outcome.is_some() {
state = setup(42 + round as u64);
}
let produced = play_round(&mut state);
// A workload whose commands get rejected still "runs", but it
// measures nothing. An earlier version of this bench stalled on
// the GR-R03 stress gate and reported throughput for rounds that
// never happened, so refuse to measure that.
assert!(
produced == EVENTS_PER_ROUND || produced == FINAL_ROUND_EVENTS,
"round {round} produced {produced} events, expected {EVENTS_PER_ROUND} \
(or {FINAL_ROUND_EVENTS} on a game's last round)"
);
applied += produced;
}
applied
}
/// Pinned by the `bench_shape` test in the aggregate crate.
const EVENTS_PER_ROUND: usize = 13;
/// GR-R09: a game's fifth round emits GameEnded instead of RoundEnded
/// plus StepAdvanced, so it is one event shorter.
const FINAL_ROUND_EVENTS: usize = 12;
/// Mean events per round over a 5-round game, scaled by 5 to stay in
/// integers: (4 x 13 + 12) = 64.
const EVENTS_PER_5_ROUNDS: usize = 64;
/// Play one round, appending every applied event to `log`.
fn record_round(state: &mut GroundState, log: &mut Vec<games_ground::GroundEvent>) {
let mut run = |state: &mut GroundState, actor: Actor, cmd: &GroundCommand| {
if let Ok(produced) = state.validate(actor, cmd) {
for event in &produced {
state.fold(event);
log.push(event.clone());
}
}
};
for (seat, action, target) in [
(0u8, Action::Attack, Some(PlayerId(1))),
(2, Action::Support, Some(PlayerId(1))),
(1, Action::Ground, None),
] {
run(
state,
Actor::Player(PlayerId(seat)),
&GroundCommand::SelectAction {
action,
target,
problem: None,
},
);
}
run(state, Actor::System, &GroundCommand::Reveal);
run(
state,
Actor::Player(PlayerId(1)),
&GroundCommand::ChooseGroundMode {
mode: GroundMode::Gr,
choice: None,
},
);
run(state, Actor::System, &GroundCommand::Resolve);
run(state, Actor::System, &GroundCommand::EndRound);
}
fn bench_synthetic(c: &mut Criterion) {
// Events per round is fixed by the workload, so throughput can be
// reported in events/second — the AM-6 unit.
assert_eq!(play_round(&mut setup(1)), EVENTS_PER_ROUND);
let mut group = c.benchmark_group("synthetic-ground-3p");
// AM-6 headline throughput and AM-7 scaling, at the sizes the
// boardgame.io harness used so the curves line up.
for &rounds in &[5_000usize, 10_000, 20_000, 40_000, 100_000] {
group.throughput(Throughput::Elements(
(rounds * EVENTS_PER_5_ROUNDS / 5) as u64,
));
group.bench_function(format!("rounds-{rounds}"), |b| {
b.iter_batched(|| rounds, run_rounds, BatchSize::SmallInput)
});
}
group.finish();
// AM-7 replay, and the honest analogue of the boardgame.io finding:
// a *single* growing event log folded back into state. The round
// benches above restart the game every 5 rounds (GR-R09), so their
// flat curve is partly by construction — this one is not, because
// the log here grows without bound.
let mut replay = c.benchmark_group("replay-ground-3p");
for &target_events in &[10_000usize, 100_000] {
// Build one log by playing real rounds, then measure folding it
// back. Must use the full command sequence: a shortened one
// stalls, because Reveal needs every seat's selection and
// EndRound is gated on Resolve.
let mut log = Vec::with_capacity(target_events);
let mut source = setup(42);
let mut games = 0u64;
while log.len() < target_events {
if source.outcome.is_some() {
games += 1;
source = setup(42 + games);
}
let before = log.len();
record_round(&mut source, &mut log);
// Positive control: a round that yields nothing means the
// workload stalled, and the loop above would spin forever.
assert!(
log.len() > before,
"replay workload stalled: a round produced no events"
);
}
replay.throughput(Throughput::Elements(log.len() as u64));
replay.bench_function(format!("fold-{target_events}-events"), |b| {
b.iter(|| {
let mut state = setup(42);
for event in &log {
state.fold(event);
}
state_hash_hex(&state)
})
});
}
replay.finish();
// AM-7: hashing the full aggregate, the per-round determinism cost.
let mut hashing = c.benchmark_group("state-hash-ground-3p");
hashing.throughput(Throughput::Elements(1));
hashing.bench_function("hash-one-state", |b| {
let state = setup(42);
b.iter(|| state_hash_hex(&state))
});
hashing.finish();
}
criterion_group!(benches, bench_synthetic);
criterion_main!(benches);