From af767855180e9e74359d3bb93c90484a2b2153e3 Mon Sep 17 00:00:00 2001 From: scx1332 Date: Wed, 15 Nov 2023 23:00:06 +0100 Subject: [PATCH] Cycle endpoints --- crates/web3_test_proxy/src/main.rs | 58 ++++++++++++++++-------------- crates/web3_test_proxy/src/plan.rs | 3 +- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/crates/web3_test_proxy/src/main.rs b/crates/web3_test_proxy/src/main.rs index 91494bee..594255da 100644 --- a/crates/web3_test_proxy/src/main.rs +++ b/crates/web3_test_proxy/src/main.rs @@ -646,19 +646,26 @@ async fn main_internal() -> Result<(), Web3ProxyError> { let str = std::fs::read(problem_plan).expect("Cannot read problem plan"); let problem_plan: ProblemProject = serde_json::from_slice(&str).expect("Cannot parse problem plan"); - let frame_interval = Duration::from_secs_f64(problem_plan.frame_interval); - let mut problem_project = SortedProblemIterator::from_problem_project(problem_plan); + //let frame_cycle = ; + let mut problem_project = SortedProblemIterator::from_problem_project(&problem_plan); let mut last_time = Instant::now(); let mut frame_no = 0; loop { - if exit_cnd_.load(std::sync::atomic::Ordering::Relaxed) { - return; + if let Some(frame_cycle) = problem_plan.frame_cycle { + if frame_no >= frame_cycle { + frame_no = 0; + problem_project = SortedProblemIterator::from_problem_project(&problem_plan.clone()); + log::info!("Cycle finished, restarting from frame 0"); + } } let server_data = server_data_.clone(); loop { - let sleep_time = frame_interval.as_secs_f64() - last_time.elapsed().as_secs_f64(); + if exit_cnd_.load(std::sync::atomic::Ordering::Relaxed) { + return; + } + let sleep_time = problem_plan.frame_interval - last_time.elapsed().as_secs_f64(); let sleep_time = sleep_time.min(0.1); if frame_no > 0 && sleep_time > 0.0 { tokio::time::sleep(Duration::from_secs_f64(sleep_time)).await; @@ -667,28 +674,27 @@ async fn main_internal() -> Result<(), Web3ProxyError> { } } - while let Some(problem_entry) = problem_project.get_next_entry(frame_no) { + { let mut shared_data = server_data.shared_data.lock().await; - for key in &problem_entry.keys { - - - let key_data = match shared_data.keys.get_mut(key) { - Some(key_data) => key_data, - None => { - shared_data.keys.insert(key.to_string(), KeyData { - key: key.to_string(), - value: "1".to_string(), - total_calls: 0, - total_requests: 0, - calls: VecDeque::new(), - problems: EndpointSimulateProblems::default(), - }); - shared_data.keys.get_mut(key).unwrap() - } - }; - - key_data.problems.apply_change(&problem_entry.values); - log::info!("Applied change for key: {}, frame: {}", key, frame_no); + while let Some(problem_entry) = problem_project.get_next_entry(frame_no) { + for key in &problem_entry.keys { + let key_data = match shared_data.keys.get_mut(key) { + Some(key_data) => key_data, + None => { + shared_data.keys.insert(key.to_string(), KeyData { + key: key.to_string(), + value: "1".to_string(), + total_calls: 0, + total_requests: 0, + calls: VecDeque::new(), + problems: EndpointSimulateProblems::default(), + }); + shared_data.keys.get_mut(key).unwrap() + } + }; + key_data.problems.apply_change(&problem_entry.values); + log::info!("Applied change for key: {}, frame: {}", key, frame_no); + } } } diff --git a/crates/web3_test_proxy/src/plan.rs b/crates/web3_test_proxy/src/plan.rs index f2af81c1..c42127c6 100644 --- a/crates/web3_test_proxy/src/plan.rs +++ b/crates/web3_test_proxy/src/plan.rs @@ -18,6 +18,7 @@ pub struct ProblemProject { name : String, plan_type: String, pub frame_interval: f64, + pub frame_cycle: Option, entries: Vec } @@ -37,7 +38,7 @@ pub struct SortedProblemEntry { impl SortedProblemIterator { // sort problems by frame - pub fn from_problem_project(problem_project: ProblemProject) -> SortedProblemIterator { + pub fn from_problem_project(problem_project: &ProblemProject) -> SortedProblemIterator { let sorted_entries: Vec = problem_project.entries .iter() .flat_map(|entry| { // Use flat_map to handle nested structure