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 339a5cd commit 4409270
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions crates/committer_cli/src/tests/benchmark_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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,
Expand All @@ -25,6 +26,24 @@ struct CommitterRegressionInput {
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,
expected_hash: String,
Expand Down Expand Up @@ -69,17 +88,16 @@ pub async fn test_benchmark_single_tree() {
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();
assert_eq!(output_hash.as_str().unwrap(), expected_hash);

// Assert the storage changes.
let Value::Object(storage_changes) = output_map.get("storage_changes").unwrap() else {
let TreeRegressionOutput {
root_hash,
storage_changes: Value::Object(storage_changes_map),
} = serde_json::from_str(&output).unwrap()
else {
panic!("Expected storage changes object to be an object.");
};
assert_eq!(root_hash, expected_hash);

assert_eq!(storage_changes, &expected_storage_changes);
assert_eq!(storage_changes_map, 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 @@ -96,37 +114,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 4409270

Please sign in to comment.