Files
chanora/crates/chanora_audio/benches/common.rs
EdisonJwa 7188a5a69d feat(perf,benchmark-infra): criterion bench harness + advisory CI workflows (SDD-120)
Implementation of SDD-120 §1-§8:

Bench harness (crates/chanora_audio/benches/):
- common.rs: deterministic synthetic audio (440 Hz sine, no RNG).
- realtime_capture.rs: bench_capture_alloc_count (dhat) +
  bench_capture_callback_wall_clock (criterion).
- opus_codec.rs: bench_opus_encode_latency + bench_opus_decode_latency
  (direct audiopus, not AudioHandler — SDD-120 §3 item 4).
- resampler.rs: bench_resampler_throughput across 44.1->48 /
  16->48 / 48->48 passthrough.

CI tooling (crates/chanora_audio/examples/):
- emit_baseline.rs: aggregates criterion estimates.json outputs
  into the SRS-217 baseline schema.
- compare_baseline.rs: applies SRS-219 tolerance, renders markdown
  table with 🟢/🟡/🔴 markers + yellow simpler-form realization per
  SDD-120 §8.

  Deviation from SDD-120 §2 / §5 / §7 placement: these tools live
  under examples/, not benches/ or src/bin/. Rationale: they must
  consume serde_json (a dev-only dep — production builds must not
  pull it). Cargo only resolves dev-dependencies for [[test]],
  [[bench]], and [[example]] targets; [[bin]] targets under
  src/bin/ see only regular [dependencies]. examples/ keeps the
  binaries out of the production dep tree while still giving them
  cargo run --example invocation. An SDD-120 amendment should
  reflect this.

Workflows (.github/workflows/):
- bench-advisory.yml: PR + push triggers; runs benches; posts a
  sticky PR comment via actions/github-script@v7; job status is
  always success (SRS-218 clause 4 — non-blocking).
- bench-baseline-update.yml: workflow_dispatch only; runs benches;
  opens PR via peter-evans/create-pull-request@v6 (sole writer of
  the SAD-089 baseline JSON).

Cargo.toml additions ([dev-dependencies] only — verified excluded
from --release builds): criterion 0.5, dhat 0.3, serde_json 1.

Source-code seam: minimal pub-but-#[doc(hidden)] bench_seam module
in chanora_audio (engine.rs + lib.rs re-export) so the criterion
bench harness can construct a CaptureState and drive
CaptureState::ingest without re-implementing the engine (SDD-120
§3). Non-iOS targets only — CaptureState itself is iOS-gated.

Initial baseline seed: crates/chanora_audio/benches/baselines/
x86_64-unknown-linux-gnu.json = {}. compare_baseline handles the
missing-baseline case gracefully and emits a 'no red markers'
report; the first manual dispatch of bench-baseline-update.yml
after merge establishes the real values.

Out of scope per SDD-120 §10: production telemetry export,
build-failing hard CI gate, multi-host benchmarking, IDE
integration, Dart-side bridge round-trip bench.

Verification:
- cargo check --workspace --all-targets: PASS.
- cargo bench --bench realtime_capture --no-run: PASS.
- cargo bench --bench opus_codec --no-run: PASS.
- cargo bench --bench resampler --no-run: PASS.
- cargo build --example emit_baseline --example compare_baseline
  -p chanora_audio: PASS.
- cargo test --workspace: 106 passed, 0 failed, 3 ignored — no
  regression from prior count.
2026-05-18 13:52:15 +08:00

58 lines
2.2 KiB
Rust

// SDD-120 §4 — deterministic synthetic input generation.
//
// Shared by the three bench files (`realtime_capture.rs`,
// `opus_codec.rs`, `resampler.rs`). Included via `mod common;` in
// each bench file because criterion bench files compile as
// independent binaries — there is no shared crate boundary between
// them and `common.rs` is NOT registered as a `[[bench]]` entry.
//
// Determinism is load-bearing for §5 baseline stability: a 440 Hz
// sine at amplitude 0.5 over the 48 kHz mixer rate, no RNG, no
// fade-in, no DC offset. Same input bytes across runs and hosts.
#![allow(dead_code)]
use std::f32::consts::PI;
/// Frame count of one 20 ms Opus frame at 48 kHz mono — matches
/// `chanora_audio::engine::FRAME_SAMPLES` (private constant; the
/// value `960` is fixed by the Opus codec protocol and SAD-088).
pub const FRAME_SAMPLES: usize = 960;
/// 48 kHz mixer rate — matches `chanora_audio::engine::SAMPLE_RATE`.
pub const SAMPLE_RATE: u32 = 48_000;
/// Interleaved synthetic capture buffer. For multi-channel layouts
/// the same scalar value is replicated across each frame's channels
/// (matches cpal's interleaved `Stream` data layout).
pub fn synthetic_capture_buffer(frames: usize, channels: usize) -> Vec<f32> {
let mut out = Vec::with_capacity(frames * channels);
for n in 0..frames {
let s = 0.5 * (2.0 * PI * 440.0 * (n as f32) / 48_000.0).sin();
for _ in 0..channels {
out.push(s);
}
}
out
}
/// One Opus-frame-sized synthetic PCM buffer (960 samples mono).
pub fn synthetic_opus_frame() -> Vec<f32> {
synthetic_capture_buffer(FRAME_SAMPLES, 1)
}
/// Synthetic encoded Opus bytes for decode benches. Built by
/// encoding `synthetic_opus_frame()` with a one-shot encoder.
pub fn synthetic_opus_bytes() -> Vec<u8> {
use audiopus::coder::Encoder;
use audiopus::{Application, Channels, SampleRate};
let enc = Encoder::new(SampleRate::Hz48000, Channels::Mono, Application::Voip)
.expect("opus encoder init");
let pcm = synthetic_opus_frame();
let mut out = vec![0u8; 1275]; // MAX_OPUS_FRAME
let len = enc
.encode_float(&pcm, &mut out)
.expect("opus encode synthetic frame");
out.truncate(len);
out
}