Skip to content

Commit

Permalink
refactor: make log interval configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
enigbe committed Mar 11, 2024
1 parent 590d834 commit 2f816b6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 39 deletions.
4 changes: 2 additions & 2 deletions sim-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub const DEFAULT_LOG_LEVEL: &str = "info";
pub const DEFAULT_NO_RESULTS: bool = false;

/// Default log interval
pub const DEFAULT_LOG_INTERVAL: u32 = 60;
pub const DEFAULT_LOG_INTERVAL: u64 = 60;

/// Configuration header
pub const CONFIG_HEADER: &str = "simln.conf";
Expand Down Expand Up @@ -145,7 +145,7 @@ pub struct Cli {
default_value_t = DEFAULT_LOG_INTERVAL,
value_parser = clap::builder::RangedU64ValueParser::<u32>::new().range(1..u32::MAX as u64)
)]
log_interval: u32,
log_interval: u64,
}

/// Implementation of Cli with default values
Expand Down
27 changes: 3 additions & 24 deletions sim-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use anyhow::anyhow;
use log::LevelFilter;
use sim_lib::{
cln::ClnNode, lnd::LndNode, ActivityDefinition, LightningError, LightningNode, NodeConnection, NodeId, SimParams,
Simulation, WriteResults,
Simulation,
};

mod cli;
Expand All @@ -35,7 +35,7 @@ async fn main() -> anyhow::Result<()> {
.build(),
);

let sim_path = read_sim_path(opts.data_dir.clone(), opts.sim_file).await?;
let sim_path = read_sim_path(opts.data_dir.clone(), opts.sim_file.clone()).await?;
let SimParams { nodes, activity } = serde_json::from_str(&std::fs::read_to_string(sim_path)?).map_err(|e| {
anyhow!(
"Nodes or activities not parsed from simulation file (line {}, col {}).",
Expand Down Expand Up @@ -135,23 +135,7 @@ async fn main() -> anyhow::Result<()> {
});
}

let write_results = if !opts.no_results {
Some(WriteResults {
results_dir: mkdir(opts.data_dir.join("results")).await?,
batch_size: opts.print_batch_size,
})
} else {
None
};

let sim = Simulation::new(
clients,
validated_activities,
opts.total_time,
opts.expected_pmt_amt,
opts.capacity_multiplier,
write_results,
);
let sim = Simulation::new(clients, validated_activities, opts).await?;
let sim2 = sim.clone();

ctrlc::set_handler(move || {
Expand Down Expand Up @@ -207,8 +191,3 @@ async fn select_sim_file(data_dir: PathBuf) -> anyhow::Result<PathBuf> {

Ok(data_dir.join(sim_files[selection].clone()))
}

async fn mkdir(dir: PathBuf) -> anyhow::Result<PathBuf> {
tokio::fs::create_dir_all(&dir).await?;
Ok(dir)
}
43 changes: 30 additions & 13 deletions sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@ pub struct Simulation {
activity_multiplier: f64,
/// Configurations for printing results to CSV. Results are not written if this option is None.
write_results: Option<WriteResults>,
/// Duration measuring how often the summary of results are logged.
log_interval: u64,
}

#[derive(Clone)]
Expand All @@ -356,25 +358,35 @@ struct ExecutorKit {
}

impl Simulation {
pub fn new(
pub async fn new(
nodes: HashMap<PublicKey, Arc<Mutex<dyn LightningNode>>>,
activity: Vec<ActivityDefinition>,
total_time: Option<u32>,
expected_payment_msat: u64,
activity_multiplier: f64,
write_results: Option<WriteResults>,
) -> Self {
config_options: SimulationConfig,
) -> Result<Self, SimulationError> {
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
Self {
let write_results = if !config_options.no_results {
Some(WriteResults {
results_dir: mkdir(config_options.data_dir.join("results")).await.map_err(|e| {
log::error!("Failed to create results directory. Error: {e}.");
SimulationError::FileError
})?,
batch_size: config_options.print_batch_size,
})
} else {
None
};

Ok(Self {
nodes,
activity,
shutdown_trigger,
shutdown_listener,
total_time: total_time.map(|x| Duration::from_secs(x as u64)),
expected_payment_msat,
activity_multiplier,
total_time: config_options.total_time.map(|x| Duration::from_secs(x as u64)),
expected_payment_msat: config_options.expected_pmt_amt,
activity_multiplier: config_options.capacity_multiplier,
write_results,
}
log_interval: config_options.log_interval,
})
}

/// validate_activity validates that the user-provided activity description is achievable for the network that
Expand Down Expand Up @@ -544,7 +556,7 @@ impl Simulation {
tasks.spawn(run_results_logger(
listener.clone(),
result_logger.clone(),
Duration::from_secs(60),
Duration::from_secs(self.log_interval),
));

tasks.spawn(consume_simulation_results(
Expand Down Expand Up @@ -1050,5 +1062,10 @@ pub struct SimulationConfig {
pub print_batch_size: u32,
pub data_dir: PathBuf,
pub sim_file: PathBuf,
pub log_interval: u32,
pub log_interval: u64,
}

async fn mkdir(dir: PathBuf) -> anyhow::Result<PathBuf> {
tokio::fs::create_dir_all(&dir).await?;
Ok(dir)
}

0 comments on commit 2f816b6

Please sign in to comment.