From 6506b66588f49fb9e51fc166a252ff5c53fe0e63 Mon Sep 17 00:00:00 2001 From: Blake Johnson Date: Tue, 13 Aug 2024 13:51:04 +0000 Subject: [PATCH] Exposing the simulation results through public functions in sim-lib --- sim-lib/src/lib.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/sim-lib/src/lib.rs b/sim-lib/src/lib.rs index 2d20568b..3e50e682 100644 --- a/sim-lib/src/lib.rs +++ b/sim-lib/src/lib.rs @@ -494,6 +494,8 @@ pub struct Simulation { write_results: Option, /// Random number generator created from fixed seed. seeded_rng: MutRng, + /// Results logger that holds the simulation statistics. + results: Arc>, } #[derive(Clone)] @@ -535,6 +537,7 @@ impl Simulation { activity_multiplier, write_results, seeded_rng: MutRng::new(seed), + results: Arc::new(Mutex::new(PaymentResultLogger::new())), } } @@ -739,6 +742,14 @@ impl Simulation { self.shutdown_trigger.trigger() } + pub async fn get_total_payments(&self) -> u64 { + self.results.lock().await.total_attempts() + } + + pub async fn get_success_rate(&self) -> f64 { + self.results.lock().await.success_rate() + } + /// run_data_collection starts the tasks required for the simulation to report of the results of the activity that /// it generates. The simulation should report outputs via the receiver that is passed in. fn run_data_collection( @@ -770,7 +781,7 @@ impl Simulation { } }); - let result_logger = Arc::new(Mutex::new(PaymentResultLogger::new())); + let result_logger = self.results.clone(); let result_logger_clone = result_logger.clone(); let result_logger_listener = listener.clone(); @@ -1238,17 +1249,24 @@ impl PaymentResultLogger { self.total_sent += details.amount_msat; } + + fn total_attempts(&self) -> u64 { + self.success_payment + self.failed_payment + } + + fn success_rate(&self) -> f64 { + (self.success_payment as f64 / self.total_attempts() as f64) * 100.0 + } } impl Display for PaymentResultLogger { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - let total_payments = self.success_payment + self.failed_payment; write!( f, "Processed {} payments sending {} msat total with {:.2}% success rate.", - total_payments, + self.total_attempts(), self.total_sent, - (self.success_payment as f64 / total_payments as f64) * 100.0 + self.success_rate() ) } }