Skip to content

Commit

Permalink
refactor: use struct for deserialization (#283)
Browse files Browse the repository at this point in the history
* refactor: use struct for deserialization

* chore: add todos
  • Loading branch information
aner-starkware authored Jul 8, 2024
1 parent 320bde2 commit 616d9af
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions crates/committer_cli/src/tests/benchmark_tests.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
use std::collections::HashMap;

use committer::patricia_merkle_tree::external_test_utils::single_tree_flow_test;
use serde::Deserialize;
use serde_json::{Map, Value};

use crate::{
commands::commit, tests::utils::parse_from_python::parse_input_single_storage_tree_flow_test,
};

//TODO(Aner, 20/06/2024): this test needs to be fixed to be run correctly in the CI:
//TODO(Aner, 20/06/2024): these tests needs to be fixed to be run correctly in the CI:
//1. Fix the test to measure cpu_time and not wall_time.
//2. Fix the max time threshold to be the expected time for the benchmark test.
const MAX_TIME_FOR_SINGLE_TREE_BECHMARK_TEST: f64 = 5.0;
const MAX_TIME_FOR_COMMITTER_FLOW_BECHMARK_TEST: f64 = 5.0;
const SINGLE_TREE_FLOW_INPUT: &str = include_str!("../../benches/tree_flow_inputs.json");
//TODO(Aner, 20/06/2024): modify the committer_flow_inputs.json file to be from pseudo-real data
// and to include the expected output.
const FLOW_TEST_INPUT: &str = include_str!("../../benches/committer_flow_inputs.json");
const OUTPUT_PATH: &str = "benchmark_output.txt";

#[derive(Deserialize)]
struct CommitterRegressionInput {
committer_input: String,
contract_states_root: String,
contract_classes_root: String,
expected_facts: String,
}

#[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() {
Expand Down Expand Up @@ -52,13 +59,11 @@ pub async fn test_benchmark_single_tree() {
#[ignore = "To avoid running the benchmark test in Coverage or without the --release flag."]
#[tokio::test(flavor = "multi_thread")]
pub async fn test_benchmark_committer_flow() {
// TODO(Aner, 8/7/2024): use structs for deserialization.
let input: HashMap<String, String> = serde_json::from_str(FLOW_TEST_INPUT).unwrap();
let committer_input = input.get("committer_input").unwrap();
let regression_input: CommitterRegressionInput = serde_json::from_str(FLOW_TEST_INPUT).unwrap();

let start = std::time::Instant::now();
// Benchmark the committer flow test.
commit(committer_input, OUTPUT_PATH.to_owned()).await;
commit(&regression_input.committer_input, OUTPUT_PATH.to_owned()).await;
let execution_time = std::time::Instant::now() - start;

// Assert the output of the committer flow test.
Expand All @@ -69,18 +74,14 @@ pub async fn test_benchmark_committer_flow() {
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();

let expected_contract_storage_root_hash = input.get("contract_states_root").unwrap();
let expected_compiled_class_root_hash = input.get("contract_classes_root").unwrap();

assert_eq!(
contract_storage_root_hash.as_str().unwrap(),
expected_contract_storage_root_hash
regression_input.contract_states_root
);
assert_eq!(
compiled_class_root_hash.as_str().unwrap(),
expected_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
Expand All @@ -92,9 +93,9 @@ pub async fn test_benchmark_committer_flow() {
panic!("Expected the storage to be an object.");
};

// TODO(Aner, 8/7/2024): use structs for deserialization.
// TODO(Aner, 8/7/2024): fix to deserialize automatically.
let expected_storage_changes: Map<String, Value> =
serde_json::from_str(input.get("expected_facts").unwrap()).unwrap();
serde_json::from_str(&regression_input.expected_facts).unwrap();

assert_eq!(storage_changes, &expected_storage_changes);

Expand Down

0 comments on commit 616d9af

Please sign in to comment.