performance-metrics: Add util module with shared micro benchmark helpers

These factor out common setup and synchronization patterns used by block
layer micro benchmarks.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
Anatol Belski
2026-03-14 15:28:14 +01:00
committed by Bo Chen
parent b12620cf25
commit be6f63a740
4 changed files with 39 additions and 0 deletions

1
Cargo.lock generated
View File

@@ -1602,6 +1602,7 @@ dependencies = [
"serde_json",
"test_infra",
"thiserror 2.0.18",
"vmm-sys-util",
]
[[package]]

View File

@@ -11,6 +11,7 @@ serde = { workspace = true, features = ["derive", "rc"] }
serde_json = { workspace = true }
test_infra = { path = "../test_infra" }
thiserror = { workspace = true }
vmm-sys-util = { workspace = true }
[lints]
workspace = true

View File

@@ -5,6 +5,7 @@
// Custom harness to run performance tests
mod performance_tests;
mod util;
use std::process::Command;
use std::sync::Arc;

View File

@@ -0,0 +1,36 @@
// Copyright 2026 The Cloud Hypervisor Authors. All rights reserved.
//
// SPDX-License-Identifier: Apache-2.0
//! Shared benchmark helpers.
use std::io::ErrorKind;
use std::thread;
use std::time::Duration;
use vmm_sys_util::eventfd::EventFd;
use vmm_sys_util::tempfile::TempFile;
pub const BLOCK_SIZE: u64 = 4096;
/// Create a temporary file pre sized to hold `num_blocks` blocks.
pub fn sized_tempfile(num_blocks: usize) -> TempFile {
let tmp = TempFile::new().expect("failed to create tempfile");
tmp.as_file()
.set_len(BLOCK_SIZE * num_blocks as u64)
.expect("failed to set file length");
tmp
}
/// Spin and wait until the given eventfd becomes readable.
pub fn wait_for_eventfd(notifier: &EventFd) {
loop {
match notifier.read() {
Ok(_) => return,
Err(e) if e.kind() == ErrorKind::WouldBlock => {
thread::sleep(Duration::from_micros(50));
}
Err(e) => panic!("eventfd read failed: {e}"),
}
}
}