From e929a2d6c28687df4b491594dc84c0ae9a1dc208 Mon Sep 17 00:00:00 2001 From: Carla Kirk-Cohen Date: Tue, 24 Oct 2023 16:43:37 -0400 Subject: [PATCH] TEMP: create SimulationConfig and add log interval flag Simulation::new() is getting very long (clippy was complaining), pull config out into a struct that can be separately passed in. --- sim-cli/src/main.rs | 18 ++++++++++++----- sim-lib/src/lib.rs | 49 ++++++++++++++++++++++----------------------- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/sim-cli/src/main.rs b/sim-cli/src/main.rs index 233fdc9e..be35e759 100644 --- a/sim-cli/src/main.rs +++ b/sim-cli/src/main.rs @@ -1,7 +1,9 @@ use bitcoin::secp256k1::PublicKey; +use sim_lib::SimulationConfig; use std::collections::HashMap; use std::path::PathBuf; use std::sync::Arc; +use std::time::Duration; use tokio::sync::Mutex; use clap::Parser; @@ -46,6 +48,9 @@ struct Cli { /// Do not create an output file containing the simulations results #[clap(long, default_value_t = false)] no_results: bool, + /// The interval at which to log payment summaries, expressed in seconds. + #[clap(long, default_value_t = 60)] + log_interval: u64, } #[tokio::main] @@ -163,11 +168,14 @@ async fn main() -> anyhow::Result<()> { let sim = Simulation::new( clients, validated_activities, - cli.total_time, - cli.print_batch_size, - cli.expected_pmt_amt, - cli.capacity_multiplier, - cli.no_results, + SimulationConfig { + total_time: cli.total_time.map(|x| Duration::from_secs(x as u64)), + print_batch_size: cli.print_batch_size, + expected_payment_msat: cli.expected_pmt_amt, + activity_multiplier: cli.capacity_multiplier, + no_results: cli.no_results, + log_interval: Duration::from_secs(cli.log_interval), + }, ); let sim2 = sim.clone(); diff --git a/sim-lib/src/lib.rs b/sim-lib/src/lib.rs index 0c44e1f7..d7f7fb74 100644 --- a/sim-lib/src/lib.rs +++ b/sim-lib/src/lib.rs @@ -319,28 +319,31 @@ pub struct Simulation { // High level triggers used to manage simulation tasks and shutdown. shutdown_trigger: Trigger, shutdown_listener: Listener, + cfg: SimulationConfig, +} + +#[derive(Clone)] +pub struct SimulationConfig { // Total simulation time. The simulation will run forever if undefined. - total_time: Option, + pub total_time: Option, /// The number of activity results to batch before printing in CSV. - print_batch_size: u32, + pub print_batch_size: u32, /// The expected payment size for the network. - expected_payment_msat: u64, + pub expected_payment_msat: u64, /// The number of times that the network sends its total capacity in a month of operation when generating random /// activity. - activity_multiplier: f64, + pub activity_multiplier: f64, /// Whether we want the simulation not to produce and result file. Useful for developing, defaults to false. - no_results: bool, + pub no_results: bool, + /// The interval at which to log summary payment results. + pub log_interval: Duration, } impl Simulation { pub fn new( nodes: HashMap>>, activity: Vec, - total_time: Option, - print_batch_size: u32, - expected_payment_msat: u64, - activity_multiplier: f64, - no_results: bool, + cfg: SimulationConfig, ) -> Self { let (shutdown_trigger, shutdown_listener) = triggered::trigger(); Self { @@ -348,11 +351,7 @@ impl Simulation { activity, shutdown_trigger, shutdown_listener, - total_time: total_time.map(|x| Duration::from_secs(x as u64)), - print_batch_size, - expected_payment_msat, - activity_multiplier, - no_results, + cfg, } } @@ -432,7 +431,7 @@ impl Simulation { } pub async fn run(&self) -> Result<(), SimulationError> { - if let Some(total_time) = self.total_time { + if let Some(total_time) = self.cfg.total_time { log::info!("Running the simulation for {}s.", total_time.as_secs()); } else { log::info!("Running the simulation forever."); @@ -484,14 +483,14 @@ impl Simulation { } else { log::info!( "Generating random activity with multiplier: {}, average payment amount: {}.", - self.activity_multiplier, - self.expected_payment_msat + self.cfg.activity_multiplier, + self.cfg.expected_payment_msat ); self.dispatch_random_producers(random_activity_nodes, producer_senders, &mut tasks) .await?; } - if let Some(total_time) = self.total_time { + if let Some(total_time) = self.cfg.total_time { let t = self.shutdown_trigger.clone(); let l = self.shutdown_listener.clone(); @@ -549,15 +548,15 @@ impl Simulation { tasks.spawn(run_results_logger( listener.clone(), result_logger.clone(), - Duration::from_secs(60), + self.cfg.log_interval, )); tasks.spawn(consume_simulation_results( result_logger, results_receiver, listener, - self.print_batch_size, - self.no_results, + self.cfg.print_batch_size, + self.cfg.no_results, )); log::debug!("Simulator data collection set up."); } @@ -575,7 +574,7 @@ impl Simulation { if let Err(e) = PaymentActivityGenerator::validate_capacity( chan_capacity, - self.expected_payment_msat, + self.cfg.expected_payment_msat, ) { log::warn!("Node: {} not eligible for activity generation: {e}.", *pk); continue; @@ -674,8 +673,8 @@ impl Simulation { let node_generator = PaymentActivityGenerator::new( source_capacity, - self.expected_payment_msat, - self.activity_multiplier, + self.cfg.expected_payment_msat, + self.cfg.activity_multiplier, )?; tasks.spawn(produce_random_events(