Skip to content

feat: report walltime percentiles and rounds - #205

Open
fargito wants to merge 1 commit into
mainfrom
feat/walltime-percentiles-and-rounds
Open

fargito wants to merge 1 commit into
mainfrom
feat/walltime-percentiles-and-rounds

Conversation

@fargito

@fargito fargito commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Report the distribution of walltime measurements, not only their summary stats.

  • Percentiles: each benchmark's stats in results/<pid>.json gain percentiles_ns, p1 to p99 of the time per iteration. They use statrs' quantile formula on the rounds sorted once: statrs runs an O(n) selection per call, so 99 calls took 5.2 s on 10M rounds against 0.38 s for one sort. q1_ns, median_ns and q3_ns now come from the same list: q1 and q3 are unchanged, and the median, mean and stdev can differ in the last floating-point bit.
  • Rounds: cargo-codspeed writes the time and iteration count of every round, in execution order, to walltime_rounds/<pid>.json.gz in the profile folder:
    {"benchmarks": [{"uri": "...::sort", "times_per_round_ns": [50649, 40082, ...], "iters_per_round": [1, 1, ...]}]}
    Above 100,000 rounds, a benchmark keeps a uniformly random subsample, still in execution order, and then has fewer times_per_round_ns than stats.rounds. On a 9.7M-round benchmark, a 100k subsample kept p1 to p99 within 1.5% and the CDF within 0.4%. The file is gzipped because the runner uploads walltime profile folders uncompressed: on a 100k-round Go benchmark, gzip took the same format from 802 KB to 231 KB. It runs at its fastest level, which compresses 2.5M rounds 11 times faster than the default level, for a file 20 to 30% larger.

The intermediate per-benchmark files under target/codspeed/walltime/raw_results/ carry the rounds until cargo-codspeed aggregates them, so they are now written as compact JSON and read through a buffer.

Compatibility: percentiles_ns is optional, and matches the field runner-shared accepts in CodSpeedHQ/codspeed#630. The rounds live in their own file, so current consumers of the results file are unaffected. Per-benchmark files from older compat crates still load.

CodSpeedHQ/codspeed-go#70 copies the percentile and sampling code into go-runner; a TODO(COD-4241) in both asks to share one implementation.

Refs COD-4241

@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Merging this PR will regress 3 benchmarks

3 warnings, 1 note

⚠️ Unable to measure the execution of 59 benchmarks

These benchmarks measured no execution time, so their results are not comparable and count as unchanged. This usually happens when the compiler removes the code under test.

Prevent compiler optimizations

See affected benchmarks

⚠️ 17 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 9 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

ℹ️ 57 benchmarks have no flamegraph

  • The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze. (9 benchmarks)
  • The flamegraph wasn't generated: no execution data was recorded while the benchmark ran. Make sure the compiler didn't optimize the benchmark out. (48 benchmarks)

See affected benchmarks

⚡ 10 improved benchmarks
❌ 3 regressed benchmarks
✅ 605 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Walltime count_set_bits[1024] 7 ns 8 ns -12.5%
❌ Walltime sudoku 109.5 µs 113.3 µs -3.39%
❌ Walltime knight_tour[6] 8.6 ms 8.8 ms -1.97%
⚡ Walltime add_two_integers[(255, 255)] 71 ns 19 ns ×3.7
⚡ CPU Simulation spawn_on_parent_runtime 69.2 µs 31.9 µs ×2.2
⚡ Walltime generate_combinations[8] 5.8 µs 4 µs +44.59%
⚡ Walltime iter_batched_ref_per_iteration 54 ns 39 ns +38.46%
⚡ Walltime iter_with_setup 58 ns 43 ns +34.88%
⚡ Walltime generate_combinations[7] 3.6 µs 2.7 µs +34.23%
⚡ Walltime spawn_on_parent_runtime 24.9 µs 21.8 µs +14.36%
⚡ Walltime count_set_bits[255] 8 ns 7 ns +14.29%
⚡ Walltime zeroed_buffer[1] 49 µs 45.3 µs +7.99%
⚡ Walltime print_env_hello 5 µs 4.7 µs +7.69%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing feat/walltime-percentiles-and-rounds (c84dd7b) with main (6ab62f6)

Open in CodSpeed

