Skip to content

Commit

Permalink
sim-all: Allow running the simulator without it writing on disk
Browse files Browse the repository at this point in the history
It is rather annoying to run the simulator and having to delete the output files
when we are doing testing/dev. This allows us to do so without the file being created.

I've approached this by making the simulator to write the results to "/dev/null" to
minimized the code diff, but I'm happy to change the approach if this feels to hacky
(it is worth to keep in mind this is for dev only).
  • Loading branch information
sr-gi committed Oct 19, 2023
1 parent ff9f918 commit 5a1b073
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions sim-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ struct Cli {
expected_pmt_amt: Option<u64>,
#[clap(long, short)]
capacity_multiplier: Option<f64>,
// DEV-ONLY args, set hide = true so they are not show in help
/// Do not an output file including the simulations results
#[clap(long, short, hide = true, default_value_t = false)]
no_results: bool,
}

#[tokio::main]
Expand Down Expand Up @@ -149,6 +153,7 @@ async fn main() -> anyhow::Result<()> {
cli.print_batch_size,
cli.expected_pmt_amt,
cli.capacity_multiplier,
cli.no_results,
);
let sim2 = sim.clone();

Expand Down
30 changes: 20 additions & 10 deletions sim-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ pub struct Simulation {
/// The number of times that the network sends its total capacity in a month of operation when generating random
/// activity.
activity_multiplier: f64,
/// Wether we want the simulation not to produce and result file. Useful for developing, defaults to false.
no_results: bool,
}

const DEFAULT_PRINT_BATCH_SIZE: u32 = 500;
Expand All @@ -346,6 +348,7 @@ impl Simulation {
print_batch_size: Option<u32>,
expected_payment_amount: Option<u64>,
activity_multiplier: Option<f64>,
no_results: bool,
) -> Self {
let (shutdown_trigger, shutdown_listener) = triggered::trigger();
Self {
Expand All @@ -357,6 +360,7 @@ impl Simulation {
print_batch_size: print_batch_size.unwrap_or(DEFAULT_PRINT_BATCH_SIZE),
expected_payment_msat: expected_payment_amount.unwrap_or(EXPECTED_PAYMENT_AMOUNT),
activity_multiplier: activity_multiplier.unwrap_or(ACTIVITY_MULTIPLIER),
no_results,
}
}

Expand Down Expand Up @@ -536,7 +540,6 @@ impl Simulation {
tasks: &mut JoinSet<()>,
) {
let listener = self.shutdown_listener.clone();
let print_batch_size = self.print_batch_size;
log::debug!("Setting up simulator data collection.");

// Create a sender/receiver pair that will be used to report final results of simulation.
Expand All @@ -552,7 +555,8 @@ impl Simulation {
tasks.spawn(consume_simulation_results(
results_receiver,
listener,
print_batch_size,
self.print_batch_size,
self.no_results,
));
log::debug!("Simulator data collection set up.");
}
Expand Down Expand Up @@ -872,10 +876,11 @@ async fn consume_simulation_results(
receiver: Receiver<(Payment, PaymentResult)>,
listener: Listener,
print_batch_size: u32,
no_results: bool,
) {
log::debug!("Simulation results consumer started.");

if let Err(e) = write_payment_results(receiver, listener, print_batch_size).await {
if let Err(e) = write_payment_results(receiver, listener, print_batch_size, no_results).await {
log::error!("Error while reporting payment results: {:?}.", e);
}

Expand All @@ -886,14 +891,19 @@ async fn write_payment_results(
mut receiver: Receiver<(Payment, PaymentResult)>,
listener: Listener,
print_batch_size: u32,
no_results: bool,
) -> Result<(), SimulationError> {
let mut writer = WriterBuilder::new().from_path(format!(
"simulation_{:?}.csv",
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs()
))?;
let mut writer = if no_results {
WriterBuilder::new().from_path("/dev/null")?
} else {
WriterBuilder::new().from_path(format!(
"simulation_{:?}.csv",
SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs()
))?
};

let mut result_logger = PaymentResultLogger::new();

Expand Down

0 comments on commit 5a1b073

Please sign in to comment.