Skip to content

Commit

Permalink
test: add threads to regression test (#144)
Browse files Browse the repository at this point in the history
* test: add threads to regression test

* test: add file printout in case of failure
  • Loading branch information
aner-starkware authored Jul 30, 2024
1 parent 13b2edf commit 4c56977
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/committer_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ workspace = true

[dev-dependencies]
criterion = { version = "0.5.1", features = ["html_reports"] }
futures.workspace = true
pretty_assertions.workspace = true
tempfile.workspace = true

[dependencies]
clap = { version = "4.5.4", features = ["cargo", "derive"] }
Expand Down
44 changes: 30 additions & 14 deletions crates/committer_cli/src/tests/regression_tests.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::collections::HashMap;
use std::fs;

use clap::Error;
use committer::block_committer::input::{ConfigImpl, Input};
use committer::patricia_merkle_tree::external_test_utils::single_tree_flow_test;
use serde::{Deserialize, Deserializer};
use serde_json::{Map, Value};
use tempfile::NamedTempFile;

use super::utils::parse_from_python::parse_input_single_storage_tree_flow_test;
use crate::commands::commit;
Expand Down Expand Up @@ -123,18 +125,15 @@ pub async fn test_regression_single_tree() {
assert!(execution_time.as_secs_f64() < MAX_TIME_FOR_SINGLE_TREE_BECHMARK_TEST);
}

pub async fn test_single_committer_flow(input: &str, output_path: &str) {
pub async fn test_single_committer_flow(input: String, output_path: String) -> Result<(), Error> {
let CommitterRegressionInput {
committer_input,
contract_states_root: expected_contract_states_root,
contract_classes_root: expected_contract_classes_root,
expected_facts,
} = serde_json::from_str(input).unwrap();

let start = std::time::Instant::now();
} = serde_json::from_str(&input).unwrap();
// Benchmark the committer flow test.
commit(committer_input.0, output_path.to_owned()).await;
let execution_time = std::time::Instant::now() - start;

// Assert correctness of the output of the committer flow test.
let CommitterRegressionOutput {
Expand All @@ -151,12 +150,21 @@ pub async fn test_single_committer_flow(input: &str, output_path: &str) {
assert_eq!(storage_changes, *expected_facts);

// Assert the execution time does not exceed the threshold.
assert!(execution_time.as_secs_f64() < MAX_TIME_FOR_COMMITTER_FLOW_BECHMARK_TEST);
// TODO(Aner, 20/06/2024): Add cpu_time time measurement and verify the time is below the
// threshold.
Ok(())
}
#[ignore = "To avoid running the regression test in Coverage or without the --release flag."]
#[tokio::test(flavor = "multi_thread")]
pub async fn test_regression_committer_flow() {
test_single_committer_flow(FLOW_TEST_INPUT, OUTPUT_PATH).await;
let start = std::time::Instant::now();
let result =
test_single_committer_flow(FLOW_TEST_INPUT.to_string(), OUTPUT_PATH.to_string()).await;
if result.is_err() {
panic!("Error {}", result.err().unwrap());
}
let execution_time = std::time::Instant::now() - start;
assert!(execution_time.as_secs_f64() < MAX_TIME_FOR_COMMITTER_FLOW_BECHMARK_TEST);
}

#[ignore = "To avoid running the regression test in Coverage or without the --release flag."]
Expand All @@ -167,12 +175,20 @@ pub async fn test_regression_committer_all_files() {
EXPECTED_NUMBER_OF_FILES
);
let dir_path = fs::read_dir("./test_inputs/regression_files").unwrap();
for file_path in dir_path {
// TODO(Aner, 23/07/24): multi-thread the test.
test_single_committer_flow(
&fs::read_to_string(file_path.unwrap().path()).unwrap(),
OUTPUT_PATH,
)
.await;
let mut tasks = Vec::with_capacity(EXPECTED_NUMBER_OF_FILES);
for entry in dir_path {
tasks.push(tokio::task::spawn(async move {
let file_path = entry.unwrap().path();
let output_file = NamedTempFile::new().unwrap();
let result = test_single_committer_flow(
fs::read_to_string(file_path.clone()).unwrap(),
output_file.path().to_str().unwrap().to_string(),
)
.await;
if result.is_err() {
panic!("Error {} for file: {:?}", result.err().unwrap(), file_path);
}
}));
}
futures::future::try_join_all(tasks).await.unwrap();
}

0 comments on commit 4c56977

Please sign in to comment.