@fargito fargito changed the title feat: report walltime percentiles and raw rounds feat: report walltime percentiles and rounds Nov 18, 2026
@fargito
fargito force-pushed the feat/walltime-percentiles-and-rounds branch 2 times, most recently from 2623443 to 1eece48 Compare November 18, 2026 15:40
Comment thread crates/cargo-codspeed/src/run.rs Resolved
Comment thread crates/cargo-codspeed/src/run.rs Resolved
Comment thread crates/codspeed/src/walltime_results.rs Resolved
Comment thread crates/codspeed/src/walltime_results.rs Outdated
Comment on lines +233 to +250
let kept_indices: Vec<usize> = if times_per_round_ns.len()
<= MAX_WALLTIME_ROUNDS_PER_BENCHMARK
{
(0..times_per_round_ns.len()).collect()
} else {
// Floyd's sampling of distinct indices; the set iterates them in execution order
let mut random = vec![0u8; 8 * MAX_WALLTIME_ROUNDS_PER_BENCHMARK];
getrandom::fill(&mut random).expect("Failed to generate random bytes");
let mut kept = BTreeSet::new();
let n = times_per_round_ns.len();
for (j, r) in (n - MAX_WALLTIME_ROUNDS_PER_BENCHMARK..n).zip(random.chunks_exact(8)) {
let t = (u64::from_ne_bytes(r.try_into().unwrap()) % (j as u64 + 1)) as usize;
if !kept.insert(t) {
kept.insert(j);
}
}
kept.into_iter().collect()
};

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this the most efficient way? If I understand correctly, in the nominal case (no sampling), we rebuild the full array of indices, then rebuild the whole array of values from it. Is this the case? That looks sub-optimal

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it rebuilt both arrays. Now from_runtime_data takes the vectors by value: at 100,000 rounds or fewer they're moved into the rounds without a copy, and the index set is built only when sampling. To avoid converting u128 to u64 on the way, the rounds struct now holds u128; the JSON is unchanged, since both are written as plain integers.

Comment thread crates/codspeed/src/walltime_results.rs Resolved
@fargito
fargito force-pushed the feat/walltime-percentiles-and-rounds branch from 1eece48 to 4873847 Compare November 19, 2026 10:08
@fargito
fargito marked this pull request as ready for review November 19, 2026 10:18
@greptile-apps

greptile-apps Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge; no actionable issues were found.

Summary

This PR adds walltime distributions and saves sampled rounds separately from summary results.

  • Walltime summaries now show p1 through p99.
  • Walltime runs now save per-round timings in a separate file.

No actionable issues found.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Benchmark measurements] --> B[Compute statistics and percentiles]
  A --> C[Keep at most 100000 paired rounds]
  B --> D[Compact intermediate JSON]
  C --> D
  D --> E[cargo-codspeed aggregation]
  E --> F[results/pid.json]
  E --> G[walltime_rounds/pid.json.gz]
Loading

Reviews (1) · Last reviewed commit: "feat: report walltime percentiles and ro..." · Reviewed by Greptile

Walltime benchmarks run many rounds, but the results only carried summary
stats. Report the distribution as well:

- Each benchmark's stats gain `percentiles_ns`, p1 to p99 of the time per
  iteration. They use statrs' quantile formula on the rounds sorted once:
  statrs runs an O(n) selection per call, so 99 calls took 5.2 s on 10M
  rounds against 0.38 s for one sort. q1, median and q3 now come from them:
  q1 and q3 are unchanged, and the median, mean and stdev can differ in the
  last floating-point bit.
- cargo-codspeed writes the time and iteration count of every round, in
  execution order, to `walltime_rounds/<pid>.json.gz` next to
  `results/<pid>.json`. Above 100,000 rounds, a benchmark keeps a uniformly
  random subsample, still in execution order, and then has fewer rounds
  than `stats.rounds`: on a 9.7M-round benchmark, that subsample kept p1 to
  p99 within 1.5% and the CDF within 0.4%. The runner uploads walltime
  profile folders uncompressed, so the file is gzipped, at the fastest
  level: it runs 11 times faster than the default on 2.5M rounds, for a
  file 20 to 30% larger.

The per-benchmark files under `target/codspeed/walltime/raw_results/` now
carry the rounds until cargo-codspeed aggregates them, so they are written
as compact JSON and read through a buffer.

Results files keep parsing with older consumers: the new stats field is
optional, and the rounds live in their own file.

Refs COD-4241
@fargito
fargito force-pushed the feat/walltime-percentiles-and-rounds branch from 4873847 to c84dd7b Compare November 23, 2026 14:09
Comment on lines +13 to +14
// TODO(COD-4241): go-runner in codspeed-go copies this file's percentile and round sampling code;
// make it call this crate instead.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having the comment in codspeed-go is enough

Comment on lines +217 to +218
let mut rounds_writer =
io::BufWriter::new(GzEncoder::new(rounds_file, Compression::fast()));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open question: Do we really need to compress it? We're already gzipping it in the runner if it exceeds the threshold. So we might not need this

Comment on lines +56 to +73
#[derive(Debug, Serialize, Deserialize)]
struct BenchmarkRounds {
times_per_round_ns: Vec<u128>,
iters_per_round: Vec<u128>,
}

#[derive(Debug, Serialize)]
struct UriRounds {
uri: String,
#[serde(flatten)]
rounds: BenchmarkRounds,
}

/// Content of `walltime_rounds/<pid>.json.gz` in the profile folder
#[derive(Debug, Serialize)]
pub struct WalltimeRounds {
benchmarks: Vec<UriRounds>,
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also just wondering, couldn't we include the UriRounds int he WalltimeBenchmark/BenchmarkStats → because we already have the URI+name there → then we'd not have duplicate info/files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants