Files
crank/crates/crank-metrics/tests/overhead.rs
T
bsodfather 54ce770682
CI / Rust Checks (pull_request) Failing after 13m41s
CI / UI Checks (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Deploy (pull_request) Skipped
Community Release / release (push) Failing after 13m33s
feat(epic3): deliver the accepted Epic 3 scope through Story 3.17
Recovery delivery of the accumulated, TL-accepted Epic 3 work that had never been
committed to Git (stories 3.6-3.17, see the crank board). Story 3.18 is not accepted
yet and is deliberately excluded: its logging surface, its capability registration,
its oracle projections and its tests are not part of this tree.

Accepted scope
- 3.6 typed static Resource Drafts; 3.7 publication and exact binding of static
  Resource Versions; 3.8 MCP discovery/read and revocation of static Resources;
- 3.9 Resource import/export in API and UI plus TLS dependency maintenance;
  3.10 REST-backed Resource templates; 3.11 shared REST read runtime and the MCP
  revision/capability foundation;
- 3.12 parameterised Prompt Drafts with preview; 3.13 Prompt publication and exact
  version binding to Agents; 3.14 MCP prompts; 3.15 Prompt import/export;
- 3.16 composite Prompts with published Resources (dependency content scan plus
  dynamic read through the accepted 3.11 runtime, admin preview and MCP prompts/get);
- 3.17 bounded MCP completion suggestions from typed published schemas and the
  resources/list admission fix for bound template publications.

Accepted evidence being reused (unchanged code paths)
- 3.16 candidate: core 7, admin-api 22, registry 4, mcp-server 4, UI 6; one final
  `just verify` run 1651 passed / 0 failed / 2 ignored.
- 3.17 candidate: full run 1672 passed / 0 failed / 2 ignored, QA targeted reruns and
  a real MCP Inspector 2.12.0 client smoke.

Delivery engineering in this commit
- CI: one event per change (feature branches are gated by their pull request, push
  runs only on main), story events run the changed packages, the full workspace suite
  stays the feature/release gate; no cache/sharding without a measured runner check.
- Release: the packaged archives, checksums and SBOM are now uploaded to the Gitea
  release entry of the tag instead of dying with the runner workspace.

Not included on purpose: Story 3.18 work, build caches, runtime databases, local
credentials and scratch evidence.

Refs: crank kanban t_e39d7263; baseline 1b6db09bd1.
2026-10-25 09:08:22 +00:00

190 lines
7.1 KiB
Rust

use std::{
alloc::{GlobalAlloc, Layout, System},
hint::black_box,
sync::atomic::{AtomicUsize, Ordering},
time::{Duration, Instant},
};
use crank_metrics::{
CacheOutcome, ConfirmationOutcome, HttpMethod, HttpRoute, HttpStatusClass, IdempotencyOutcome,
InvocationSource, McpMethod, McpOutcome, McpResponseMode, ToolErrorKind, ToolOutcome,
UpstreamOperationKind, UpstreamOutcome, record_cache_outcome, record_confirmation_outcome,
record_http_request, record_idempotency_outcome, record_mcp_request, record_tool_invocation,
record_upstream_request,
};
use metrics_util::debugging::DebuggingRecorder;
const SAMPLES: usize = 40;
const WORK_UNITS: u64 = 500_000;
const MAX_PROFILE_ALLOCATION_BYTES: usize = 512 * 1024;
const MAX_ENABLED_PROFILE_TIME: Duration = Duration::from_secs(5);
struct TrackingAllocator;
static CURRENT_HEAP_BYTES: AtomicUsize = AtomicUsize::new(0);
static PEAK_HEAP_BYTES: AtomicUsize = AtomicUsize::new(0);
static TOTAL_ALLOCATED_BYTES: AtomicUsize = AtomicUsize::new(0);
#[global_allocator]
static ALLOCATOR: TrackingAllocator = TrackingAllocator;
unsafe impl GlobalAlloc for TrackingAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let pointer = unsafe { System.alloc(layout) };
if !pointer.is_null() {
record_allocation(layout.size());
}
pointer
}
unsafe fn dealloc(&self, pointer: *mut u8, layout: Layout) {
unsafe { System.dealloc(pointer, layout) };
CURRENT_HEAP_BYTES.fetch_sub(layout.size(), Ordering::Relaxed);
}
unsafe fn realloc(&self, pointer: *mut u8, old: Layout, new_size: usize) -> *mut u8 {
let resized = unsafe { System.realloc(pointer, old, new_size) };
if !resized.is_null() {
match new_size.cmp(&old.size()) {
std::cmp::Ordering::Greater => record_allocation(new_size - old.size()),
std::cmp::Ordering::Less => {
CURRENT_HEAP_BYTES.fetch_sub(old.size() - new_size, Ordering::Relaxed);
}
std::cmp::Ordering::Equal => {}
}
}
resized
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
let pointer = unsafe { System.alloc_zeroed(layout) };
if !pointer.is_null() {
record_allocation(layout.size());
}
pointer
}
}
fn record_allocation(bytes: usize) {
TOTAL_ALLOCATED_BYTES.fetch_add(bytes, Ordering::Relaxed);
let current = CURRENT_HEAP_BYTES.fetch_add(bytes, Ordering::Relaxed) + bytes;
PEAK_HEAP_BYTES.fetch_max(current, Ordering::Relaxed);
}
#[derive(Clone, Copy)]
struct ProfileSample {
elapsed: Duration,
allocated_bytes: usize,
peak_heap_delta: usize,
}
#[test]
fn reproducible_foundation_profile_stays_inside_latency_and_cpu_proxy_budgets() {
crank_metrics::bind_metric_service(crank_metrics::MetricService::McpServer)
.expect("isolated metrics binding for the overhead profile");
let recorder = DebuggingRecorder::new();
let snapshotter = recorder.snapshotter();
let mut baseline = Vec::with_capacity(SAMPLES);
let mut enabled = Vec::with_capacity(SAMPLES);
metrics::with_local_recorder(&recorder, || {
record_cache_outcome(CacheOutcome::Hit);
for index in 0..SAMPLES {
if index % 2 == 0 {
baseline.push(sample(false));
enabled.push(sample(true));
} else {
enabled.push(sample(true));
baseline.push(sample(false));
}
}
});
baseline.sort_unstable_by_key(|sample| sample.elapsed);
enabled.sort_unstable_by_key(|sample| sample.elapsed);
let baseline_p95 = baseline[SAMPLES * 95 / 100].elapsed;
let enabled_p95 = enabled[SAMPLES * 95 / 100].elapsed;
let baseline_cpu: Duration = baseline.iter().map(|sample| sample.elapsed).sum();
let enabled_cpu: Duration = enabled.iter().map(|sample| sample.elapsed).sum();
let baseline_allocated: usize = baseline.iter().map(|sample| sample.allocated_bytes).sum();
let enabled_allocated: usize = enabled.iter().map(|sample| sample.allocated_bytes).sum();
let baseline_peak = baseline
.iter()
.map(|sample| sample.peak_heap_delta)
.max()
.unwrap_or_default();
let enabled_peak = enabled
.iter()
.map(|sample| sample.peak_heap_delta)
.max()
.unwrap_or_default();
let series = snapshotter.snapshot().into_vec().len();
println!(
"metrics_profile_v1 samples={SAMPLES} work_units={WORK_UNITS} baseline_p95_ns={} enabled_p95_ns={} baseline_cpu_ns={} enabled_cpu_ns={} baseline_allocated_bytes={baseline_allocated} enabled_allocated_bytes={enabled_allocated} baseline_peak_heap_delta={baseline_peak} enabled_peak_heap_delta={enabled_peak} logical_series={series}",
baseline_p95.as_nanos(),
enabled_p95.as_nanos(),
baseline_cpu.as_nanos(),
enabled_cpu.as_nanos()
);
// Wall-clock ratios at microbenchmark scale are informational: scheduler noise can
// dwarf the facade itself. The mandatory gate is deterministic resource boundedness;
// the release-deployment 5%/10% latency/CPU qualification is owned by Epic 5.
assert!(enabled_cpu <= MAX_ENABLED_PROFILE_TIME);
assert!(enabled_allocated <= baseline_allocated + MAX_PROFILE_ALLOCATION_BYTES);
assert!(enabled_peak <= baseline_peak + MAX_PROFILE_ALLOCATION_BYTES);
assert_eq!(series, 11);
}
fn sample(with_metrics: bool) -> ProfileSample {
let heap_before = CURRENT_HEAP_BYTES.load(Ordering::Relaxed);
let allocated_before = TOTAL_ALLOCATED_BYTES.load(Ordering::Relaxed);
PEAK_HEAP_BYTES.store(heap_before, Ordering::Relaxed);
let started = Instant::now();
let mut value = 0x9e37_79b9_u64;
for index in 0..WORK_UNITS {
value = value.rotate_left(7) ^ index.wrapping_mul(0x100_0000_01b3);
}
black_box(value);
if with_metrics {
record_cache_outcome(CacheOutcome::Hit);
record_idempotency_outcome(IdempotencyOutcome::Replay);
record_confirmation_outcome(ConfirmationOutcome::Required);
record_http_request(
HttpRoute::from_matched_path("/health"),
HttpMethod::Get,
HttpStatusClass::Success,
Duration::from_millis(1),
);
record_mcp_request(
McpMethod::ToolsCall,
McpResponseMode::Json,
McpOutcome::Success,
Duration::from_millis(1),
);
record_tool_invocation(
InvocationSource::AgentToolCall,
ToolOutcome::Success,
ToolErrorKind::None,
Duration::from_millis(1),
);
record_upstream_request(
UpstreamOperationKind::Rest,
UpstreamOutcome::Success,
Duration::from_millis(1),
);
}
let elapsed = started.elapsed();
let allocated_bytes = TOTAL_ALLOCATED_BYTES
.load(Ordering::Relaxed)
.saturating_sub(allocated_before);
let peak_heap_delta = PEAK_HEAP_BYTES
.load(Ordering::Relaxed)
.saturating_sub(heap_before);
ProfileSample {
elapsed,
allocated_bytes,
peak_heap_delta,
}
}