turbopack_bench/util/
module_picker.rs1use std::path::PathBuf;
2
3use rand::{SeedableRng, rngs::StdRng, seq::IndexedRandom};
4use rustc_hash::FxHashMap;
5
6pub struct ModulePicker {
12 depths: Vec<usize>,
13 modules_by_depth: FxHashMap<usize, Vec<PathBuf>>,
14 rng: parking_lot::Mutex<StdRng>,
15}
16
17impl ModulePicker {
18 pub fn new(mut modules: Vec<(PathBuf, usize)>) -> Self {
20 let rng = StdRng::seed_from_u64(42);
21
22 modules.sort();
24
25 let mut modules_by_depth: FxHashMap<_, Vec<_>> = FxHashMap::default();
26 for (module, depth) in modules {
27 modules_by_depth.entry(depth).or_default().push(module);
28 }
29 let mut depths: Vec<_> = modules_by_depth.keys().copied().collect();
30 depths.sort();
32
33 Self {
34 depths,
35 modules_by_depth,
36 rng: parking_lot::Mutex::new(rng),
37 }
38 }
39
40 pub fn pick(&self) -> &PathBuf {
42 let mut rng = self.rng.lock();
43 let depth = self.depths.choose(&mut *rng).unwrap();
45 self.modules_by_depth[depth].choose(&mut *rng).unwrap()
46 }
47}