-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sample many modules in benchmarks + reliability fixes (vercel/turbo#2750
) * Sample many modules in benchmarks + reliability fixes * Fix depth sampling to be uniform * Fix Webpack benchmark * Only use detector component for RSC * Clippy * Clippy
- Loading branch information
Showing
10 changed files
with
294 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::{error::Error, str::FromStr}; | ||
|
||
use anyhow::{anyhow, Context, Result}; | ||
|
||
/// Reads an environment variable. | ||
pub fn read_env<T>(name: &str, default: T) -> Result<T> | ||
where | ||
T: FromStr, | ||
<T as FromStr>::Err: Error + Send + Sync + 'static, | ||
{ | ||
let config = std::env::var(name).ok(); | ||
match config.as_deref() { | ||
None | Some("") => Ok(default), | ||
Some(config) => config | ||
.parse() | ||
.with_context(|| anyhow!("Invalid value for {}", name)), | ||
} | ||
} | ||
|
||
/// Reads an boolean-like environment variable, where any value but "0", "no", | ||
/// or "false" is are considered true. | ||
pub fn read_env_bool(name: &str) -> bool { | ||
let config = std::env::var(name).ok(); | ||
!matches!( | ||
config.as_deref(), | ||
None | Some("") | Some("0") | Some("no") | Some("false") | ||
) | ||
} | ||
|
||
/// Reads a comma-separated environment variable as a vector. | ||
pub fn read_env_list<T>(name: &str, default: Vec<T>) -> Result<Vec<T>> | ||
where | ||
T: FromStr, | ||
<T as FromStr>::Err: Error + Send + Sync + 'static, | ||
{ | ||
let config = std::env::var(name).ok(); | ||
match config.as_deref() { | ||
None | Some("") => Ok(default), | ||
Some(config) => config | ||
.split(',') | ||
.map(|s| { | ||
s.parse() | ||
.with_context(|| anyhow!("Invalid value for {}", name)) | ||
}) | ||
.collect(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::{collections::HashMap, path::PathBuf}; | ||
|
||
use rand::{rngs::StdRng, seq::SliceRandom, SeedableRng}; | ||
|
||
/// Picks modules at random, but with a fixed seed so runs are somewhat | ||
/// reproducible. | ||
/// | ||
/// This must be initialized outside of `bench_with_input` so we don't repeat | ||
/// the same sequence in different samples. | ||
pub struct ModulePicker { | ||
depths: Vec<usize>, | ||
modules_by_depth: HashMap<usize, Vec<PathBuf>>, | ||
rng: parking_lot::Mutex<StdRng>, | ||
} | ||
|
||
impl ModulePicker { | ||
/// Creates a new module picker. | ||
pub fn new(mut modules: Vec<(PathBuf, usize)>) -> Self { | ||
let rng = StdRng::seed_from_u64(42); | ||
|
||
// Ensure the module order is deterministic. | ||
modules.sort(); | ||
|
||
let mut modules_by_depth: HashMap<_, Vec<_>> = HashMap::new(); | ||
for (module, depth) in modules { | ||
modules_by_depth.entry(depth).or_default().push(module); | ||
} | ||
let mut depths: Vec<_> = modules_by_depth.keys().copied().collect(); | ||
// Ensure the depth order is deterministic. | ||
depths.sort(); | ||
|
||
Self { | ||
depths, | ||
modules_by_depth, | ||
rng: parking_lot::Mutex::new(rng), | ||
} | ||
} | ||
|
||
/// Picks a random module with a uniform distribution over all depths. | ||
pub fn pick(&self) -> &PathBuf { | ||
let mut rng = self.rng.lock(); | ||
// Sample from all depths uniformly. | ||
let depth = self.depths.choose(&mut *rng).unwrap(); | ||
self.modules_by_depth[depth].choose(&mut *rng).unwrap() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters