forked from astral-sh/uv
/
uv.rs
96 lines (82 loc) · 2.98 KB
/
uv.rs
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! Benchmarks uv using criterion.
//!
//! The benchmarks in this file assume that the `uv` executable is available.
//!
//! To set up the required environment, run:
//!
//! ```shell
//! cargo build --release
//! ./target/release/uv venv
//! source .venv/bin/activate
//! ```
use std::env;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use bench::criterion::{criterion_group, criterion_main, measurement::WallTime, Criterion};
use criterion::BatchSize;
use fs_err as fs;
const REQUIREMENTS_DIR: &str = "../../scripts/requirements";
const REQUIREMENTS: [&str; 2] = ["trio.in", "home-assistant.in"];
const COMPILED: [&str; 2] = ["compiled/trio.txt", "home-assistant.in"];
fn resolve_warm(c: &mut Criterion<WallTime>) {
for requirements in REQUIREMENTS {
let input = fs::canonicalize(PathBuf::from_iter([REQUIREMENTS_DIR, requirements])).unwrap();
let name = format!(
"resolve_warm_{}",
input.file_stem().unwrap().to_string_lossy()
);
let temp_dir = tempfile::tempdir().unwrap();
let root = env::current_dir().unwrap();
env::set_current_dir(&temp_dir).unwrap();
let output = "requirements.txt";
let setup = || fs::remove_file(output).ok();
let run = |_| {
Command::new("uv")
.args(["pip", "compile"])
.arg(&input)
.args(["--cache-dir", ".cache"])
.args(["--output-file", output])
.current_dir(&temp_dir)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
};
c.bench_function(&name, |b| b.iter_batched(setup, run, BatchSize::SmallInput));
env::set_current_dir(&root).unwrap();
}
}
fn install_warm(c: &mut Criterion<WallTime>) {
for requirements in COMPILED {
let input = fs::canonicalize(PathBuf::from_iter([REQUIREMENTS_DIR, requirements])).unwrap();
let name = format!(
"install_warm_{}",
input.file_stem().unwrap().to_string_lossy()
);
let temp_dir = tempfile::tempdir().unwrap();
let root = env::current_dir().unwrap();
env::set_current_dir(&temp_dir).unwrap();
let setup = || {
Command::new("uv")
.args(["venv", ".venv", "-p", "3.12"])
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
};
let run = |()| {
Command::new("uv")
.args(["pip", "sync"])
.arg(&input)
.args(["--cache-dir", ".cache"])
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
};
c.bench_function(&name, |b| b.iter_batched(setup, run, BatchSize::SmallInput));
env::set_current_dir(&root).unwrap();
}
}
criterion_group!(uv, install_warm, resolve_warm);
criterion_main!(uv);