Skip to content

Commit

Permalink
test: benchmarking test Tokio vs Rayon
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware committed Mar 31, 2024
1 parent ca9a3ef commit 82135d3
Showing 1 changed file with 67 additions and 39 deletions.
106 changes: 67 additions & 39 deletions crates/committer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ pub mod test {
));
node.hash_value.unwrap()
} else {
Felt::default()
//TODO: compute/return the path hash
todo!("Path hash computation")
}
}

Expand All @@ -340,52 +341,79 @@ pub mod test {
));
node.hash_value.unwrap()
} else {
Felt::default()
//TODO: compute/return the path hash
todo!("Path hash computation")
}
}

//run with `cargo test --release -- --nocapture bench_threading`
#[tokio::test]
#[tokio::test(flavor = "multi_thread")]
async fn bench_threading() {
let _height = TREE_HEIGHT;
// fill the tree with data
let now = Instant::now();
let root = create_dummy_tree(_height);
let root_clone = clone_tree(&root);
let root_clone_clone = clone_tree(&root_clone);
let elapased_time = now.elapsed();
println!("Tree creation time: {:?}", elapased_time);
let height = TREE_HEIGHT;
let num_repetitions: usize = 3;
let mut time_tokio: Vec<Duration> = Vec::new();
let mut time_rayon: Vec<Duration> = Vec::new();
let mut time_seq: Vec<Duration> = Vec::new();

let now = Instant::now();
// Code block to measure.
let result_tokio = algorithm_tokio(*root_clone.unwrap()).await;
// TODO: sanity check
// End of code block to measure.
let elapased_time_tokio = now.elapsed();
// Print measurement.
println!("Tokio time: {:?}", elapased_time_tokio);
for _i in 0..num_repetitions {
// fill the tree with data
let now = Instant::now();
let root = create_dummy_tree(height);
let root_clone = clone_tree(&root);
let root_clone_clone = clone_tree(&root_clone);
let elapased_time = now.elapsed();
println!("Tree creation time: {:?}", elapased_time);

let now = Instant::now();
// Code block to measure.
let result_tokio = algorithm_tokio(*root_clone.unwrap()).await;
// End of code block to measure.
let elapased_time_tokio = now.elapsed();
time_tokio.push(elapased_time_tokio);
// Print measurement.
println!("Tokio time: {:?}", elapased_time_tokio);

let now = Instant::now();
// Code block to measure.
let result_rayon = algorithm_rayon(*root_clone_clone.unwrap());
// TODO: sanity check
// End of code block to measure.
let elapased_time_rayon = now.elapsed();
// Print measurement.
println!("Rayon time: {:?}", elapased_time_rayon);
let now = Instant::now();
// Code block to measure.
let result_rayon = algorithm_rayon(*root_clone_clone.unwrap());
// End of code block to measure.
let elapased_time_rayon = now.elapsed();
time_rayon.push(elapased_time_rayon);
// Print measurement.
println!("Rayon time: {:?}", elapased_time_rayon);

let now = Instant::now();
// Code block to measure.
let result_seq = algorithm_seq(*root.unwrap());
// TODO: sanity check
// End of code block to measure.
let elapased_time_seq = now.elapsed();
// Print measurement.
println!("Sequential time: {:?}", elapased_time_seq);
let now = Instant::now();
// Code block to measure.
let result_seq = algorithm_seq(*root.unwrap());
// End of code block to measure.
let elapased_time_seq = now.elapsed();
time_seq.push(elapased_time_seq);
// Print measurement.
println!("Sequential time: {:?}", elapased_time_seq);

// Sanity check.
assert_eq!(result_seq, result_tokio);
assert_eq!(result_seq, result_rayon);
println!("Sanity check passed!");
// Sanity check.
assert_eq!(result_seq, result_tokio);
assert_eq!(result_seq, result_rayon);
println!("Sanity check passed!");
}
// Print statistics.
println!(
"Average time for {:?} hashes using tokio: {:?}, Std deviation: {:?}",
2_u32.pow(height.into()),
mean(&time_tokio),
std_deviation(&time_tokio),
);
println!(
"Average time for {:?} hashes using rayon: {:?}, Std deviation: {:?}",
2_u32.pow(height.into()),
mean(&time_rayon),
std_deviation(&time_rayon),
);
println!(
"Average time for {:?} hashes using sequential: {:?}, Std deviation: {:?}",
2_u32.pow(height.into()),
mean(&time_seq),
std_deviation(&time_seq),
);
}
}

0 comments on commit 82135d3

Please sign in to comment.