|
| 1 | +//! Feature-gated measurements for the repository's release benchmark. |
| 2 | +
|
| 3 | +use std::num::NonZeroUsize; |
| 4 | +use std::time::{Duration, Instant}; |
| 5 | + |
| 6 | +use serde_json::Value; |
| 7 | + |
| 8 | +use crate::emission_plan::{CompletionPass, LoweredEmissionPlan}; |
| 9 | +use crate::{EmissionPolicy, EmissionReport, ValuesSchemaInput}; |
| 10 | + |
| 11 | +/// One named policy projected from the shared benchmark plan. |
| 12 | +#[derive(Debug, Clone, Copy)] |
| 13 | +pub struct BenchmarkPolicy { |
| 14 | + /// Stable report label. |
| 15 | + pub name: &'static str, |
| 16 | + /// Checked policy projected by the benchmark. |
| 17 | + pub policy: EmissionPolicy, |
| 18 | +} |
| 19 | + |
| 20 | +/// Repeated timings and final artifacts for one benchmark policy. |
| 21 | +#[derive(Debug)] |
| 22 | +pub struct PolicyProjectionBenchmark { |
| 23 | + /// Stable report label. |
| 24 | + pub name: &'static str, |
| 25 | + /// Time spent selecting and materializing the projected tree per run. |
| 26 | + pub projection_times: Vec<Duration>, |
| 27 | + /// Time spent in policy-free completion passes per run. |
| 28 | + pub completion_times: Vec<Duration>, |
| 29 | + /// Completed schema from the final run. |
| 30 | + pub schema: Value, |
| 31 | + /// Emission accounting from the final run. |
| 32 | + pub emission_report: EmissionReport, |
| 33 | +} |
| 34 | + |
| 35 | +/// Measurements from repeatedly building one plan and projecting every policy. |
| 36 | +#[derive(Debug)] |
| 37 | +pub struct MultiPolicyBenchmark { |
| 38 | + /// Time spent constructing the policy-free plan per run. |
| 39 | + pub plan_construction_times: Vec<Duration>, |
| 40 | + /// Process-resident byte increase while retaining the first plan. |
| 41 | + pub retained_plan_bytes: Option<u64>, |
| 42 | + /// Unique canonical provider-candidate payload bytes retained by the plan. |
| 43 | + pub retained_candidate_bytes: usize, |
| 44 | + /// Per-policy projection and completion measurements. |
| 45 | + pub policies: Vec<PolicyProjectionBenchmark>, |
| 46 | +} |
| 47 | + |
| 48 | +/// Builds one immutable plan per run and projects all policies from that plan. |
| 49 | +#[must_use] |
| 50 | +pub fn benchmark_policies( |
| 51 | + input: &ValuesSchemaInput<'_>, |
| 52 | + policies: &[BenchmarkPolicy], |
| 53 | + runs: NonZeroUsize, |
| 54 | +) -> MultiPolicyBenchmark { |
| 55 | + let mut plan_construction_times = Vec::with_capacity(runs.get()); |
| 56 | + let mut retained_plan_bytes = None; |
| 57 | + let mut retained_candidate_bytes = 0; |
| 58 | + let mut outputs = policies |
| 59 | + .iter() |
| 60 | + .map(|policy| PolicyProjectionBenchmark { |
| 61 | + name: policy.name, |
| 62 | + projection_times: Vec::with_capacity(runs.get()), |
| 63 | + completion_times: Vec::with_capacity(runs.get()), |
| 64 | + schema: Value::Null, |
| 65 | + emission_report: EmissionReport::default(), |
| 66 | + }) |
| 67 | + .collect::<Vec<_>>(); |
| 68 | + |
| 69 | + for run in 0..runs.get() { |
| 70 | + let resident_before = resident_memory_kib(); |
| 71 | + let started = Instant::now(); |
| 72 | + let plan = LoweredEmissionPlan::build(input); |
| 73 | + plan_construction_times.push(started.elapsed()); |
| 74 | + if run == 0 { |
| 75 | + retained_plan_bytes = resident_memory_kib() |
| 76 | + .zip(resident_before) |
| 77 | + .map(|(after, before)| after.saturating_sub(before) * 1024); |
| 78 | + retained_candidate_bytes = plan.benchmark_retained_candidate_bytes(); |
| 79 | + } |
| 80 | + |
| 81 | + for (policy, output) in policies.iter().zip(&mut outputs) { |
| 82 | + let started = Instant::now(); |
| 83 | + let projected = plan.project(policy.policy); |
| 84 | + output.projection_times.push(started.elapsed()); |
| 85 | + |
| 86 | + let started = Instant::now(); |
| 87 | + let completed = plan.complete(projected, CompletionPass::Descriptions); |
| 88 | + output.completion_times.push(started.elapsed()); |
| 89 | + output.schema = completed.schema; |
| 90 | + output.emission_report = completed.emission_report; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + MultiPolicyBenchmark { |
| 95 | + plan_construction_times, |
| 96 | + retained_plan_bytes, |
| 97 | + retained_candidate_bytes, |
| 98 | + policies: outputs, |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[cfg(target_os = "linux")] |
| 103 | +fn resident_memory_kib() -> Option<u64> { |
| 104 | + let status = std::fs::read_to_string("/proc/self/status").ok()?; |
| 105 | + let line = status.lines().find(|line| line.starts_with("VmRSS:"))?; |
| 106 | + line.split_ascii_whitespace().nth(1)?.parse().ok() |
| 107 | +} |
| 108 | + |
| 109 | +#[cfg(not(target_os = "linux"))] |
| 110 | +fn resident_memory_kib() -> Option<u64> { |
| 111 | + None |
| 112 | +} |
0 commit comments