relay_server/utils/
pick.rs1pub fn is_rolled_out(id: u64, rate: f32) -> bool {
6 ((id % 100000) as f32 / 100000.0f32) < rate
7}
8
9pub fn sample(rate: f32) -> bool {
13 (rate >= 1.0) || (rate > 0.0 && rand::random::<f32>() < rate)
14}
15
16#[cfg(test)]
17mod test {
18 use super::*;
19
20 #[test]
21 fn test_rollout() {
22 assert!(!is_rolled_out(1, 0.0));
23 assert!(is_rolled_out(1, 0.0001)); assert!(is_rolled_out(1, 0.1));
25 assert!(is_rolled_out(1, 0.9));
26 assert!(is_rolled_out(1, 1.0));
27
28 assert!(!is_rolled_out(0, 0.0));
29 assert!(is_rolled_out(0, 1.0));
30 assert!(!is_rolled_out(100000, 0.0));
31 assert!(is_rolled_out(100000, 1.0));
32 assert!(!is_rolled_out(100001, 0.0));
33 assert!(is_rolled_out(100001, 1.0));
34
35 assert!(!is_rolled_out(42, -100.0));
36 assert!(is_rolled_out(42, 100.0));
37 }
38
39 #[test]
40 fn test_sample() {
41 assert!(sample(1.0));
42 assert!(!sample(0.0));
43
44 let mut r: i64 = 0;
45 for _ in 0..10000 {
46 if sample(0.5) {
47 r += 1;
48 } else {
49 r -= 1;
50 }
51 }
52 assert!(r.abs() < 500);
53 }
54}