|
1 | 1 | //! `DogStatsD` payload. |
2 | 2 |
|
3 | | -use std::{cmp, fmt, io::Write, ops::Range, rc::Rc}; |
| 3 | +use std::{fmt, io::Write, rc::Rc}; |
4 | 4 |
|
5 | | -use rand::{ |
6 | | - Rng, |
7 | | - distr::{uniform::SampleUniform, weighted::WeightedIndex}, |
8 | | - prelude::Distribution, |
9 | | - seq::IndexedRandom, |
10 | | -}; |
| 5 | +use rand::{Rng, distr::weighted::WeightedIndex, prelude::Distribution}; |
11 | 6 | use serde::{Deserialize, Serialize as SerdeSerialize}; |
12 | 7 | use tracing::{debug, warn}; |
13 | 8 |
|
14 | | -use crate::{Serialize, common::strings}; |
| 9 | +use crate::{ |
| 10 | + Serialize, |
| 11 | + common::{ |
| 12 | + config::ConfRange, |
| 13 | + strings, |
| 14 | + strings::{random_strings_with_length, random_strings_with_length_range}, |
| 15 | + }, |
| 16 | +}; |
15 | 17 |
|
16 | 18 | use self::{ |
17 | 19 | common::tags, event::EventGenerator, metric::MetricGenerator, |
@@ -144,69 +146,6 @@ impl Default for ValueConf { |
144 | 146 | } |
145 | 147 | } |
146 | 148 | } |
147 | | -/// Range expression for configuration |
148 | | -#[derive(Debug, Deserialize, SerdeSerialize, Clone, PartialEq, Copy)] |
149 | | -#[serde(deny_unknown_fields)] |
150 | | -#[serde(rename_all = "snake_case")] |
151 | | -#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))] |
152 | | - |
153 | | -pub enum ConfRange<T> |
154 | | -where |
155 | | - T: PartialEq + cmp::PartialOrd + Clone + Copy, |
156 | | -{ |
157 | | - /// A constant T |
158 | | - Constant(T), |
159 | | - /// In which a T is chosen between `min` and `max`, inclusive of `max`. |
160 | | - Inclusive { |
161 | | - /// The minimum of the range. |
162 | | - min: T, |
163 | | - /// The maximum of the range. |
164 | | - max: T, |
165 | | - }, |
166 | | -} |
167 | | - |
168 | | -impl<T> ConfRange<T> |
169 | | -where |
170 | | - T: PartialEq + cmp::PartialOrd + Clone + Copy, |
171 | | -{ |
172 | | - /// Returns true if the range provided by the user is valid, false |
173 | | - /// otherwise. |
174 | | - fn valid(&self) -> (bool, &'static str) { |
175 | | - match self { |
176 | | - Self::Constant(_) => (true, ""), |
177 | | - Self::Inclusive { min, max } => (min < max, "min must be less than max"), |
178 | | - } |
179 | | - } |
180 | | - |
181 | | - fn start(&self) -> T { |
182 | | - match self { |
183 | | - ConfRange::Constant(c) => *c, |
184 | | - ConfRange::Inclusive { min, .. } => *min, |
185 | | - } |
186 | | - } |
187 | | - |
188 | | - fn end(&self) -> T { |
189 | | - match self { |
190 | | - ConfRange::Constant(c) => *c, |
191 | | - ConfRange::Inclusive { max, .. } => *max, |
192 | | - } |
193 | | - } |
194 | | -} |
195 | | - |
196 | | -impl<T> ConfRange<T> |
197 | | -where |
198 | | - T: PartialEq + cmp::PartialOrd + Clone + Copy + SampleUniform, |
199 | | -{ |
200 | | - fn sample<R>(&self, rng: &mut R) -> T |
201 | | - where |
202 | | - R: rand::Rng + ?Sized, |
203 | | - { |
204 | | - match self { |
205 | | - ConfRange::Constant(c) => *c, |
206 | | - ConfRange::Inclusive { min, max } => rng.random_range(*min..=*max), |
207 | | - } |
208 | | - } |
209 | | -} |
210 | 149 |
|
211 | 150 | /// Configure the `DogStatsD` payload. |
212 | 151 | #[derive(Debug, Deserialize, SerdeSerialize, Clone, PartialEq, Copy)] |
@@ -359,70 +298,6 @@ impl Config { |
359 | 298 | } |
360 | 299 | } |
361 | 300 |
|
362 | | -fn choose_or_not_ref<'a, R, T>(mut rng: &mut R, pool: &'a [T]) -> Option<&'a T> |
363 | | -where |
364 | | - R: rand::Rng + ?Sized, |
365 | | -{ |
366 | | - if rng.random() { |
367 | | - pool.choose(&mut rng) |
368 | | - } else { |
369 | | - None |
370 | | - } |
371 | | -} |
372 | | - |
373 | | -fn choose_or_not_fn<R, T, F>(rng: &mut R, func: F) -> Option<T> |
374 | | -where |
375 | | - T: Clone, |
376 | | - R: rand::Rng + ?Sized, |
377 | | - F: FnOnce(&mut R) -> Option<T>, |
378 | | -{ |
379 | | - if rng.random() { func(rng) } else { None } |
380 | | -} |
381 | | - |
382 | | -#[inline] |
383 | | -/// Generate a total number of strings randomly chosen from the range `min_max` with a maximum length |
384 | | -/// per string of `max_length`. |
385 | | -fn random_strings_with_length<R>( |
386 | | - pool: &strings::Pool, |
387 | | - min_max: Range<usize>, |
388 | | - max_length: u16, |
389 | | - rng: &mut R, |
390 | | -) -> Vec<String> |
391 | | -where |
392 | | - R: Rng + ?Sized, |
393 | | -{ |
394 | | - let total = rng.random_range(min_max); |
395 | | - let length_range = ConfRange::Inclusive { |
396 | | - min: 1, |
397 | | - max: max_length, |
398 | | - }; |
399 | | - |
400 | | - random_strings_with_length_range(pool, total, length_range, rng) |
401 | | -} |
402 | | - |
403 | | -#[inline] |
404 | | -/// Generate a `total` number of strings with a maximum length per string of |
405 | | -/// `max_length`. |
406 | | -fn random_strings_with_length_range<R>( |
407 | | - pool: &strings::Pool, |
408 | | - total: usize, |
409 | | - length_range: ConfRange<u16>, |
410 | | - mut rng: &mut R, |
411 | | -) -> Vec<String> |
412 | | -where |
413 | | - R: Rng + ?Sized, |
414 | | -{ |
415 | | - let mut buf = Vec::with_capacity(total); |
416 | | - for _ in 0..total { |
417 | | - let sz = length_range.sample(&mut rng) as usize; |
418 | | - buf.push(String::from( |
419 | | - pool.of_size(&mut rng, sz) |
420 | | - .expect("failed to generate string"), |
421 | | - )); |
422 | | - } |
423 | | - buf |
424 | | -} |
425 | | - |
426 | 301 | #[derive(Debug, Clone)] |
427 | 302 | struct MemberGenerator { |
428 | 303 | kind_weights: WeightedIndex<u16>, |
|
0 commit comments