1use crate::{
2 error::Error,
3 state::{GroupName, Model},
4};
5use rand::{rng, RngExt};
6use serde::{
7 de::{value, IntoDeserializer},
8 Deserialize, Serialize,
9};
10use std::{collections::BTreeMap, path::Path, time::Duration};
11
12const ITEM_UPPER_BOUND: u64 = 1 << 40;
14
15const DEFAULT_GROUP_COUNT: u64 = 10;
16const DEFAULT_PERSON_COUNT: u64 = 10;
17
18const DEFAULT_WARMUP_TIME: u64 = 10;
19const DEFAULT_TEST_TIME: Option<u64> = Some(180);
20
21#[derive(Debug, Serialize, Deserialize)]
22pub struct GroupProperties {
23 pub member_count: Option<u64>,
24}
25
26#[derive(Debug, Serialize, Deserialize)]
27pub struct Profile {
28 control_uri: String,
29 admin_password: String,
30 idm_admin_password: String,
31 seed: i64,
32 extra_uris: Vec<String>,
33 warmup_time: u64,
35 test_time: Option<u64>,
36 group_count: u64,
37 person_count: u64,
38 thread_count: Option<usize>,
39 model: Model,
40 group: BTreeMap<String, GroupProperties>,
41 #[serde(default)]
42 dump_raw_data: bool,
43}
44
45impl Profile {
46 pub fn control_uri(&self) -> &str {
47 self.control_uri.as_str()
48 }
49
50 pub fn extra_uris(&self) -> &[String] {
51 self.extra_uris.as_slice()
52 }
53
54 pub fn admin_password(&self) -> &str {
55 self.admin_password.as_str()
56 }
57
58 pub fn idm_admin_password(&self) -> &str {
59 self.idm_admin_password.as_str()
60 }
61
62 #[allow(dead_code)]
63 pub fn group_count(&self) -> u64 {
64 self.group_count
65 }
66
67 pub fn person_count(&self) -> u64 {
68 self.person_count
69 }
70
71 pub fn thread_count(&self) -> Option<usize> {
72 self.thread_count
73 }
74
75 pub fn get_properties_by_group(&self) -> &BTreeMap<String, GroupProperties> {
76 &self.group
77 }
78
79 pub fn seed(&self) -> u64 {
80 if self.seed < 0 {
81 self.seed.wrapping_mul(-1) as u64
82 } else {
83 self.seed as u64
84 }
85 }
86
87 pub fn model(&self) -> &Model {
88 &self.model
89 }
90
91 pub fn warmup_time(&self) -> Duration {
92 Duration::from_secs(self.warmup_time)
93 }
94
95 pub fn test_time(&self) -> Option<Duration> {
96 self.test_time.map(Duration::from_secs)
97 }
98
99 pub fn dump_raw_data(&self) -> bool {
100 self.dump_raw_data
101 }
102}
103
104pub struct ProfileBuilder {
105 pub control_uri: String,
106 pub admin_password: String,
107 pub idm_admin_password: String,
108 pub seed: Option<u64>,
109 pub extra_uris: Vec<String>,
110 pub warmup_time: Option<u64>,
112 pub test_time: Option<Option<u64>>,
113 pub group_count: Option<u64>,
114 pub person_count: Option<u64>,
115 pub thread_count: Option<usize>,
116 pub model: Model,
117 pub dump_raw_data: bool,
118}
119
120fn validate_u64_bound(value: Option<u64>, default: u64) -> Result<u64, Error> {
121 if let Some(v) = value {
122 if v > ITEM_UPPER_BOUND {
123 error!("group count exceeds upper bound ({})", ITEM_UPPER_BOUND);
124 Err(Error::ProfileBuilder)
125 } else {
126 Ok(v)
127 }
128 } else {
129 Ok(default)
130 }
131}
132
133impl ProfileBuilder {
134 pub fn new(
135 control_uri: String,
136 extra_uris: Vec<String>,
137 admin_password: String,
138 idm_admin_password: String,
139 model: Model,
140 thread_count: Option<usize>,
141 dump_raw_data: bool,
142 ) -> Self {
143 ProfileBuilder {
144 control_uri,
145 extra_uris,
146 admin_password,
147 idm_admin_password,
148 seed: None,
149 warmup_time: None,
150 test_time: None,
151 group_count: None,
152 person_count: None,
153 thread_count,
154 model,
155 dump_raw_data,
156 }
157 }
158
159 pub fn seed(mut self, seed: Option<u64>) -> Self {
160 self.seed = seed;
161 self
162 }
163
164 #[allow(dead_code)]
165 pub fn warmup_time(mut self, time: Option<u64>) -> Self {
166 self.warmup_time = time;
167 self
168 }
169
170 #[allow(dead_code)]
171 pub fn test_time(mut self, time: Option<Option<u64>>) -> Self {
172 self.test_time = time;
173 self
174 }
175
176 #[allow(dead_code)]
177 pub fn group_count(mut self, group_count: Option<u64>) -> Self {
178 self.group_count = group_count;
179 self
180 }
181
182 #[allow(dead_code)]
183 pub fn person_count(mut self, person_count: Option<u64>) -> Self {
184 self.person_count = person_count;
185 self
186 }
187
188 pub fn build(self) -> Result<Profile, Error> {
189 let ProfileBuilder {
190 control_uri,
191 admin_password,
192 idm_admin_password,
193 seed,
194 extra_uris,
195 warmup_time,
196 test_time,
197 group_count,
198 person_count,
199 thread_count,
200 model,
201 dump_raw_data,
202 } = self;
203
204 let seed: u64 = seed.unwrap_or_else(|| {
205 let mut rng = rng();
206 rng.random()
207 });
208
209 let group = BTreeMap::new();
211
212 let group_count = validate_u64_bound(group_count, DEFAULT_GROUP_COUNT)?;
213 let person_count = validate_u64_bound(person_count, DEFAULT_PERSON_COUNT)?;
214
215 let warmup_time = warmup_time.unwrap_or(DEFAULT_WARMUP_TIME);
216 let test_time = test_time.unwrap_or(DEFAULT_TEST_TIME);
217
218 let seed: i64 = if seed > i64::MAX as u64 {
219 let seed = seed - i64::MAX as u64;
221 -(seed as i64)
222 } else {
223 seed as i64
224 };
225
226 Ok(Profile {
227 control_uri,
228 admin_password,
229 idm_admin_password,
230 seed,
231 extra_uris,
232 warmup_time,
233 test_time,
234 group_count,
235 person_count,
236 thread_count,
237 group,
238 model,
239 dump_raw_data,
240 })
241 }
242}
243
244impl Profile {
245 pub fn write_to_path(&self, path: &Path) -> Result<(), Error> {
246 let file_contents = toml::to_string(self).map_err(|toml_err| {
247 error!(?toml_err);
248 Error::SerdeToml
249 })?;
250
251 std::fs::write(path, file_contents).map_err(|io_err| {
252 error!(?io_err);
253 Error::Io
254 })
255 }
256
257 fn validate_group_names_and_member_count(&self) -> Result<(), Error> {
258 for (group_name, group_properties) in self.group.iter() {
259 let _ = GroupName::deserialize(group_name.as_str().into_deserializer()).map_err(
260 |_: value::Error| {
261 error!("Invalid group name provided: {group_name}");
262 Error::InvalidState
263 },
264 )?;
265 let provided_member_count = group_properties.member_count.unwrap_or_default();
266 let max_member_count = self.person_count();
267 if provided_member_count > max_member_count {
268 error!("Member count of {group_name} is out of bound: max value is {max_member_count}, but {provided_member_count} was provided");
269 return Err(Error::InvalidState);
270 }
271 }
272 Ok(())
273 }
274}
275
276impl TryFrom<&Path> for Profile {
277 type Error = Error;
278
279 fn try_from(path: &Path) -> Result<Self, Self::Error> {
280 let file_contents = std::fs::read_to_string(path).map_err(|io_err| {
281 error!(?io_err);
282 Error::Io
283 })?;
284
285 let profile: Profile = toml::from_str(&file_contents).map_err(|toml_err| {
286 error!(?toml_err);
287 Error::SerdeToml
288 })?;
289 profile.validate_group_names_and_member_count()?;
290
291 Ok(profile)
292 }
293}