Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing the simulation results through public functions in sim-lib #197

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ pub struct Simulation {
write_results: Option<WriteResults>,
/// Random number generator created from fixed seed.
seeded_rng: MutRng,
/// Results logger that holds the simulation statistics.
results: Arc<Mutex<PaymentResultLogger>>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -535,6 +537,7 @@ impl Simulation {
activity_multiplier,
write_results,
seeded_rng: MutRng::new(seed),
results: Arc::new(Mutex::new(PaymentResultLogger::new())),
}
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -770,7 +781,7 @@ impl Simulation {
}
});

let result_logger = Arc::new(Mutex::new(PaymentResultLogger::new()));
let result_logger = self.results.clone();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need both let result_logger = self.results.clone(); and let result_logger_clone = result_logger.clone(); below? Seems like we could reduce to a single clone?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. I think you need a results logger in the run_results_logger spawned task and the consume_simulation_results spawned task as well as the main simulation thread. So the Arc should have a reference count of 3? Which means you need to clone it twice. Am I understanding that correctly? Very possible I am missing something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope you're totally right - missed consume_simulation_results below!


let result_logger_clone = result_logger.clone();
let result_logger_listener = listener.clone();
Expand Down Expand Up @@ -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()
)
}
}
Expand Down