Skip to content

Commit 71d44cb

Browse files
committed
sim-lib: report results on time interval for more consistent logging
1 parent 0376a67 commit 71d44cb

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

sim-lib/src/lib.rs

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,12 @@ impl Simulation {
551551

552552
let result_logger = Arc::new(Mutex::new(PaymentResultLogger::new()));
553553

554+
tasks.spawn(run_results_logger(
555+
listener.clone(),
556+
result_logger.clone(),
557+
Duration::from_secs(60),
558+
));
559+
554560
tasks.spawn(consume_simulation_results(
555561
result_logger,
556562
results_receiver,
@@ -946,22 +952,17 @@ async fn write_payment_results(
946952
}
947953
}
948954

949-
/// PaymentResultLogger is an aggregate logger that will report on a summary of the payments that have been reported
950-
/// to it at regular intervals (defined by the log_interval it is created with).
955+
/// PaymentResultLogger is an aggregate logger that will report on a summary of the payments that have been reported.
951956
#[derive(Default)]
952957
struct PaymentResultLogger {
953958
success_payment: u64,
954959
failed_payment: u64,
955960
total_sent: u64,
956-
call_count: u8,
957-
log_interval: u8,
958961
}
959962

960963
impl PaymentResultLogger {
961964
fn new() -> Self {
962965
PaymentResultLogger {
963-
// TODO: set the interval at which we log based on the number of payment we're expecting to log.
964-
log_interval: 10,
965966
..Default::default()
966967
}
967968
}
@@ -973,18 +974,44 @@ impl PaymentResultLogger {
973974
}
974975

975976
self.total_sent += details.amount_msat;
976-
self.call_count += 1;
977+
}
978+
}
977979

978-
if self.call_count % self.log_interval == 0 || self.call_count == 0 {
979-
let total_payments = self.success_payment + self.failed_payment;
980-
log::info!(
981-
"Processed {} payments sending {} msat total with {}% success rate.",
982-
total_payments,
983-
self.total_sent,
984-
(self.success_payment * 100 / total_payments)
985-
);
980+
impl Display for PaymentResultLogger {
981+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
982+
let total_payments = self.success_payment + self.failed_payment;
983+
write!(
984+
f,
985+
"Processed {} payments sending {} msat total with {:.2}% success rate.",
986+
total_payments,
987+
self.total_sent,
988+
(self.success_payment as f64 / total_payments as f64) * 100.0
989+
)
990+
}
991+
}
992+
993+
async fn run_results_logger(
994+
listener: Listener,
995+
logger: Arc<Mutex<PaymentResultLogger>>,
996+
interval: Duration,
997+
) {
998+
log::debug!("Results logger started.");
999+
log::info!("Summary of results will be reported every {:?}.", interval);
1000+
1001+
loop {
1002+
select! {
1003+
biased;
1004+
_ = listener.clone() => {
1005+
break
1006+
}
1007+
1008+
_ = time::sleep(interval) => {
1009+
log::info!("{}", logger.lock().await)
1010+
}
9861011
}
9871012
}
1013+
1014+
log::debug!("Results logger stopped.")
9881015
}
9891016

9901017
/// produce_results is responsible for receiving the outputs of events that the simulator has taken and

0 commit comments

Comments
 (0)