From 0376a67d38874b711b0505692ef0a2dedf4f5853 Mon Sep 17 00:00:00 2001 From: Carla Kirk-Cohen Date: Tue, 17 Oct 2023 10:48:01 -0400 Subject: [PATCH 1/2] sim-lib/refactor: pass report logger into results method We're going to need to start the report logger in its own task in the commit that follows (so that we can log every minute). This preparatory commit refactors creations of a PaymentResultLogger so that we can easily spin up its run task in our existing join set. --- sim-lib/src/lib.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/sim-lib/src/lib.rs b/sim-lib/src/lib.rs index 3cfd94f1..e60dd266 100644 --- a/sim-lib/src/lib.rs +++ b/sim-lib/src/lib.rs @@ -549,7 +549,10 @@ impl Simulation { listener.clone(), )); + let result_logger = Arc::new(Mutex::new(PaymentResultLogger::new())); + tasks.spawn(consume_simulation_results( + result_logger, results_receiver, listener, self.print_batch_size, @@ -874,6 +877,7 @@ async fn produce_random_events>, receiver: Receiver<(Payment, PaymentResult)>, listener: Listener, print_batch_size: u32, @@ -881,7 +885,9 @@ async fn consume_simulation_results( ) { log::debug!("Simulation results consumer started."); - if let Err(e) = write_payment_results(receiver, listener, print_batch_size, no_results).await { + if let Err(e) = + write_payment_results(logger, receiver, listener, print_batch_size, no_results).await + { log::error!("Error while reporting payment results: {:?}.", e); } @@ -889,6 +895,7 @@ async fn consume_simulation_results( } async fn write_payment_results( + logger: Arc>, mut receiver: Receiver<(Payment, PaymentResult)>, listener: Listener, print_batch_size: u32, @@ -906,8 +913,7 @@ async fn write_payment_results( None }; - let mut result_logger = PaymentResultLogger::new(); - let mut counter = 0; + let mut counter = 1; loop { tokio::select! { biased; @@ -918,7 +924,7 @@ async fn write_payment_results( payment_report = receiver.recv() => { match payment_report { Some((details, result)) => { - result_logger.report_result(&details, &result); + logger.lock().await.report_result(&details, &result); log::trace!("Resolved dispatched payment: {} with: {}.", details, result); if let Some(ref mut w) = writer { From 71d44cb8b5499e47cf12859ae51ff6efc2930c03 Mon Sep 17 00:00:00 2001 From: Carla Kirk-Cohen Date: Tue, 17 Oct 2023 10:44:21 -0400 Subject: [PATCH 2/2] sim-lib: report results on time interval for more consistent logging --- sim-lib/src/lib.rs | 57 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/sim-lib/src/lib.rs b/sim-lib/src/lib.rs index e60dd266..d18e7cd7 100644 --- a/sim-lib/src/lib.rs +++ b/sim-lib/src/lib.rs @@ -551,6 +551,12 @@ impl Simulation { let result_logger = Arc::new(Mutex::new(PaymentResultLogger::new())); + tasks.spawn(run_results_logger( + listener.clone(), + result_logger.clone(), + Duration::from_secs(60), + )); + tasks.spawn(consume_simulation_results( result_logger, results_receiver, @@ -946,22 +952,17 @@ async fn write_payment_results( } } -/// PaymentResultLogger is an aggregate logger that will report on a summary of the payments that have been reported -/// to it at regular intervals (defined by the log_interval it is created with). +/// PaymentResultLogger is an aggregate logger that will report on a summary of the payments that have been reported. #[derive(Default)] struct PaymentResultLogger { success_payment: u64, failed_payment: u64, total_sent: u64, - call_count: u8, - log_interval: u8, } impl PaymentResultLogger { fn new() -> Self { PaymentResultLogger { - // TODO: set the interval at which we log based on the number of payment we're expecting to log. - log_interval: 10, ..Default::default() } } @@ -973,18 +974,44 @@ impl PaymentResultLogger { } self.total_sent += details.amount_msat; - self.call_count += 1; + } +} - if self.call_count % self.log_interval == 0 || self.call_count == 0 { - let total_payments = self.success_payment + self.failed_payment; - log::info!( - "Processed {} payments sending {} msat total with {}% success rate.", - total_payments, - self.total_sent, - (self.success_payment * 100 / total_payments) - ); +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_sent, + (self.success_payment as f64 / total_payments as f64) * 100.0 + ) + } +} + +async fn run_results_logger( + listener: Listener, + logger: Arc>, + interval: Duration, +) { + log::debug!("Results logger started."); + log::info!("Summary of results will be reported every {:?}.", interval); + + loop { + select! { + biased; + _ = listener.clone() => { + break + } + + _ = time::sleep(interval) => { + log::info!("{}", logger.lock().await) + } } } + + log::debug!("Results logger stopped.") } /// produce_results is responsible for receiving the outputs of events that the simulator has taken and