Skip to content

Commit 025447e

Browse files
committed
performance-metrics: Add ability to override test iterations
Signed-off-by: Rob Bradford <[email protected]>
1 parent d582121 commit 025447e

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

performance-metrics/src/main.rs

+57-10
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ mod performance_tests;
1313
use clap::{Arg, Command as ClapCommand};
1414
use performance_tests::*;
1515
use serde::{Deserialize, Serialize};
16-
use std::{env, fmt, process::Command, sync::mpsc::channel, thread, time::Duration};
16+
use std::{
17+
env, fmt,
18+
process::Command,
19+
sync::{mpsc::channel, Arc},
20+
thread,
21+
time::Duration,
22+
};
1723
use thiserror::Error;
1824

1925
#[derive(Error, Debug)]
@@ -84,6 +90,21 @@ impl Default for MetricsReport {
8490
}
8591
}
8692

93+
#[derive(Default)]
94+
pub struct PerformanceTestOverrides {
95+
test_iterations: Option<u32>,
96+
}
97+
98+
impl fmt::Display for PerformanceTestOverrides {
99+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
100+
if let Some(test_iterations) = self.test_iterations {
101+
write!(f, "test_iterations = {}", test_iterations)?;
102+
}
103+
104+
Ok(())
105+
}
106+
}
107+
87108
pub struct PerformanceTestControl {
88109
test_timeout: u32,
89110
test_iterations: u32,
@@ -142,9 +163,12 @@ struct PerformanceTest {
142163
}
143164

144165
impl PerformanceTest {
145-
pub fn run(&self) -> PerformanceTestResult {
166+
pub fn run(&self, overrides: &PerformanceTestOverrides) -> PerformanceTestResult {
146167
let mut metrics = Vec::new();
147-
for _ in 0..self.control.test_iterations {
168+
for _ in 0..overrides
169+
.test_iterations
170+
.unwrap_or(self.control.test_iterations)
171+
{
148172
metrics.push((self.func_ptr)(&self.control));
149173
}
150174

@@ -164,8 +188,9 @@ impl PerformanceTest {
164188

165189
// Calculate the timeout for each test
166190
// Note: To cover the setup/cleanup time, 20s is added for each iteration of the test
167-
pub fn calc_timeout(&self) -> u64 {
168-
((self.control.test_timeout + 20) * self.control.test_iterations) as u64
191+
pub fn calc_timeout(&self, test_iterations: &Option<u32>) -> u64 {
192+
((self.control.test_timeout + 20) * test_iterations.unwrap_or(self.control.test_iterations))
193+
as u64
169194
}
170195
}
171196

@@ -405,12 +430,20 @@ const TEST_LIST: [PerformanceTest; 17] = [
405430
},
406431
];
407432

408-
fn run_test_with_timeout(test: &'static PerformanceTest) -> Result<PerformanceTestResult, Error> {
433+
fn run_test_with_timeout(
434+
test: &'static PerformanceTest,
435+
overrides: &Arc<PerformanceTestOverrides>,
436+
) -> Result<PerformanceTestResult, Error> {
409437
let (sender, receiver) = channel::<Result<PerformanceTestResult, Error>>();
438+
let test_iterations = overrides.test_iterations;
439+
let overrides = overrides.clone();
410440
thread::spawn(move || {
411-
println!("Test '{}' running .. ({})", test.name, test.control);
441+
println!(
442+
"Test '{}' running .. (control: {}, overrides: {})",
443+
test.name, test.control, overrides
444+
);
412445

413-
let output = match std::panic::catch_unwind(|| test.run()) {
446+
let output = match std::panic::catch_unwind(|| test.run(&overrides)) {
414447
Ok(test_result) => {
415448
println!(
416449
"Test '{}' .. ok: mean = {}, std_dev = {}",
@@ -425,7 +458,7 @@ fn run_test_with_timeout(test: &'static PerformanceTest) -> Result<PerformanceTe
425458
});
426459

427460
// Todo: Need to cleanup/kill all hanging child processes
428-
let test_timeout = test.calc_timeout();
461+
let test_timeout = test.calc_timeout(&test_iterations);
429462
receiver
430463
.recv_timeout(Duration::from_secs(test_timeout))
431464
.map_err(|_| {
@@ -469,6 +502,12 @@ fn main() {
469502
.help("Report file. Standard error is used if not specified")
470503
.takes_value(true),
471504
)
505+
.arg(
506+
Arg::new("iterations")
507+
.long("iterations")
508+
.help("Override number of test iterations")
509+
.takes_value(true),
510+
)
472511
.get_matches();
473512

474513
// It seems that the tool (ethr) used for testing the virtio-net latency
@@ -497,9 +536,17 @@ fn main() {
497536

498537
init_tests();
499538

539+
let overrides = Arc::new(PerformanceTestOverrides {
540+
test_iterations: cmd_arguments
541+
.value_of("iterations")
542+
.map(|s| s.parse())
543+
.transpose()
544+
.unwrap_or_default(),
545+
});
546+
500547
for test in test_list.iter() {
501548
if test_filter.is_empty() || test_filter.iter().any(|&s| test.name.contains(s)) {
502-
match run_test_with_timeout(test) {
549+
match run_test_with_timeout(test, &overrides) {
503550
Ok(r) => {
504551
metrics_report.results.push(r);
505552
}

0 commit comments

Comments
 (0)