From cf2a16b5a47a3929c822560045bfa660743ce402 Mon Sep 17 00:00:00 2001 From: Till Faelligen <2353100+S7evinK@users.noreply.github.com> Date: Wed, 12 Jun 2024 09:56:15 +0200 Subject: [PATCH 1/3] Make GooseUser and GooseUserData cloneable --- src/goose.rs | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/src/goose.rs b/src/goose.rs index 4ba0481d..e9282d14 100644 --- a/src/goose.rs +++ b/src/goose.rs @@ -296,6 +296,7 @@ use std::sync::Arc; use std::time::Duration; use std::{fmt, str}; use std::{future::Future, pin::Pin, time::Instant}; +use std::fmt::{Debug, Formatter}; use url::Url; use crate::logger::GooseLog; @@ -822,8 +823,28 @@ pub trait GooseUserData: Downcast + Send + Sync + 'static {} impl_downcast!(GooseUserData); impl GooseUserData for T {} +trait CloneGooseUserData { + fn clone_goose_user_data(&self) -> Box; +} + +impl CloneGooseUserData for T + where + T: GooseUserData + Clone + 'static, +{ + fn clone_goose_user_data(&self) -> Box { + Box::new(self.clone()) + } +} + +impl Clone for Box { + fn clone(&self) -> Self { + self.clone_goose_user_data() + } +} + /// An individual user state, repeatedly running all [`Transaction`](./struct.Transaction.html)s /// in a specific [`Scenario`](./struct.Scenario.html). +#[derive(Debug)] pub struct GooseUser { /// The Instant when this `GooseUser` client started. pub started: Instant, @@ -870,6 +891,51 @@ pub struct GooseUser { /// [`GooseUserData`] trait. session_data: Option>, } + +impl Clone for GooseUser { + fn clone(&self) -> Self { + Self { + started: self.started, + iterations: self.iterations, + scenarios_index: self.scenarios_index, + scenario_name: self.scenario_name.clone(), + transaction_index: self.transaction_index.clone(), + transaction_name: self.transaction_name.clone(), + client: self.client.clone(), + base_url: self.base_url.clone(), + config: self.config.clone(), + logger: self.logger.clone(), + throttle: self.throttle.clone(), + is_throttled: self.is_throttled, + metrics_channel: self.metrics_channel.clone(), + shutdown_channel: self.shutdown_channel.clone(), + weighted_users_index: self.weighted_users_index, + load_test_hash: self.load_test_hash, + request_cadence: GooseRequestCadence { + last_time: self.request_cadence.last_time, + delays_since_last_time: self.request_cadence.delays_since_last_time, + counter: self.request_cadence.counter, + minimum_cadence: self.request_cadence.minimum_cadence, + maximum_cadence: self.request_cadence.maximum_cadence, + average_cadence: self.request_cadence.average_cadence, + total_elapsed: self.request_cadence.total_elapsed, + coordinated_omission_mitigation: self.request_cadence.coordinated_omission_mitigation, + user_cadence: self.request_cadence.user_cadence, + coordinated_omission_counter: self.request_cadence.coordinated_omission_counter, + }, + slept: self.slept, + session_data: if self.session_data.is_some() { Option::from(self.session_data.clone_goose_user_data()) } else { None }, + } + } +} + + +impl Debug for dyn GooseUserData { + fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { + write!(fmt, "GooseUserData") + } +} + impl GooseUser { /// Create a new user state. pub fn new( @@ -3351,7 +3417,7 @@ mod tests { #[test] fn test_get_mut_session_data() { - #[derive(Debug)] + #[derive(Debug, Clone)] struct CustomSessionData { data: String, } From e1a151a3f2029eef9239992c419455e0bd2f1a11 Mon Sep 17 00:00:00 2001 From: Till Faelligen <2353100+S7evinK@users.noreply.github.com> Date: Wed, 12 Jun 2024 09:58:25 +0200 Subject: [PATCH 2/3] Simplify cloning request_cadence Signed-off-by: Till Faelligen <2353100+S7evinK@users.noreply.github.com> --- src/goose.rs | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/goose.rs b/src/goose.rs index e9282d14..6f7dc5c7 100644 --- a/src/goose.rs +++ b/src/goose.rs @@ -911,18 +911,7 @@ impl Clone for GooseUser { shutdown_channel: self.shutdown_channel.clone(), weighted_users_index: self.weighted_users_index, load_test_hash: self.load_test_hash, - request_cadence: GooseRequestCadence { - last_time: self.request_cadence.last_time, - delays_since_last_time: self.request_cadence.delays_since_last_time, - counter: self.request_cadence.counter, - minimum_cadence: self.request_cadence.minimum_cadence, - maximum_cadence: self.request_cadence.maximum_cadence, - average_cadence: self.request_cadence.average_cadence, - total_elapsed: self.request_cadence.total_elapsed, - coordinated_omission_mitigation: self.request_cadence.coordinated_omission_mitigation, - user_cadence: self.request_cadence.user_cadence, - coordinated_omission_counter: self.request_cadence.coordinated_omission_counter, - }, + request_cadence: self.request_cadence.clone(), slept: self.slept, session_data: if self.session_data.is_some() { Option::from(self.session_data.clone_goose_user_data()) } else { None }, } From 097122ac3bb3f3f8bb4ad8f69949e810a3349a17 Mon Sep 17 00:00:00 2001 From: Till Faelligen <2353100+S7evinK@users.noreply.github.com> Date: Wed, 12 Jun 2024 11:31:38 +0200 Subject: [PATCH 3/3] Run cargo fmt --- src/goose.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/goose.rs b/src/goose.rs index 6f7dc5c7..68c0450f 100644 --- a/src/goose.rs +++ b/src/goose.rs @@ -291,12 +291,12 @@ use downcast_rs::{impl_downcast, Downcast}; use regex::Regex; use reqwest::{header, Client, ClientBuilder, Method, RequestBuilder, Response}; use serde::{Deserialize, Serialize}; +use std::fmt::{Debug, Formatter}; use std::hash::{Hash, Hasher}; use std::sync::Arc; use std::time::Duration; use std::{fmt, str}; use std::{future::Future, pin::Pin, time::Instant}; -use std::fmt::{Debug, Formatter}; use url::Url; use crate::logger::GooseLog; @@ -828,8 +828,8 @@ trait CloneGooseUserData { } impl CloneGooseUserData for T - where - T: GooseUserData + Clone + 'static, +where + T: GooseUserData + Clone + 'static, { fn clone_goose_user_data(&self) -> Box { Box::new(self.clone()) @@ -913,12 +913,15 @@ impl Clone for GooseUser { load_test_hash: self.load_test_hash, request_cadence: self.request_cadence.clone(), slept: self.slept, - session_data: if self.session_data.is_some() { Option::from(self.session_data.clone_goose_user_data()) } else { None }, + session_data: if self.session_data.is_some() { + Option::from(self.session_data.clone_goose_user_data()) + } else { + None + }, } } } - impl Debug for dyn GooseUserData { fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { write!(fmt, "GooseUserData")