Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions simulation/amaru-sim/src/simulator/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,22 +301,22 @@ pub fn generate_inputs<R: Rng>(rng: &mut R, file_path: &PathBuf) -> Result<Vec<C
}

pub fn generate_arrival_times<R: Rng>(
rng: &mut R,
start_time: Instant,
mean_millis: f64,
length: usize,
) -> Vec<Instant> {
let mut time: Instant = start_time;
let mut arrival_times = Vec::new();
) -> impl Fn(usize, &mut R) -> Vec<Instant> {
move |size, rng| {
let mut time: Instant = start_time;
let mut arrival_times = Vec::new();

let exp = Exp::new(1.0 / mean_millis).unwrap_or_else(|err| panic!("{}", err));
for _ in 0..length {
arrival_times.push(time);
let delay = exp.sample(rng);
let delay_ms = delay.ceil().min(u64::MAX as f64) as u64;
time = time + Duration::from_millis(delay_ms);
let exp = Exp::new(1.0 / mean_millis).unwrap_or_else(|err| panic!("{}", err));
for _ in 0..size {
arrival_times.push(time);
let delay = exp.sample(rng);
let delay_ms = delay.ceil().min(u64::MAX as f64) as u64;
time = time + Duration::from_millis(delay_ms);
}
arrival_times
}
arrival_times
}

pub fn generate_entries<R: Rng>(
Expand All @@ -331,7 +331,7 @@ pub fn generate_entries<R: Rng>(
let messages =
generate_inputs(rng, file_path).expect("Failed to generate inputs from chain file");
let arrival_times =
generate_arrival_times(rng, start_time, mean_millis, messages.len());
generate_arrival_times(start_time, mean_millis)(messages.len(), rng);
entries.extend(
messages
.into_iter()
Expand All @@ -354,10 +354,9 @@ pub fn generate_entries<R: Rng>(
}

pub fn generate_vec<A>(
size: usize,
generator: impl Fn(&mut StdRng) -> A,
) -> impl Fn(&mut StdRng) -> Vec<A> {
move |rng| {
) -> impl Fn(usize, &mut StdRng) -> Vec<A> {
move |size, rng| {
let mut result = Vec::<A>::with_capacity(size);
for _ in 0..size {
result.push(generator(rng));
Expand All @@ -378,13 +377,14 @@ pub fn generate_u8(low: u8, high: u8) -> impl Fn(&mut StdRng) -> u8 {
}

pub fn generate_zip_with<A: Copy, B: Copy, C>(
generator1: impl Fn(&mut StdRng) -> Vec<A>,
generator2: impl Fn(&mut StdRng) -> Vec<B>,
size: usize,
generator1: impl Fn(usize, &mut StdRng) -> Vec<A>,
generator2: impl Fn(usize, &mut StdRng) -> Vec<B>,
f: impl Fn(A, B) -> C,
) -> impl Fn(&mut StdRng) -> Vec<C> {
move |rng| {
let xs = generator1(rng);
let ys = generator2(rng);
let xs = generator1(size, rng);
let ys = generator2(size, rng);
assert_eq!(xs.len(), ys.len());
xs.into_iter().zip(ys).map(|(x, y)| f(x, y)).collect()
}
Expand Down Expand Up @@ -1100,7 +1100,7 @@ mod test {
let seed = 1;
let mut rng = StdRng::seed_from_u64(seed);
let result =
generate_arrival_times(&mut rng, Instant::at_offset(Duration::new(0, 0)), 200.0, 5);
generate_arrival_times(Instant::at_offset(Duration::new(0, 0)), 200.0)(5, &mut rng);
assert_eq!(
result,
vec![
Expand Down
2 changes: 1 addition & 1 deletion simulation/amaru-sim/src/simulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ pub fn run(rt: tokio::runtime::Runtime, args: Args) {

fn chain_property(
chain_data_path: &PathBuf,
) -> impl Fn(History<ChainSyncMessage>) -> Result<(), String> + use<'_> {
) -> impl Fn(&History<ChainSyncMessage>) -> Result<(), String> + use<'_> {
move |history| {
match history.0.last() {
None => Err("impossible, no last entry in history".to_string()),
Expand Down
2 changes: 1 addition & 1 deletion simulation/amaru-sim/src/simulator/shrink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub fn shrink<A: Debug + Clone, B: Debug>(
number_of_shrinks += 1;
last_error = result;
input = complement;
n = n.max(2) - 1;
n = (n - 1).max(2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't n become < 1 due to the !some_complement_is_failing branch below?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No? n is initial 2 and while input.len() >= 2 we potentially update it, in the branch you mention, to n = (n * 2).min(input.len()).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! My bad; I missed the while input.len() >= 2 🤦

some_complement_is_failing = true;
break;
}
Expand Down
75 changes: 30 additions & 45 deletions simulation/amaru-sim/src/simulator/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ impl<Msg> Drop for World<Msg> {
fn run_test<Msg: Debug + PartialEq + Clone, F: Fn() -> NodeHandle<Msg>>(
number_of_nodes: u8,
spawn: F,
property: impl Fn(History<Msg>) -> Result<(), String>,
property: impl Fn(&History<Msg>) -> Result<(), String>,
) -> impl Fn(&[Reverse<Entry<Msg>>]) -> (History<Msg>, Result<(), String>) {
move |entries| {
let node_handles: Vec<_> = (1..=number_of_nodes)
Expand All @@ -266,7 +266,7 @@ fn run_test<Msg: Debug + PartialEq + Clone, F: Fn() -> NodeHandle<Msg>>(
match world.run_world() {
Ok(history) => {
let history = History(history.to_vec());
let result = property(history.clone());
let result = property(&history);
(history, result)
}
Err((reason, history)) => (History(history.to_vec()), Err(reason)),
Expand All @@ -278,7 +278,7 @@ pub fn simulate<Msg, F>(
config: SimulateConfig,
spawn: F,
generator: impl Fn(&mut StdRng) -> Vec<Reverse<Entry<Msg>>>,
property: impl Fn(History<Msg>) -> Result<(), String>,
property: impl Fn(&History<Msg>) -> Result<(), String>,
trace_buffer: Arc<parking_lot::Mutex<TraceBuffer>>,
persist_on_success: bool,
) where
Expand Down Expand Up @@ -421,7 +421,7 @@ mod tests {
use std::fs;

use crate::simulator::generate::{
generate_arrival_times, generate_u8, generate_u8_then, generate_vec, generate_zip_with,
generate_arrival_times, generate_u8, generate_vec, generate_zip_with,
};

use super::*;
Expand Down Expand Up @@ -483,11 +483,28 @@ mod tests {

pure_stage_node_handle(rx, stage.without_state(), running).unwrap()
};
simulate(
SimulateConfig {
number_of_tests,
seed,
number_of_nodes,
disable_shrinking: false,
},
spawn,
echo_generator,
echo_property,
TraceBuffer::new_shared(0, 0),
false,
)
}

fn echo_generator(rng: &mut StdRng) -> Vec<Reverse<Entry<EchoMessage>>> {
let now = Instant::at_offset(Duration::from_secs(0));
let size = 20;
let generator = generate_zip_with(
generate_vec(size, generate_u8(0, 128)),
|rng| generate_arrival_times(rng, now, 200.0, size),
let messages = generate_zip_with(
size,
generate_vec(generate_u8(0, 128)),
generate_arrival_times(now, 200.0),
|msg, arrival_time| {
Reverse(Entry {
arrival_time,
Expand All @@ -501,27 +518,12 @@ mod tests {
},
})
},
);

simulate(
SimulateConfig {
number_of_tests,
seed,
number_of_nodes,
disable_shrinking: false,
},
spawn,
generator,
ECHO_PROPERTY,
TraceBuffer::new_shared(0, 0),
false,
)
)(rng);
messages
}

// TODO: Take response time into account.
const ECHO_PROPERTY: fn(History<EchoMessage>) -> Result<(), String> = |history: History<
EchoMessage,
>| {
fn echo_property(history: &History<EchoMessage>) -> Result<(), String> {
for (index, msg) in history
.0
.iter()
Expand All @@ -543,7 +545,7 @@ mod tests {
}
}
Ok(())
};
}

// This shows how we can test external binaries. The test is disabled because building and
// locating a binary on CI, across all platforms, is annoying.
Expand All @@ -557,23 +559,6 @@ mod tests {
let spawn: fn() -> NodeHandle<EchoMessage> = || {
pipe_node_handle(Path::new("../../target/debug/echo"), &[]).expect("node handle failed")
};
let now = Instant::at_offset(Duration::from_secs(0));
let generate_messages = generate_vec(
10,
generate_u8_then(0, 128, |i| {
Reverse(Entry {
arrival_time: now,
envelope: Envelope {
src: "c1".to_string(),
dest: "n1".to_string(),
body: EchoMessage::Echo {
msg_id: 0,
echo: format!("Please echo {}", i),
},
},
})
}),
);
simulate(
SimulateConfig {
number_of_tests,
Expand All @@ -582,8 +567,8 @@ mod tests {
disable_shrinking: false,
},
spawn,
generate_messages,
ECHO_PROPERTY,
echo_generator,
echo_property,
TraceBuffer::new_shared(0, 0),
false,
)
Expand Down
Loading