From 638cb3d7f2bcf36a81f9d5d2f34de75901aa1823 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Wed, 15 Apr 2026 19:09:48 +0200 Subject: [PATCH] performance-metrics: Add deterministic permutation helper Add deterministic_permutation() which produces a reproducible pseudo random permutation of [0, n) using a Fisher-Yates shuffle seeded by DefaultHasher. This is used by the random read micro benchmarks to generate a fixed access pattern that is identical across runs. Signed-off-by: Anatol Belski --- performance-metrics/src/util.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/performance-metrics/src/util.rs b/performance-metrics/src/util.rs index bbafea0d3..09d6ad7e6 100644 --- a/performance-metrics/src/util.rs +++ b/performance-metrics/src/util.rs @@ -89,6 +89,24 @@ pub fn write_iovec(buf: &[u8]) -> libc::iovec { } } +/// Build a deterministic pseudo-random permutation of `[0, n)`. +/// +/// Uses a Fisher-Yates shuffle seeded by `DefaultHasher` so the +/// permutation is identical across runs. +pub fn deterministic_permutation(n: usize) -> Vec { + use std::collections::hash_map::DefaultHasher; + use std::hash::{Hash, Hasher}; + + let mut indices: Vec = (0..n).collect(); + for i in (1..n).rev() { + let mut h = DefaultHasher::new(); + i.hash(&mut h); + let j = h.finish() as usize % (i + 1); + indices.swap(i, j); + } + indices +} + /// Submit `count` sequential read_vectored calls at `stride`-byte intervals. pub fn submit_reads(async_io: &mut dyn AsyncIo, count: usize, stride: u64, iovec: &[libc::iovec]) { for i in 0..count {