forked from InftyAI/AMRS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom.rs
More file actions
60 lines (52 loc) · 1.42 KB
/
random.rs
File metadata and controls
60 lines (52 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use rand::Rng;
use crate::client::config::ModelName;
use crate::router::router::{ModelInfo, Router};
pub struct RandomRouter {
pub model_infos: Vec<ModelInfo>,
}
impl RandomRouter {
pub fn new(model_infos: Vec<ModelInfo>) -> Self {
Self { model_infos }
}
}
impl Router for RandomRouter {
fn name(&self) -> &'static str {
"RandomRouter"
}
fn sample(&self) -> ModelName {
let mut rng = rand::rng();
let idx = rng.random_range(0..self.model_infos.len());
self.model_infos[idx].name.clone()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_random_router_sampling() {
let model_infos = vec![
ModelInfo {
name: "model_x".to_string(),
weight: 1,
},
ModelInfo {
name: "model_y".to_string(),
weight: 2,
},
ModelInfo {
name: "model_z".to_string(),
weight: 3,
},
];
let mut router = RandomRouter::new(model_infos.clone());
let mut counts = std::collections::HashMap::new();
for _ in 0..1000 {
let candidate = router.sample();
*counts.entry(candidate.clone()).or_insert(0) += 1;
}
assert!(counts.len() == model_infos.len());
for count in counts.values() {
assert!(*count > 0);
}
}
}