Skip to content

Commit 3376c4c

Browse files
committed
sim-rs: add tx-start-time and tx-stop-time parameters
1 parent da350dd commit 3376c4c

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed

data/simulation/config.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ export interface Config {
6767
"tx-validation-cpu-time-ms": number;
6868
/** Only supported by Rust simulation. */
6969
"tx-max-size-bytes": bigint;
70+
/**
71+
* When the first transaction should appear.
72+
* Only supported by Rust simulation. */
73+
"tx-start-time"?: number | null;
74+
/**
75+
* The cutoff time after which transactions should not appear.
76+
* Only supported by Rust simulation. */
77+
"tx-stop-time"?: number | null;
7078

7179
// Ranking Block Configuration
7280
"rb-generation-probability": number;

data/simulation/config.schema.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,14 @@
351351
"$ref": "#/definitions/Distribution",
352352
"description": "Only supported by Rust simulation."
353353
},
354+
"tx-start-time": {
355+
"description": "When the first transaction should appear.\nOnly supported by Rust simulation.",
356+
"type": "number"
357+
},
358+
"tx-stop-time": {
359+
"description": "The cutoff time after which transactions should not appear.\nOnly supported by Rust simulation.",
360+
"type": "number"
361+
},
354362
"tx-validation-cpu-time-ms": {
355363
"description": "Only supported by Rust simulation.",
356364
"type": "number"

sim-rs/sim-cli/src/events.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ fn compute_stats<Iter: IntoIterator<Item = f64>>(data: Iter) -> Stats {
620620
}
621621
}
622622

623+
#[allow(clippy::large_enum_variant)]
623624
enum OutputTarget {
624625
AggregatedEventStream {
625626
aggregation: TraceAggregator,

sim-rs/sim-core/src/config.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
use anyhow::{anyhow, bail, Result};
99
use serde::{Deserialize, Serialize};
1010

11-
use crate::{model::TransactionId, probability::FloatDistribution};
11+
use crate::{clock::Timestamp, model::TransactionId, probability::FloatDistribution};
1212

1313
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
1414
pub struct NodeId(usize);
@@ -70,6 +70,8 @@ pub struct RawParameters {
7070
pub tx_sharded_percentage: f64,
7171
pub tx_validation_cpu_time_ms: f64,
7272
pub tx_max_size_bytes: u64,
73+
pub tx_start_time: Option<f64>,
74+
pub tx_stop_time: Option<f64>,
7375

7476
// Ranking block configuration
7577
pub rb_generation_probability: f64,
@@ -377,6 +379,12 @@ impl TransactionConfig {
377379
sharded_percentage: params.tx_sharded_percentage,
378380
frequency_ms: params.tx_generation_distribution.into(),
379381
size_bytes: params.tx_size_bytes_distribution.into(),
382+
start_time: params
383+
.tx_start_time
384+
.map(|t| Timestamp::zero() + Duration::from_secs_f64(t)),
385+
stop_time: params
386+
.tx_stop_time
387+
.map(|t| Timestamp::zero() + Duration::from_secs_f64(t)),
380388
})
381389
} else {
382390
Self::Mock(MockTransactionConfig {
@@ -394,6 +402,8 @@ pub(crate) struct RealTransactionConfig {
394402
pub sharded_percentage: f64,
395403
pub frequency_ms: FloatDistribution,
396404
pub size_bytes: FloatDistribution,
405+
pub start_time: Option<Timestamp>,
406+
pub stop_time: Option<Timestamp>,
397407
}
398408

399409
#[derive(Debug, Clone)]

sim-rs/sim-core/src/sim/tx.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ impl TransactionProducer {
4747
let mut next_tx_id = 0;
4848
let mut next_tx_at = Timestamp::zero();
4949
let mut rng = &mut self.rng;
50+
51+
if let Some(start) = config.start_time {
52+
self.clock.wait_until(start).await;
53+
next_tx_at = start;
54+
};
55+
5056
loop {
5157
let id = TransactionId::new(next_tx_id);
5258
let shard = rng
@@ -67,7 +73,12 @@ impl TransactionProducer {
6773
let millis_until_tx = config.frequency_ms.sample(&mut rng) as u64;
6874
next_tx_at += Duration::from_millis(millis_until_tx);
6975

70-
self.clock.wait_until(next_tx_at).await;
76+
if config.stop_time.is_some_and(|t| next_tx_at > t) {
77+
self.clock.wait_forever().await;
78+
return Ok(());
79+
} else {
80+
self.clock.wait_until(next_tx_at).await;
81+
}
7182
}
7283
}
7384
}

0 commit comments

Comments
 (0)