From 025447e8cc863f89e38165bc860b1da153cd1180 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Thu, 19 May 2022 16:44:17 +0100 Subject: [PATCH] performance-metrics: Add ability to override test iterations Signed-off-by: Rob Bradford --- performance-metrics/src/main.rs | 67 ++++++++++++++++++++++++++++----- 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/performance-metrics/src/main.rs b/performance-metrics/src/main.rs index 382be34fe..0065e1534 100644 --- a/performance-metrics/src/main.rs +++ b/performance-metrics/src/main.rs @@ -13,7 +13,13 @@ mod performance_tests; use clap::{Arg, Command as ClapCommand}; use performance_tests::*; use serde::{Deserialize, Serialize}; -use std::{env, fmt, process::Command, sync::mpsc::channel, thread, time::Duration}; +use std::{ + env, fmt, + process::Command, + sync::{mpsc::channel, Arc}, + thread, + time::Duration, +}; use thiserror::Error; #[derive(Error, Debug)] @@ -84,6 +90,21 @@ impl Default for MetricsReport { } } +#[derive(Default)] +pub struct PerformanceTestOverrides { + test_iterations: Option, +} + +impl fmt::Display for PerformanceTestOverrides { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if let Some(test_iterations) = self.test_iterations { + write!(f, "test_iterations = {}", test_iterations)?; + } + + Ok(()) + } +} + pub struct PerformanceTestControl { test_timeout: u32, test_iterations: u32, @@ -142,9 +163,12 @@ struct PerformanceTest { } impl PerformanceTest { - pub fn run(&self) -> PerformanceTestResult { + pub fn run(&self, overrides: &PerformanceTestOverrides) -> PerformanceTestResult { let mut metrics = Vec::new(); - for _ in 0..self.control.test_iterations { + for _ in 0..overrides + .test_iterations + .unwrap_or(self.control.test_iterations) + { metrics.push((self.func_ptr)(&self.control)); } @@ -164,8 +188,9 @@ impl PerformanceTest { // Calculate the timeout for each test // Note: To cover the setup/cleanup time, 20s is added for each iteration of the test - pub fn calc_timeout(&self) -> u64 { - ((self.control.test_timeout + 20) * self.control.test_iterations) as u64 + pub fn calc_timeout(&self, test_iterations: &Option) -> u64 { + ((self.control.test_timeout + 20) * test_iterations.unwrap_or(self.control.test_iterations)) + as u64 } } @@ -405,12 +430,20 @@ const TEST_LIST: [PerformanceTest; 17] = [ }, ]; -fn run_test_with_timeout(test: &'static PerformanceTest) -> Result { +fn run_test_with_timeout( + test: &'static PerformanceTest, + overrides: &Arc, +) -> Result { let (sender, receiver) = channel::>(); + let test_iterations = overrides.test_iterations; + let overrides = overrides.clone(); thread::spawn(move || { - println!("Test '{}' running .. ({})", test.name, test.control); + println!( + "Test '{}' running .. (control: {}, overrides: {})", + test.name, test.control, overrides + ); - let output = match std::panic::catch_unwind(|| test.run()) { + let output = match std::panic::catch_unwind(|| test.run(&overrides)) { Ok(test_result) => { println!( "Test '{}' .. ok: mean = {}, std_dev = {}", @@ -425,7 +458,7 @@ fn run_test_with_timeout(test: &'static PerformanceTest) -> Result { metrics_report.results.push(r); }