Skip to content

Commit

Permalink
refactor: deserialization structs for regression test output
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware committed Jul 9, 2024
1 parent 9e72961 commit 8ac50c4
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions crates/committer_cli/src/tests/benchmark_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,29 @@ const SINGLE_TREE_FLOW_INPUT: &str = include_str!("../../benches/tree_flow_input
const FLOW_TEST_INPUT: &str = include_str!("../../benches/committer_flow_inputs.json");
const OUTPUT_PATH: &str = "benchmark_output.txt";

// TODO(Aner, 8/7/2024): use deserializer to extract the facts to mapping directly.
#[derive(Deserialize)]
struct CommitterRegressionInput {
committer_input: String,
contract_states_root: String,
contract_classes_root: String,
expected_facts: String,
}
#[derive(Deserialize)]
struct TreeRegressionOutput {
root_hash: Value,
storage_changes: Value,
}
#[derive(Deserialize)]
struct StorageObject {
storage: Value,
}
#[derive(Deserialize)]
struct CommitterRegressionOutput {
contract_storage_root_hash: Value,
compiled_class_root_hash: Value,
storage: StorageObject,
}

struct TreeRegressionInput {
tree_flow_input: TreeFlowInput,
Expand Down Expand Up @@ -52,32 +68,28 @@ impl<'de> Deserialize<'de> for TreeRegressionInput {
#[ignore = "To avoid running the benchmark test in Coverage or without the --release flag."]
#[tokio::test(flavor = "multi_thread")]
pub async fn test_benchmark_single_tree() {
let tree_regression_input: TreeRegressionInput =
let regression_input: TreeRegressionInput =
serde_json::from_str(SINGLE_TREE_FLOW_INPUT).unwrap();
let TreeFlowInput {
leaf_modifications,
storage,
root_hash,
} = tree_regression_input.tree_flow_input;
} = regression_input.tree_flow_input;

let start = std::time::Instant::now();
// Benchmark the single tree flow test.
let output = single_tree_flow_test(leaf_modifications, storage, root_hash).await;
let execution_time = std::time::Instant::now() - start;

// Assert correctness of the output of the single tree flow test.
// TODO(Aner, 8/7/2024): use structs for deserialization.
let output_map: HashMap<&str, Value> = serde_json::from_str(&output).unwrap();
let output_hash = output_map.get("root_hash").unwrap();
let expected_hash = tree_regression_input.expected_hash;
assert_eq!(output_hash.as_str().unwrap(), expected_hash);
let regression_output: TreeRegressionOutput = serde_json::from_str(&output).unwrap();
assert_eq!(regression_output.root_hash, regression_input.expected_hash);

// Assert the storage changes.
let Value::Object(storage_changes) = output_map.get("storage_changes").unwrap() else {
let Value::Object(storage_changes) = regression_output.storage_changes else {
panic!("Expected storage changes object to be an object.");
};
let expected_storage_changes = &tree_regression_input.expected_storage_changes;
assert_eq!(storage_changes, expected_storage_changes);
assert_eq!(storage_changes, regression_input.expected_storage_changes);

// 4. Assert the execution time does not exceed the threshold.
assert!(execution_time.as_secs_f64() < MAX_TIME_FOR_SINGLE_TREE_BECHMARK_TEST);
Expand All @@ -94,37 +106,27 @@ pub async fn test_benchmark_committer_flow() {
let execution_time = std::time::Instant::now() - start;

// Assert the output of the committer flow test.
// TODO(Aner, 8/7/2024): use structs for deserialization.
let committer_output: HashMap<String, Value> =
let committer_output: CommitterRegressionOutput =
serde_json::from_str(&std::fs::read_to_string(OUTPUT_PATH).unwrap()).unwrap();

let contract_storage_root_hash = committer_output.get("contract_storage_root_hash").unwrap();
let compiled_class_root_hash = committer_output.get("compiled_class_root_hash").unwrap();

assert_eq!(
contract_storage_root_hash.as_str().unwrap(),
committer_output.contract_storage_root_hash,
regression_input.contract_states_root
);
assert_eq!(
compiled_class_root_hash.as_str().unwrap(),
committer_output.compiled_class_root_hash,
regression_input.contract_classes_root
);
// Assert the storage changes.
// TODO(Aner, 8/7/2024): use structs for deserialization.
let Value::Object(storage_changes) = committer_output
.get("storage")
.unwrap()
.get("storage")
.unwrap()
else {
let Value::Object(storage_changes) = committer_output.storage.storage else {
panic!("Expected the storage to be an object.");
};

// TODO(Aner, 8/7/2024): fix to deserialize automatically.
// TODO(Aner, 8/7/2024): fix to deserialize automatically, using deserializer.
let expected_storage_changes: Map<String, Value> =
serde_json::from_str(&regression_input.expected_facts).unwrap();

assert_eq!(storage_changes, &expected_storage_changes);
assert_eq!(storage_changes, expected_storage_changes);

// Assert the execution time does not exceed the threshold.
assert!(execution_time.as_secs_f64() < MAX_TIME_FOR_COMMITTER_FLOW_BECHMARK_TEST);
Expand Down

0 comments on commit 8ac50c4

Please sign in to comment.