Skip to content

Commit

Permalink
TEMP: create SimulationConfig and add log interval flag
Browse files Browse the repository at this point in the history
Simulation::new() is getting very long (clippy was complaining), pull
config out into a struct that can be separately passed in.
  • Loading branch information
carlaKC committed Oct 24, 2023
1 parent f097084 commit e929a2d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
18 changes: 13 additions & 5 deletions sim-cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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();

Expand Down
49 changes: 24 additions & 25 deletions sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,40 +319,39 @@ 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<time::Duration>,
pub total_time: Option<time::Duration>,
/// 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<PublicKey, Arc<Mutex<dyn LightningNode + Send>>>,
activity: Vec<ActivityDefinition>,
total_time: Option<u32>,
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 {
nodes,
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,
}
}

Expand Down Expand Up @@ -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.");
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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.");
}
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit e929a2d

Please sign in to comment.