Skip to content

Commit 31ef59e

Browse files
authored
Merge pull request #197 from bjohnson5/196-sim-results
Exposing the simulation results through public functions in sim-lib
2 parents 9e87bee + 6506b66 commit 31ef59e

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

sim-lib/src/lib.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,8 @@ pub struct Simulation {
494494
write_results: Option<WriteResults>,
495495
/// Random number generator created from fixed seed.
496496
seeded_rng: MutRng,
497+
/// Results logger that holds the simulation statistics.
498+
results: Arc<Mutex<PaymentResultLogger>>,
497499
}
498500

499501
#[derive(Clone)]
@@ -535,6 +537,7 @@ impl Simulation {
535537
activity_multiplier,
536538
write_results,
537539
seeded_rng: MutRng::new(seed),
540+
results: Arc::new(Mutex::new(PaymentResultLogger::new())),
538541
}
539542
}
540543

@@ -739,6 +742,14 @@ impl Simulation {
739742
self.shutdown_trigger.trigger()
740743
}
741744

745+
pub async fn get_total_payments(&self) -> u64 {
746+
self.results.lock().await.total_attempts()
747+
}
748+
749+
pub async fn get_success_rate(&self) -> f64 {
750+
self.results.lock().await.success_rate()
751+
}
752+
742753
/// run_data_collection starts the tasks required for the simulation to report of the results of the activity that
743754
/// it generates. The simulation should report outputs via the receiver that is passed in.
744755
fn run_data_collection(
@@ -770,7 +781,7 @@ impl Simulation {
770781
}
771782
});
772783

773-
let result_logger = Arc::new(Mutex::new(PaymentResultLogger::new()));
784+
let result_logger = self.results.clone();
774785

775786
let result_logger_clone = result_logger.clone();
776787
let result_logger_listener = listener.clone();
@@ -1238,17 +1249,24 @@ impl PaymentResultLogger {
12381249

12391250
self.total_sent += details.amount_msat;
12401251
}
1252+
1253+
fn total_attempts(&self) -> u64 {
1254+
self.success_payment + self.failed_payment
1255+
}
1256+
1257+
fn success_rate(&self) -> f64 {
1258+
(self.success_payment as f64 / self.total_attempts() as f64) * 100.0
1259+
}
12411260
}
12421261

12431262
impl Display for PaymentResultLogger {
12441263
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1245-
let total_payments = self.success_payment + self.failed_payment;
12461264
write!(
12471265
f,
12481266
"Processed {} payments sending {} msat total with {:.2}% success rate.",
1249-
total_payments,
1267+
self.total_attempts(),
12501268
self.total_sent,
1251-
(self.success_payment as f64 / total_payments as f64) * 100.0
1269+
self.success_rate()
12521270
)
12531271
}
12541272
}

0 commit comments

Comments
 (0)