#![expect(
clippy::disallowed_types,
reason = "the wire bodies this module offers a SUT are JSON documents; the composition fixture is stamped as a document and posted as bytes"
)]
use std::collections::BTreeMap;
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
use std::sync::{LazyLock, Mutex, mpsc};
use std::time::{Duration, Instant};
use hdrhistogram::Histogram;
use reqwest::{Method, StatusCode};
use serde_json::{Value, json};
use crate::bench::client::{AuthKind, BenchClient, PreferReturn, created_identifier, query_value};
use crate::bench::compare::summarize;
use crate::bench::fingerprint::EnvironmentFingerprint;
use crate::bench::pack::{
BenchOp, BenchPack, BenchPhase, Fixture, FixtureKind, MeasurePhase, SeedPhase, SweepPhase,
};
use crate::bench::posture::{AuthnMode, Bracket, CanaryTarget, PostureProfile, VersionSample};
use crate::bench::result::{
BenchResult, ErrorClass, LoopRegime, MeasuredPhaseRecord, Methodology, OperationStats,
PackRecord, RepetitionRecord, ScaleRecord, SeedPhaseRecord, SweepPhaseRecord, TargetRecord,
};
use crate::bench::{BOUNDARY_STATEMENT, BenchError, METHODOLOGY};
const HDR_MAX_US: u64 = 600_000_000;
const ADHOC_UID_AQL: &str = "SELECT c/uid/value FROM EHR e CONTAINS COMPOSITION c \
WHERE e/ehr_id/value = $ehr_id LIMIT 10";
const AQL_POINT_LOOKUP: &str = "SELECT c/uid/value FROM EHR e[ehr_id/value=$ehr_id] \
CONTAINS COMPOSITION c WHERE c/uid/value = $uid";
const AQL_EHR_SCAN: &str =
"SELECT c/uid/value FROM EHR e[ehr_id/value=$ehr_id] CONTAINS COMPOSITION c";
const AQL_ORDERED_PAGE: &str = "SELECT c/uid/value, c/context/start_time/value \
FROM EHR e CONTAINS COMPOSITION c ORDER BY c/context/start_time/value DESC";
const SYSTOLIC_MAGNITUDE: &str =
"o/data[at0001]/events[at0006]/data[at0003]/items[at0004]/value/magnitude";
const BLOOD_PRESSURE: &str = "CONTAINS OBSERVATION o[openEHR-EHR-OBSERVATION.blood_pressure.v2]";
static AQL_FILTERED: LazyLock<String> = LazyLock::new(|| {
format!(
"SELECT c/uid/value FROM EHR e[ehr_id/value=$ehr_id] CONTAINS COMPOSITION c \
{BLOOD_PRESSURE} WHERE {SYSTOLIC_MAGNITUDE} >= $systolic"
)
});
static AQL_POPULATION: LazyLock<String> = LazyLock::new(|| {
format!(
"SELECT c/uid/value FROM EHR e CONTAINS COMPOSITION c {BLOOD_PRESSURE} \
WHERE {SYSTOLIC_MAGNITUDE} >= $systolic"
)
});
static AQL_AGGREGATE: LazyLock<String> = LazyLock::new(|| {
format!(
"SELECT COUNT(c/uid/value) FROM EHR e CONTAINS COMPOSITION c {BLOOD_PRESSURE} \
WHERE {SYSTOLIC_MAGNITUDE} >= $systolic"
)
});
const SYSTOLIC_FLOOR: u64 = 90;
const SYSTOLIC_SPAN: u64 = 60;
const POPULATION_FETCH: u64 = 50;
const PAGE_FETCH: u64 = 20;
const ORDERED_PAGES: u64 = 10;
const PAYLOAD_VARIANTS: u64 = 16;
const SYSTOLIC: &str = "/content/0/data/events/0/data/items/0/value/magnitude";
const DIASTOLIC: &str = "/content/0/data/events/0/data/items/1/value/magnitude";
const STREAM_OP: u64 = 0x6f70_6572_6174_696f;
const STREAM_EHR: u64 = 0x6568_725f_7461_7267;
const STREAM_COMPOSITION: u64 = 0x636f_6d70_5f74_6172;
const STREAM_PAYLOAD: u64 = 0x7061_796c_6f61_645f;
const STREAM_QUERY: u64 = 0x7175_6572_795f_7061;
#[derive(Debug)]
pub struct BenchRun<'a> {
pub pack: &'a BenchPack,
pub base_url: &'a str,
pub profile: &'a PostureProfile,
pub auth: AuthKind,
pub user: Option<&'a str>,
pub credential: Option<&'a str>,
pub repetitions: u32,
pub label: Option<&'a str>,
pub scale: f64,
pub seed_workers: Option<usize>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct SeededComposition {
ehr_index: usize,
object_uid: String,
version_uid: String,
}
#[derive(Debug, Default)]
struct BenchCorpus {
ehr_ids: Vec<String>,
compositions: Vec<SeededComposition>,
version_at_time: String,
}
const POSTURE_SAMPLES: usize = 3;
impl BenchCorpus {
fn version_samples(&self) -> Vec<VersionSample> {
let total = self.compositions.len();
if total == 0 {
return Vec::new();
}
let wanted = POSTURE_SAMPLES.min(total);
let mut samples = Vec::with_capacity(wanted);
for slot in 0..wanted {
#[expect(
clippy::integer_division,
reason = "an even spread across the population: exact integer bucketing"
)]
let index = slot.saturating_mul(total) / wanted;
let Some(composition) = self.compositions.get(index) else {
continue;
};
let Some(ehr_id) = self.ehr_ids.get(composition.ehr_index) else {
continue;
};
samples.push(VersionSample {
ehr_id: ehr_id.clone(),
object_uid: composition.object_uid.clone(),
version_uid: composition.version_uid.clone(),
});
}
samples
}
}
const fn authn_of(auth: AuthKind) -> AuthnMode {
match auth {
AuthKind::None => AuthnMode::None,
AuthKind::Basic => AuthnMode::Basic,
AuthKind::Bearer => AuthnMode::Bearer,
}
}
fn created(status: StatusCode) -> bool {
status == StatusCode::CREATED || status == StatusCode::NO_CONTENT
}
fn scaled_ehrs(declared: usize, factor: f64) -> Result<usize, BenchError> {
if !factor.is_finite() || factor <= 0.0 {
return Err(BenchError::Seed {
phase: "(scale)".to_owned(),
detail: format!("--scale must be a positive finite number (got {factor})"),
});
}
#[expect(
clippy::as_conversions,
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
reason = "an operator-scale EHR count times an operator-scale factor, rounded, far below 2^52"
)]
let scaled = (declared as f64 * factor).round() as usize;
Ok(scaled.max(1))
}
fn reads_a_version_at_time(pack: &BenchPack) -> bool {
let at_time = |op: &BenchOp| {
matches!(
op,
BenchOp::GetCompositionAtTime | BenchOp::GetVersionedCompositionVersionAtTime
)
};
pack.sweep_phases()
.iter()
.any(|sweep| sweep.per_composition.iter().any(at_time))
|| pack
.measure_phases()
.iter()
.any(|phase| phase.mix.iter().any(|entry| at_time(&entry.op)))
}
fn object_uid_of(version_uid: &str) -> String {
version_uid
.split("::")
.next()
.unwrap_or(version_uid)
.to_owned()
}
pub fn execute(
run: &BenchRun<'_>,
progress: &(dyn Fn(String) + Sync),
) -> Result<BenchResult, BenchError> {
if run.repetitions == 0 {
return Err(BenchError::Repetitions(run.repetitions));
}
run.pack.verify_pins()?;
if let Some(warning) = plain_http_credential_warning(run.base_url, run.auth) {
progress(warning);
}
let client = BenchClient::with_credential(run.base_url, run.auth, run.user, run.credential)?;
let started_at = jiff::Timestamp::now().to_string();
progress("preflight: proving the write-then-read path".to_owned());
preflight(&client, run.pack)?;
let sut_version = probe_sut_version(&client);
let mut corpus = BenchCorpus::default();
let (seed_phases, declared_workers) = seed_all(&client, run, &mut corpus, progress)?;
corpus.version_at_time = jiff::Timestamp::now().to_string();
let raw = client.without_decompression()?;
let anonymous = client.without_credential()?;
let samples = corpus.version_samples();
let canary = CanaryTarget {
client: &client,
raw: &raw,
anonymous: &anonymous,
profile: run.profile,
authn: authn_of(run.auth),
tls: crate::bench::posture::tls_of(&client.recorded_base_url()),
invalid_twin: run.pack.invalid_twin(),
samples: &samples,
};
progress(format!(
"posture canaries: reading the `{}` declaration before the measured window",
run.profile.name
));
let before = crate::bench::posture::bracket(&canary, Bracket::Before);
let measure_phases = run.pack.measure_phases();
let sweep_phases = run.pack.sweep_phases();
let mut repetitions = Vec::with_capacity(usize::try_from(run.repetitions).unwrap_or(1));
for repetition in 1..=run.repetitions {
repetitions.push(one_repetition(
&client,
run,
repetition,
&sweep_phases,
&measure_phases,
&corpus,
progress,
)?);
}
progress("posture canaries: re-reading the declaration after the measured window".to_owned());
let after = crate::bench::posture::bracket(&canary, Bracket::After);
let posture =
crate::bench::posture::settle(run.profile, canary.authn, canary.tls, &before, &after)?;
let cross = summarize(&repetitions);
let mut result = BenchResult {
schema_version: crate::schema::SCHEMA_VERSION.to_owned(),
boundary_statement: BOUNDARY_STATEMENT.to_owned(),
label: run.label.map(str::to_owned),
pack: PackRecord::of(run.pack),
target: TargetRecord {
base_url: client.recorded_base_url(),
sut_version,
},
environment: EnvironmentFingerprint::detect(),
started_at,
finished_at: jiff::Timestamp::now().to_string(),
scale: ScaleRecord::new(run.scale, declared_workers),
version_at_time: reads_a_version_at_time(run.pack).then(|| corpus.version_at_time.clone()),
seed_phases,
repetitions,
cross,
baselines: Vec::new(),
relative: Vec::new(),
methodology: Methodology {
statement: METHODOLOGY.to_owned(),
open_loop: true,
coordinated_omission_free: true,
seed_once_measure_n: true,
repetitions: run.repetitions,
},
submittable: false,
submittable_unmet: Vec::new(),
posture,
};
result.settle_submittability();
Ok(result)
}
fn seed_all(
client: &BenchClient,
run: &BenchRun<'_>,
corpus: &mut BenchCorpus,
progress: &(dyn Fn(String) + Sync),
) -> Result<(Vec<SeedPhaseRecord>, bool), BenchError> {
let mut seed_phases = Vec::new();
let mut declared_workers = true;
for phase in &run.pack.phases {
let BenchPhase::Seed(seed) = phase else {
continue;
};
let ehrs = scaled_ehrs(seed.ehrs, run.scale)?;
let workers = run.seed_workers.unwrap_or(seed.workers).max(1);
if workers != seed.workers {
declared_workers = false;
}
progress(format!(
"seed phase {}: {} EHRs x {} compositions on {} worker(s)",
seed.name, ehrs, seed.compositions_per_ehr, workers
));
let record = seed_phase(client, seed, ehrs, workers, corpus, progress)?;
seed_phases.push(record);
}
if corpus.ehr_ids.is_empty() {
return Err(BenchError::Seed {
phase: "(none)".to_owned(),
detail: "the pack seeded no EHR, so no measured phase has a target".to_owned(),
});
}
Ok((seed_phases, declared_workers))
}
fn one_repetition(
client: &BenchClient,
run: &BenchRun<'_>,
repetition: u32,
sweep_phases: &[&SweepPhase],
measure_phases: &[&MeasurePhase],
corpus: &BenchCorpus,
progress: &(dyn Fn(String) + Sync),
) -> Result<RepetitionRecord, BenchError> {
let mut sweeps = BTreeMap::new();
for phase in sweep_phases {
progress(format!(
"repetition {repetition}/{}: sweep {} over {} composition(s) x {} request(s) on {} worker(s)",
run.repetitions,
phase.name,
corpus.compositions.len(),
phase.per_composition.len(),
phase.workers
));
let record = sweep_phase(client, phase, corpus, progress)?;
let _replaced = sweeps.insert(phase.name.clone(), record);
}
let mut phases = BTreeMap::new();
for (index, phase) in measure_phases.iter().enumerate() {
progress(format!(
"repetition {repetition}/{}: phase {} at {}/s for {}s warmup + {}s measured",
run.repetitions, phase.name, phase.rate_per_s, phase.warmup_s, phase.duration_s
));
let record = measure_phase(
client,
run.pack,
phase,
u64::try_from(index).unwrap_or(0),
corpus,
progress,
)?;
let _replaced = phases.insert(phase.name.clone(), record);
}
Ok(RepetitionRecord {
repetition,
phases,
sweeps,
})
}
fn plain_http_credential_warning(base_url: &str, auth: AuthKind) -> Option<String> {
if matches!(auth, AuthKind::None) {
return None;
}
let rest = base_url.strip_prefix("http://")?;
let authority = rest.split(['/', '?']).next().unwrap_or_default();
let host = authority
.strip_prefix('[')
.and_then(|inside| inside.split(']').next())
.unwrap_or_else(|| authority.split(':').next().unwrap_or_default());
let loopback = host == "localhost" || host.starts_with("127.") || host == "::1";
if loopback {
return None;
}
Some(format!(
"warning: the credential rides plain http to {host} — every request sends it \
unencrypted across the network; prefer an https base URL for any target that \
is not this machine"
))
}
pub fn preflight(client: &BenchClient, pack: &BenchPack) -> Result<(), BenchError> {
let templates = client.send(
"template list",
Method::GET,
"/definition/template/adl1.4",
None,
PreferReturn::Unstated,
)?;
if !templates.status.is_success() {
return Err(refused(
"template list",
format!(
"GET /definition/template/adl1.4 answered {}",
templates.status
),
));
}
let fixtures = pack.fixtures();
for fixture in fixtures
.iter()
.filter(|fixture| fixture.kind == FixtureKind::OperationalTemplate)
{
upload_template(client, "template upload", fixture)
.map_err(|detail| refused("template upload", detail))?;
}
let Some(composition) = fixtures
.iter()
.find(|fixture| fixture.kind == FixtureKind::Composition)
else {
return Ok(());
};
preflight_round_trip(client, composition)
}
fn refused(exchange: &str, detail: String) -> BenchError {
BenchError::Preflight {
exchange: exchange.to_owned(),
detail,
}
}
fn upload_template(
client: &BenchClient,
exchange: &'static str,
fixture: &Fixture,
) -> Result<(), String> {
let upload = client
.send(
exchange,
Method::POST,
"/definition/template/adl1.4",
Some((fixture.kind.media_type(), fixture.bytes.as_bytes().to_vec())),
PreferReturn::Unstated,
)
.map_err(|error| error.to_string())?;
if created(upload.status) || upload.status == StatusCode::CONFLICT {
return Ok(());
}
Err(format!(
"POST /definition/template/adl1.4 for {} answered {} (201, 204 or 409 expected)",
fixture.key, upload.status
))
}
fn preflight_round_trip(client: &BenchClient, composition: &Fixture) -> Result<(), BenchError> {
let ehr = client.send(
"scratch ehr create",
Method::POST,
"/ehr",
None,
PreferReturn::Identifier,
)?;
if ehr.status != StatusCode::CREATED {
return Err(refused(
"scratch ehr create",
format!("POST /ehr answered {} (201 expected)", ehr.status),
));
}
let ehr_id = created_identifier(&ehr).ok_or_else(|| {
refused(
"scratch ehr create",
"the create disclosed no ehr_id: no uid body, no ETag and no Location".to_owned(),
)
})?;
let commit = client.send(
"scratch composition commit",
Method::POST,
&format!("/ehr/{ehr_id}/composition"),
Some((
composition.kind.media_type(),
composition.bytes.as_bytes().to_vec(),
)),
PreferReturn::Identifier,
)?;
if !created(commit.status) {
return Err(refused(
"scratch composition commit",
format!(
"POST /ehr/{ehr_id}/composition answered {} (201 or 204 expected)",
commit.status
),
));
}
let uid = created_identifier(&commit)
.map(|version| object_uid_of(&version))
.ok_or_else(|| {
refused(
"scratch composition commit",
"the commit disclosed no version uid: no uid body, no ETag and no Location"
.to_owned(),
)
})?;
let read = client.send(
"scratch composition read",
Method::GET,
&format!("/ehr/{ehr_id}/composition/{uid}"),
None,
PreferReturn::Unstated,
)?;
if read.status != StatusCode::OK {
return Err(refused(
"scratch composition read",
format!(
"GET /ehr/{ehr_id}/composition/{uid} answered {} (200 expected)",
read.status
),
));
}
Ok(())
}
#[must_use]
pub fn probe_sut_version(client: &BenchClient) -> Option<String> {
for path in ["/../system/info", "/"] {
let Ok(reply) = client.send(
"version probe",
Method::GET,
path,
None,
PreferReturn::Unstated,
) else {
continue;
};
if !reply.status.is_success() {
continue;
}
let Ok(document) = serde_json::from_slice::<Value>(&reply.body) else {
continue;
};
for pointer in ["/solution_version", "/version", "/info/version"] {
if let Some(version) = document.pointer(pointer).and_then(Value::as_str) {
return Some(version.to_owned());
}
}
}
None
}
fn seed_phase(
client: &BenchClient,
phase: &SeedPhase,
ehrs: usize,
workers: usize,
corpus: &mut BenchCorpus,
progress: &(dyn Fn(String) + Sync),
) -> Result<SeedPhaseRecord, BenchError> {
let fail = |detail: String| BenchError::Seed {
phase: phase.name.clone(),
detail,
};
for fixture in &phase.fixtures {
if fixture.kind != FixtureKind::OperationalTemplate {
continue;
}
upload_template(client, "seed template upload", fixture).map_err(fail)?;
}
let composition = phase
.fixtures
.iter()
.find(|fixture| fixture.kind == FixtureKind::Composition)
.ok_or_else(|| fail("the phase declares no composition fixture to commit".to_owned()))?;
let started = Instant::now();
let workers = workers.max(1);
let base = corpus.ehr_ids.len();
let created_ehrs = seed_ehrs(client, ehrs, workers).map_err(fail)?;
corpus.ehr_ids.extend(created_ehrs);
progress(format!("seed phase {}: {ehrs} EHRs created", phase.name));
let total = ehrs
.checked_mul(phase.compositions_per_ehr)
.ok_or_else(|| fail("the seed volume overflows".to_owned()))?;
let mut compositions =
seed_compositions(client, phase, composition, corpus, base, workers, total)
.map_err(fail)?;
if compositions.len() != total {
return Err(fail(format!(
"committed {} of {total} compositions",
compositions.len()
)));
}
compositions.sort();
corpus.compositions.append(&mut compositions);
let elapsed_s = started.elapsed().as_secs_f64();
let writes = ehrs.saturating_add(total);
#[expect(
clippy::as_conversions,
clippy::cast_precision_loss,
reason = "seed volumes are far below 2^52"
)]
let (bulk_load_writes_per_s, whole_loop_ms_per_composition) = {
let throughput = if elapsed_s > 0.0 {
writes as f64 / elapsed_s
} else {
0.0
};
let per_composition = if total > 0 {
elapsed_s * 1000.0 / total as f64
} else {
0.0
};
(throughput, per_composition)
};
Ok(SeedPhaseRecord {
name: phase.name.clone(),
regime: LoopRegime::ClosedLoop,
ehrs: u64::try_from(ehrs).unwrap_or(u64::MAX),
compositions_per_ehr: u64::try_from(phase.compositions_per_ehr).unwrap_or(u64::MAX),
workers: u64::try_from(workers).unwrap_or(u64::MAX),
elapsed_s,
bulk_load_writes_per_s,
whole_loop_ms_per_composition,
})
}
fn seed_ehrs(client: &BenchClient, count: usize, workers: usize) -> Result<Vec<String>, String> {
let slots: Vec<Mutex<Option<String>>> = (0..count).map(|_| Mutex::new(None)).collect();
let failures: Mutex<Vec<String>> = Mutex::new(Vec::new());
let next = AtomicUsize::new(0);
std::thread::scope(|scope| {
for _ in 0..workers {
let _handle = scope.spawn(|| {
loop {
let index = next.fetch_add(1, Ordering::Relaxed);
if index >= count {
break;
}
let outcome = client
.send(
"seed ehr create",
Method::POST,
"/ehr",
None,
PreferReturn::Identifier,
)
.map_err(|error| error.to_string())
.and_then(|reply| {
if reply.status != StatusCode::CREATED {
return Err(format!("create ehr answered {}", reply.status));
}
created_identifier(&reply)
.ok_or_else(|| "create ehr disclosed no ehr_id".to_owned())
});
match outcome {
Ok(id) => {
if let Some(Ok(mut slot)) = slots.get(index).map(Mutex::lock) {
*slot = Some(id);
}
}
Err(detail) => {
if let Ok(mut recorded) = failures.lock() {
recorded.push(detail);
}
break;
}
}
}
});
}
});
if let Ok(recorded) = failures.lock()
&& let Some(first) = recorded.first()
{
return Err(format!("seeding EHRs: {first}"));
}
let mut ids = Vec::with_capacity(count);
for slot in &slots {
let id = slot
.lock()
.ok()
.and_then(|guard| guard.clone())
.ok_or_else(|| "seeding EHRs left a gap".to_owned())?;
ids.push(id);
}
Ok(ids)
}
fn seed_compositions(
client: &BenchClient,
phase: &SeedPhase,
composition: &Fixture,
corpus: &BenchCorpus,
base: usize,
workers: usize,
total: usize,
) -> Result<Vec<SeededComposition>, String> {
let body = composition.bytes.as_bytes().to_vec();
let committed: Mutex<Vec<SeededComposition>> = Mutex::new(Vec::with_capacity(total));
let failures: Mutex<Vec<String>> = Mutex::new(Vec::new());
let next = AtomicUsize::new(0);
std::thread::scope(|scope| {
for _ in 0..workers {
let _handle = scope.spawn(|| {
let mut local = Vec::new();
loop {
let slot = next.fetch_add(1, Ordering::Relaxed);
if slot >= total {
break;
}
#[expect(
clippy::integer_division,
reason = "which EHR the slot-th commit belongs to: exact integer bucketing"
)]
let local_index = slot / phase.compositions_per_ehr.max(1);
let ehr_index = base.saturating_add(local_index);
let Some(ehr_id) = corpus.ehr_ids.get(ehr_index) else {
break;
};
let outcome = client
.send(
"seed composition commit",
Method::POST,
&format!("/ehr/{ehr_id}/composition"),
Some((composition.kind.media_type(), body.clone())),
PreferReturn::Identifier,
)
.map_err(|error| error.to_string())
.and_then(|reply| {
if !created(reply.status) {
return Err(format!("commit answered {}", reply.status));
}
created_identifier(&reply)
.ok_or_else(|| "commit disclosed no version uid".to_owned())
});
match outcome {
Ok(version) => local.push(SeededComposition {
ehr_index,
object_uid: object_uid_of(&version),
version_uid: version,
}),
Err(detail) => {
if let Ok(mut recorded) = failures.lock() {
recorded.push(detail);
}
break;
}
}
}
if let Ok(mut all) = committed.lock() {
all.append(&mut local);
}
});
}
});
if let Ok(recorded) = failures.lock()
&& let Some(first) = recorded.first()
{
return Err(format!("seeding compositions: {first}"));
}
committed
.into_inner()
.map_err(|error| format!("seeding lock poisoned: {error}"))
}
#[derive(Debug, Clone, Copy)]
struct PlannedArrival {
at: Duration,
op: BenchOp,
recorded: bool,
index: u64,
}
#[derive(Debug)]
struct Completion {
op: BenchOp,
latency_us: u64,
class: Option<ErrorClass>,
recorded: bool,
}
fn build_schedule(pack: &BenchPack, phase: &MeasurePhase, phase_index: u64) -> Vec<PlannedArrival> {
let total = phase.planned_arrivals();
if total == 0 {
return Vec::new();
}
let mut arrivals = Vec::with_capacity(usize::try_from(total).unwrap_or(0));
for index in 0..total {
#[expect(
clippy::as_conversions,
clippy::cast_precision_loss,
reason = "the arrival ordinal is far below 2^52"
)]
let offset_s = index as f64 / phase.rate_per_s;
let draw = crate::perf_run::fnv1a(pack.seed ^ STREAM_OP, &[phase_index, index]);
let Some(op) = phase.op_for_draw(draw) else {
continue;
};
arrivals.push(PlannedArrival {
at: Duration::from_secs_f64(offset_s),
op,
recorded: phase.is_measured(index),
index,
});
}
arrivals
}
fn payload_variants(pack: &BenchPack, fixture: &Fixture) -> Result<Vec<Vec<u8>>, BenchError> {
let template: Value =
serde_json::from_str(fixture.bytes).map_err(|source| BenchError::Serialize {
context: "composition fixture",
source,
})?;
let mut variants = Vec::with_capacity(usize::try_from(PAYLOAD_VARIANTS).unwrap_or(1));
for variant in 0..PAYLOAD_VARIANTS {
let mut document = template.clone();
let draw = crate::perf_run::fnv1a(pack.seed ^ STREAM_PAYLOAD, &[variant]);
if let Some(slot) = document.pointer_mut(SYSTOLIC) {
*slot = json!(90_u64.saturating_add(draw % 60));
}
if let Some(slot) = document.pointer_mut(DIASTOLIC) {
*slot = json!(50_u64.saturating_add((draw >> 8) % 40));
}
variants.push(
serde_json::to_vec(&document).map_err(|source| BenchError::Serialize {
context: "stamped composition",
source,
})?,
);
}
Ok(variants)
}
#[derive(Debug, Clone, Copy)]
struct ArrivalTarget<'a> {
ehr_id: &'a str,
object_uid: &'a str,
version_uid: &'a str,
version_at_time: &'a str,
payload: &'a [u8],
query_draw: u64,
}
fn query_request(op: BenchOp, target: ArrivalTarget<'_>) -> Option<Value> {
let draw = target.query_draw;
let systolic = SYSTOLIC_FLOOR.saturating_add(draw % SYSTOLIC_SPAN);
let offset = ((draw >> 8) % ORDERED_PAGES).saturating_mul(PAGE_FETCH);
let body = match op {
BenchOp::AdhocQueryUid => {
json!({ "q": ADHOC_UID_AQL, "query_parameters": { "ehr_id": target.ehr_id } })
}
BenchOp::AdhocQueryPointLookup => json!({
"q": AQL_POINT_LOOKUP,
"query_parameters": { "ehr_id": target.ehr_id, "uid": target.version_uid },
}),
BenchOp::AdhocQueryEhrScan => {
json!({ "q": AQL_EHR_SCAN, "query_parameters": { "ehr_id": target.ehr_id } })
}
BenchOp::AdhocQueryFiltered => json!({
"q": AQL_FILTERED.as_str(),
"query_parameters": { "ehr_id": target.ehr_id, "systolic": systolic },
}),
BenchOp::AdhocQueryPopulation => json!({
"q": AQL_POPULATION.as_str(),
"fetch": POPULATION_FETCH,
"query_parameters": { "systolic": systolic },
}),
BenchOp::AdhocQueryAggregate => json!({
"q": AQL_AGGREGATE.as_str(),
"query_parameters": { "systolic": systolic },
}),
BenchOp::AdhocQueryOrderedPage => json!({
"q": AQL_ORDERED_PAGE,
"fetch": PAGE_FETCH,
"offset": offset,
}),
BenchOp::CreateComposition
| BenchOp::GetCompositionAtTime
| BenchOp::GetCompositionLatest
| BenchOp::GetEhr
| BenchOp::GetEhrStatus
| BenchOp::GetVersionedComposition
| BenchOp::GetVersionedCompositionRevisionHistory
| BenchOp::GetVersionedCompositionVersionAtTime
| BenchOp::GetVersionedCompositionVersionById
| BenchOp::GetVersionedCompositionVersionLatest => return None,
};
Some(body)
}
fn offer(
client: &BenchClient,
op: BenchOp,
target: ArrivalTarget<'_>,
) -> (bool, Option<ErrorClass>) {
let path = op.path(
target.ehr_id,
target.object_uid,
&query_value(target.version_uid),
target.version_at_time,
);
let reply = match op {
BenchOp::CreateComposition => client.send(
op.as_str(),
Method::POST,
&path,
Some(("application/json", target.payload.to_vec())),
PreferReturn::Identifier,
),
BenchOp::GetCompositionLatest
| BenchOp::GetCompositionAtTime
| BenchOp::GetVersionedComposition
| BenchOp::GetVersionedCompositionVersionLatest
| BenchOp::GetVersionedCompositionVersionAtTime
| BenchOp::GetVersionedCompositionVersionById
| BenchOp::GetVersionedCompositionRevisionHistory
| BenchOp::GetEhr
| BenchOp::GetEhrStatus => client.send(
op.as_str(),
Method::GET,
&path,
None,
PreferReturn::Unstated,
),
BenchOp::AdhocQueryUid
| BenchOp::AdhocQueryAggregate
| BenchOp::AdhocQueryEhrScan
| BenchOp::AdhocQueryFiltered
| BenchOp::AdhocQueryOrderedPage
| BenchOp::AdhocQueryPointLookup
| BenchOp::AdhocQueryPopulation => {
let Some(body) = query_request(op, target) else {
return (false, Some(ErrorClass::Transport));
};
match serde_json::to_vec(&body) {
Ok(bytes) => client.send(
op.as_str(),
Method::POST,
&path,
Some(("application/json", bytes)),
PreferReturn::Unstated,
),
Err(_unserializable) => return (false, Some(ErrorClass::Transport)),
}
}
};
match reply {
Err(BenchError::Transport { source, .. }) => {
let class = if source.is_timeout() {
ErrorClass::Timeout
} else {
ErrorClass::Transport
};
(false, Some(class))
}
Err(_other) => (false, Some(ErrorClass::Transport)),
Ok(reply) => {
let accepted = match op {
BenchOp::CreateComposition => created(reply.status),
BenchOp::GetCompositionAtTime
| BenchOp::GetCompositionLatest
| BenchOp::GetEhr
| BenchOp::GetEhrStatus
| BenchOp::GetVersionedComposition
| BenchOp::GetVersionedCompositionRevisionHistory
| BenchOp::GetVersionedCompositionVersionAtTime
| BenchOp::GetVersionedCompositionVersionById
| BenchOp::GetVersionedCompositionVersionLatest
| BenchOp::AdhocQueryUid
| BenchOp::AdhocQueryAggregate
| BenchOp::AdhocQueryEhrScan
| BenchOp::AdhocQueryFiltered
| BenchOp::AdhocQueryOrderedPage
| BenchOp::AdhocQueryPointLookup
| BenchOp::AdhocQueryPopulation => reply.status == StatusCode::OK,
};
if accepted {
(true, None)
} else {
(false, Some(ErrorClass::of_status(reply.status)))
}
}
}
}
fn aggregate(
observations: &[(BenchOp, u64, Option<ErrorClass>)],
elapsed_s: f64,
) -> Result<BTreeMap<String, OperationStats>, BenchError> {
let mut recorders: BTreeMap<&'static str, (Histogram<u64>, BTreeMap<String, u64>)> =
BTreeMap::new();
for (op, latency_us, class) in observations {
let entry = match recorders.entry(op.as_str()) {
std::collections::btree_map::Entry::Occupied(entry) => entry.into_mut(),
std::collections::btree_map::Entry::Vacant(entry) => {
let histogram = Histogram::new_with_bounds(1, HDR_MAX_US, 3)
.map_err(|error| BenchError::Histogram(error.to_string()))?;
entry.insert((histogram, BTreeMap::new()))
}
};
let _saturated = entry.0.record(latency_us.clamp(&1, &HDR_MAX_US).to_owned());
if let Some(class) = class {
let counter = entry.1.entry(class.as_str().to_owned()).or_insert(0);
*counter = counter.saturating_add(1);
}
}
let mut operations = BTreeMap::new();
for (op, (histogram, classes)) in recorders {
let _replaced = operations.insert(
op.to_owned(),
OperationStats::from_histogram(&histogram, classes, elapsed_s)?,
);
}
Ok(operations)
}
fn sweep_phase(
client: &BenchClient,
phase: &SweepPhase,
corpus: &BenchCorpus,
progress: &(dyn Fn(String) + Sync),
) -> Result<SweepPhaseRecord, BenchError> {
let fail = |detail: String| BenchError::Measure {
phase: phase.name.clone(),
detail,
};
let compositions = corpus.compositions.len();
let per_composition = phase.per_composition.len();
if compositions == 0 || per_composition == 0 {
return Err(fail(
"the sweep has no composition to walk or no request to offer".to_owned(),
));
}
let encoded_at_time = query_value(&corpus.version_at_time);
let workers = phase.workers.max(1);
let next = AtomicUsize::new(0);
let collected: Mutex<Vec<(BenchOp, u64, Option<ErrorClass>)>> =
Mutex::new(Vec::with_capacity(phase.requests(compositions)));
let started = Instant::now();
{
let cursor = &next;
let sink = &collected;
let at_time = encoded_at_time.as_str();
std::thread::scope(|scope| {
for _ in 0..workers {
let _handle = scope.spawn(move || {
let mut local = Vec::new();
loop {
let slot = cursor.fetch_add(1, Ordering::Relaxed);
let Some(seeded) = corpus.compositions.get(slot) else {
break;
};
let ehr_id = corpus
.ehr_ids
.get(seeded.ehr_index)
.map_or("", String::as_str);
for op in &phase.per_composition {
let issued = Instant::now();
let (ok, class) = offer(
client,
*op,
ArrivalTarget {
ehr_id,
object_uid: seeded.object_uid.as_str(),
version_uid: seeded.version_uid.as_str(),
version_at_time: at_time,
payload: &[],
query_draw: u64::try_from(slot).unwrap_or(0),
},
);
let latency_us = u64::try_from(
issued.elapsed().as_micros().min(u128::from(HDR_MAX_US)),
)
.unwrap_or(HDR_MAX_US);
local.push((*op, latency_us, if ok { None } else { class }));
}
}
if let Ok(mut all) = sink.lock() {
all.append(&mut local);
}
});
}
});
}
let elapsed_s = started.elapsed().as_secs_f64();
let observations = collected
.into_inner()
.map_err(|error| fail(format!("sweep lock poisoned: {error}")))?;
let requests = observations.len();
let operations = aggregate(&observations, elapsed_s)?;
#[expect(
clippy::as_conversions,
clippy::cast_precision_loss,
reason = "request counts are far below 2^52"
)]
let whole_loop_us_per_request = if requests > 0 {
elapsed_s * 1_000_000.0 / requests as f64
} else {
0.0
};
progress(format!(
"sweep {} finished: {requests} request(s) in {elapsed_s:.1}s, {whole_loop_us_per_request:.1} us/request whole-loop",
phase.name
));
Ok(SweepPhaseRecord {
name: phase.name.clone(),
regime: LoopRegime::ClosedLoop,
workers: u64::try_from(workers).unwrap_or(u64::MAX),
compositions: u64::try_from(compositions).unwrap_or(u64::MAX),
requests_per_composition: u64::try_from(per_composition).unwrap_or(u64::MAX),
requests: u64::try_from(requests).unwrap_or(u64::MAX),
elapsed_s,
whole_loop_us_per_request,
operations,
})
}
#[expect(
clippy::too_many_lines,
reason = "one measured-window procedure: schedule, dispatch, collect, aggregate"
)]
fn measure_phase(
client: &BenchClient,
pack: &BenchPack,
phase: &MeasurePhase,
phase_index: u64,
corpus: &BenchCorpus,
progress: &(dyn Fn(String) + Sync),
) -> Result<MeasuredPhaseRecord, BenchError> {
let fail = |detail: String| BenchError::Measure {
phase: phase.name.clone(),
detail,
};
let composition_fixture = pack
.fixtures()
.into_iter()
.find(|fixture| fixture.kind == FixtureKind::Composition)
.ok_or_else(|| fail("the pack embeds no composition to write".to_owned()))?;
let payloads = payload_variants(pack, &composition_fixture)?;
let encoded_at_time = query_value(&corpus.version_at_time);
let schedule = build_schedule(pack, phase, phase_index);
let planned_measured = schedule.iter().filter(|arrival| arrival.recorded).count();
let (tx, rx) = mpsc::channel::<Completion>();
let collector = std::thread::spawn(move || {
let mut recorders: BTreeMap<&'static str, (Histogram<u64>, BTreeMap<String, u64>)> =
BTreeMap::new();
let mut warmup: u64 = 0;
let mut broken: u64 = 0;
for done in rx {
if !done.recorded {
warmup = warmup.saturating_add(1);
continue;
}
let entry = match recorders.entry(done.op.as_str()) {
std::collections::btree_map::Entry::Occupied(entry) => entry.into_mut(),
std::collections::btree_map::Entry::Vacant(entry) => {
let Ok(histogram) = Histogram::new_with_bounds(1, HDR_MAX_US, 3) else {
broken = broken.saturating_add(1);
continue;
};
entry.insert((histogram, BTreeMap::new()))
}
};
let _saturated = entry.0.record(done.latency_us.clamp(1, HDR_MAX_US));
if let Some(class) = done.class {
let counter = entry.1.entry(class.as_str().to_owned()).or_insert(0);
*counter = counter.saturating_add(1);
}
}
(recorders, warmup, broken)
});
let start = Instant::now();
let dispatched_measured = AtomicU64::new(0);
let dispatch_span = std::thread::scope(|scope| {
for arrival in &schedule {
let planned = start + arrival.at;
let now = Instant::now();
if planned > now {
std::thread::sleep(planned - now);
}
if arrival.recorded {
let _previous = dispatched_measured.fetch_add(1, Ordering::Relaxed);
}
let tx = tx.clone();
let payloads = &payloads;
let encoded_at_time = encoded_at_time.as_str();
let arrival = *arrival;
let _handle = scope.spawn(move || {
let ehr_draw =
crate::perf_run::fnv1a(pack.seed ^ STREAM_EHR, &[phase_index, arrival.index]);
let composition_draw = crate::perf_run::fnv1a(
pack.seed ^ STREAM_COMPOSITION,
&[phase_index, arrival.index],
);
let payload_draw = crate::perf_run::fnv1a(
pack.seed ^ STREAM_PAYLOAD,
&[phase_index, arrival.index],
);
let query_draw =
crate::perf_run::fnv1a(pack.seed ^ STREAM_QUERY, &[phase_index, arrival.index]);
let ehr_slot = usize::try_from(
ehr_draw % u64::try_from(corpus.ehr_ids.len().max(1)).unwrap_or(1),
)
.unwrap_or(0);
let composition_slot = usize::try_from(
composition_draw % u64::try_from(corpus.compositions.len().max(1)).unwrap_or(1),
)
.unwrap_or(0);
let payload_slot = usize::try_from(payload_draw % PAYLOAD_VARIANTS).unwrap_or(0);
let drawn = corpus.compositions.get(composition_slot);
let target = match drawn {
Some(seeded) if arrival.op.addresses_a_composition() => ArrivalTarget {
ehr_id: corpus
.ehr_ids
.get(seeded.ehr_index)
.map_or("", String::as_str),
object_uid: seeded.object_uid.as_str(),
version_uid: seeded.version_uid.as_str(),
version_at_time: encoded_at_time,
payload: payloads.get(payload_slot).map_or(&[][..], Vec::as_slice),
query_draw,
},
_ => ArrivalTarget {
ehr_id: corpus.ehr_ids.get(ehr_slot).map_or("", String::as_str),
object_uid: "",
version_uid: "",
version_at_time: encoded_at_time,
payload: payloads.get(payload_slot).map_or(&[][..], Vec::as_slice),
query_draw,
},
};
let (ok, class) = offer(client, arrival.op, target);
let latency = planned.elapsed();
let latency_us = u64::try_from(latency.as_micros().min(u128::from(HDR_MAX_US)))
.unwrap_or(HDR_MAX_US);
let _closed = tx.send(Completion {
op: arrival.op,
latency_us,
class: if ok { None } else { class },
recorded: arrival.recorded,
});
});
}
drop(tx);
start.elapsed()
});
let (recorders, warmup_arrivals, broken) = collector.join().map_err(|payload| {
let detail = payload
.downcast_ref::<&str>()
.map(|message| (*message).to_owned())
.or_else(|| payload.downcast_ref::<String>().cloned())
.unwrap_or_else(|| "non-string panic payload".to_owned());
fail(format!("collector thread panicked: {detail}"))
})?;
if broken > 0 {
return Err(fail(format!(
"{broken} arrival(s) could not be recorded into a histogram"
)));
}
let planned_span_s = phase.warmup_s.saturating_add(phase.duration_s);
#[expect(
clippy::as_conversions,
clippy::cast_precision_loss,
reason = "spans and counts are far below 2^52"
)]
let (measured_span_s, offered_load_sustained_per_s, generator_bound) = {
let actual_span = dispatch_span.as_secs_f64().max(planned_span_s as f64);
let measured_span = (actual_span - phase.warmup_s as f64).max(0.0);
let dispatched = dispatched_measured.load(Ordering::Relaxed) as f64;
let offered = if measured_span > 0.0 {
dispatched / measured_span
} else {
0.0
};
let lagged = dispatch_span.as_secs_f64() > planned_span_s as f64 * 1.02;
(measured_span, offered, lagged)
};
let mut operations = BTreeMap::new();
for (op, (histogram, classes)) in recorders {
let _replaced = operations.insert(
op.to_owned(),
OperationStats::from_histogram(&histogram, classes, measured_span_s)?,
);
}
progress(format!(
"phase {} finished: {} measured arrival(s) over {} operation(s)",
phase.name,
dispatched_measured.load(Ordering::Relaxed),
operations.len()
));
Ok(MeasuredPhaseRecord {
regime: LoopRegime::OpenLoop,
rate_per_s: phase.rate_per_s,
warmup_s: phase.warmup_s,
duration_s: phase.duration_s,
planned_measured_arrivals: u64::try_from(planned_measured).unwrap_or(u64::MAX),
dispatched_measured_arrivals: dispatched_measured.load(Ordering::Relaxed),
warmup_arrivals,
offered_load_sustained_per_s,
generator_bound,
operations,
})
}
#[cfg(test)]
#[expect(
clippy::panic_in_result_fn,
reason = "a Result-returning test in the Book ch11 shape that also asserts; \
clippy offers no allow-in-tests knob for this lint"
)]
mod tests {
use super::*;
use crate::bench::pack;
#[test]
fn plain_http_credentials_warn_beyond_loopback() {
let warns = |url: &str, auth: AuthKind| plain_http_credential_warning(url, auth);
let warning = warns("http://cdr.example:8080/openehr/v1", AuthKind::Basic)
.expect("a remote plain-http credential warns");
assert!(warning.contains("cdr.example"), "{warning}");
assert!(
warns("http://[2001:db8::1]:8080/v1", AuthKind::Bearer).is_some(),
"a bracketed remote IPv6 host warns"
);
for silent in [
("http://localhost:8080/openehr/v1", AuthKind::Basic),
("http://127.0.0.1:8080/v1", AuthKind::Bearer),
("http://[::1]:8080/v1", AuthKind::Basic),
("https://cdr.example/openehr/v1", AuthKind::Basic),
("http://cdr.example/openehr/v1", AuthKind::None),
] {
assert!(warns(silent.0, silent.1).is_none(), "{silent:?}");
}
}
#[test]
fn the_schedule_is_deterministic_in_the_seed() {
let deck = pack::smoke();
let Some(phase) = deck.measure_phases().first().copied().cloned() else {
panic!("the smoke pack lost its measured phase");
};
let first = build_schedule(&deck, &phase, 0);
let second = build_schedule(&deck, &phase, 0);
let ops_first: Vec<BenchOp> = first.iter().map(|a| a.op).collect();
let ops_second: Vec<BenchOp> = second.iter().map(|a| a.op).collect();
assert_eq!(ops_first, ops_second);
assert!(!first.is_empty());
}
#[test]
fn separate_phases_draw_separate_streams() {
let deck = pack::smoke();
let Some(phase) = deck.measure_phases().first().copied().cloned() else {
panic!("the smoke pack lost its measured phase");
};
let zero: Vec<BenchOp> = build_schedule(&deck, &phase, 0)
.iter()
.map(|a| a.op)
.collect();
let one: Vec<BenchOp> = build_schedule(&deck, &phase, 1)
.iter()
.map(|a| a.op)
.collect();
assert_ne!(zero, one);
}
#[test]
fn warmup_arrivals_are_excluded_from_the_measured_span() {
let deck = pack::smoke();
let phase = MeasurePhase {
name: "t".to_owned(),
rate_per_s: 10.0,
warmup_s: 2,
duration_s: 3,
mix: vec![pack::MixEntry::new(BenchOp::GetEhr, 1, "the EHR read")],
};
let schedule = build_schedule(&deck, &phase, 0);
assert_eq!(schedule.len(), 50);
assert_eq!(schedule.iter().filter(|a| a.recorded).count(), 30);
assert!(schedule.iter().take(20).all(|a| !a.recorded));
}
#[test]
fn a_zero_rate_schedules_nothing() {
let deck = pack::smoke();
let phase = MeasurePhase {
name: "t".to_owned(),
rate_per_s: 0.0,
warmup_s: 0,
duration_s: 10,
mix: vec![pack::MixEntry::new(BenchOp::GetEhr, 1, "the EHR read")],
};
assert!(build_schedule(&deck, &phase, 0).is_empty());
}
#[test]
fn payload_variants_are_deterministic_and_distinct() -> Result<(), BenchError> {
let deck = pack::smoke();
let fixture = deck
.fixtures()
.into_iter()
.find(|fixture| fixture.kind == FixtureKind::Composition)
.ok_or_else(|| BenchError::Histogram("no composition fixture".to_owned()))?;
let first = payload_variants(&deck, &fixture)?;
let second = payload_variants(&deck, &fixture)?;
assert_eq!(first, second);
assert_eq!(first.len(), 16);
let distinct: std::collections::BTreeSet<&Vec<u8>> = first.iter().collect();
assert!(distinct.len() > 1, "every variant carried the same bytes");
Ok(())
}
#[test]
fn a_version_uid_reduces_to_its_versioned_object() {
assert_eq!(object_uid_of("abc::sys::3"), "abc");
assert_eq!(object_uid_of("bare"), "bare");
}
#[test]
fn zero_repetitions_are_refused() {
let deck = pack::smoke();
let error = execute(
&BenchRun {
pack: &deck,
base_url: "http://stub",
profile: &crate::bench::posture::MINIMAL,
auth: AuthKind::None,
user: None,
credential: None,
repetitions: 0,
label: None,
scale: 1.0,
seed_workers: None,
},
&|_message| {},
)
.unwrap_err();
assert!(matches!(error, BenchError::Repetitions(0)), "{error}");
}
#[test]
fn the_scale_factor_shrinks_the_population_without_emptying_it()
-> Result<(), Box<dyn std::error::Error>> {
assert_eq!(scaled_ehrs(100, 1.0)?, 100);
assert_eq!(scaled_ehrs(100, 0.1)?, 10);
assert_eq!(scaled_ehrs(100, 2.5)?, 250);
assert_eq!(scaled_ehrs(100, 0.0001)?, 1, "a scaled run still seeds one");
for refused in [0.0, -1.0, f64::NAN, f64::INFINITY] {
assert!(scaled_ehrs(100, refused).is_err(), "{refused} was accepted");
}
Ok(())
}
#[test]
fn only_a_pack_that_reads_at_an_instant_records_one() {
assert!(!reads_a_version_at_time(&pack::smoke()));
assert!(reads_a_version_at_time(&pack::community_vitals()));
}
fn query_target(draw: u64) -> ArrivalTarget<'static> {
ArrivalTarget {
ehr_id: "EHR-7",
object_uid: "c-1",
version_uid: "c-1::sut::1",
version_at_time: "2026-08-29T00%3A00%3A00Z",
payload: &[],
query_draw: draw,
}
}
#[test]
fn every_query_class_carries_its_own_statement() {
let mut statements: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
for op in BenchOp::ALL {
let body = query_request(*op, query_target(1));
assert_eq!(
body.is_some(),
op.is_adhoc_query(),
"{op} disagrees about being a query"
);
let Some(body) = body else { continue };
let statement = body
.pointer("/q")
.and_then(Value::as_str)
.unwrap_or_default()
.to_owned();
assert!(statement.starts_with("SELECT "), "{op}: {statement}");
assert!(
statements.insert(statement.clone()),
"{op} repeats another class's statement: {statement}"
);
}
assert_eq!(statements.len(), 7);
}
#[test]
fn each_query_class_has_the_shape_its_rationale_claims() {
assert!(AQL_POINT_LOOKUP.contains("WHERE c/uid/value = $uid"));
assert!(AQL_POINT_LOOKUP.contains("EHR e[ehr_id/value=$ehr_id]"));
assert!(AQL_EHR_SCAN.contains("EHR e[ehr_id/value=$ehr_id]"));
assert!(!AQL_EHR_SCAN.contains("WHERE"), "the scan filters");
assert!(AQL_FILTERED.contains("EHR e[ehr_id/value=$ehr_id]"));
assert!(AQL_FILTERED.contains(SYSTOLIC_MAGNITUDE));
assert!(AQL_FILTERED.ends_with(">= $systolic"));
assert!(AQL_POPULATION.contains(SYSTOLIC_MAGNITUDE));
assert!(
!AQL_POPULATION.contains("$ehr_id"),
"the population query is scoped to one EHR"
);
assert!(AQL_AGGREGATE.contains("COUNT(c/uid/value)"));
assert!(AQL_AGGREGATE.contains(SYSTOLIC_MAGNITUDE));
assert!(AQL_ORDERED_PAGE.contains("ORDER BY c/context/start_time/value DESC"));
assert!(!AQL_ORDERED_PAGE.contains("$ehr_id"));
}
#[test]
fn only_the_paged_classes_carry_a_fetch_bound() {
let fetch = |op: BenchOp| {
query_request(op, query_target(0x0102_0304))
.and_then(|body| body.pointer("/fetch").and_then(Value::as_u64))
};
assert_eq!(fetch(BenchOp::AdhocQueryPopulation), Some(50));
assert_eq!(fetch(BenchOp::AdhocQueryOrderedPage), Some(20));
assert_eq!(fetch(BenchOp::AdhocQueryPointLookup), None);
assert_eq!(fetch(BenchOp::AdhocQueryEhrScan), None);
assert_eq!(fetch(BenchOp::AdhocQueryFiltered), None);
assert_eq!(fetch(BenchOp::AdhocQueryAggregate), None);
}
#[test]
fn query_parameters_are_deterministic_in_the_draw() {
for draw in [0_u64, 1, 7, 0x0102_0304_0506_0708, u64::MAX] {
assert_eq!(
query_request(BenchOp::AdhocQueryFiltered, query_target(draw)),
query_request(BenchOp::AdhocQueryFiltered, query_target(draw)),
"draw {draw} is not deterministic"
);
let systolic = query_request(BenchOp::AdhocQueryFiltered, query_target(draw))
.and_then(|body| {
body.pointer("/query_parameters/systolic")
.and_then(Value::as_u64)
})
.unwrap_or_default();
assert!((90..150).contains(&systolic), "{systolic} is off the band");
let offset = query_request(BenchOp::AdhocQueryOrderedPage, query_target(draw))
.and_then(|body| body.pointer("/offset").and_then(Value::as_u64))
.unwrap_or_default();
assert!(
offset < 200 && offset.is_multiple_of(20),
"{offset} is off the grid"
);
}
}
#[test]
fn different_draws_move_the_threshold_and_the_page() {
let systolic = |draw: u64| {
query_request(BenchOp::AdhocQueryFiltered, query_target(draw)).and_then(|body| {
body.pointer("/query_parameters/systolic")
.and_then(Value::as_u64)
})
};
assert_ne!(systolic(0), systolic(1));
let offset = |draw: u64| {
query_request(BenchOp::AdhocQueryOrderedPage, query_target(draw))
.and_then(|body| body.pointer("/offset").and_then(Value::as_u64))
};
assert_ne!(offset(0), offset(1 << 8));
}
#[test]
fn the_point_lookup_addresses_the_drawn_composition() {
let Some(body) = query_request(BenchOp::AdhocQueryPointLookup, query_target(3)) else {
panic!("the point lookup carries no body");
};
assert_eq!(
body.pointer("/query_parameters/uid")
.and_then(Value::as_str),
Some("c-1::sut::1")
);
assert_eq!(
body.pointer("/query_parameters/ehr_id")
.and_then(Value::as_str),
Some("EHR-7")
);
assert!(
BenchOp::AdhocQueryPointLookup.addresses_a_composition(),
"the point lookup would be given an EHR-only target"
);
}
#[test]
fn the_community_walk_addresses_only_compositions() {
let deck = pack::community_vitals();
let Some(sweep) = deck.sweep_phases().first().copied() else {
panic!("the community pack lost its walk");
};
assert!(
sweep
.per_composition
.iter()
.all(|op| op.addresses_a_composition())
);
}
}