diff --git a/crates/uv-bench/Cargo.toml b/crates/uv-bench/Cargo.toml index 655818745ee..1087aadae0f 100644 --- a/crates/uv-bench/Cargo.toml +++ b/crates/uv-bench/Cargo.toml @@ -58,6 +58,11 @@ name = "package_build" path = "benches/package_build.rs" harness = false +[[bench]] +name = "core_metadata" +path = "benches/core_metadata.rs" +harness = false + [[bench]] name = "uv_pypi_types" path = "benches/uv_pypi_types.rs" diff --git a/crates/uv-bench/benches/core_metadata.rs b/crates/uv-bench/benches/core_metadata.rs new file mode 100644 index 00000000000..79714a67f6f --- /dev/null +++ b/crates/uv-bench/benches/core_metadata.rs @@ -0,0 +1,58 @@ +//! Parsing and formatting dependencies from published wheel metadata. + +extern crate uv_performance_memory_allocator; + +use std::hint::black_box; +use std::str::FromStr; + +use criterion::{ + BenchmarkId, Criterion, Throughput, criterion_group, criterion_main, measurement::WallTime, +}; +use uv_bench::fixture_path; +use uv_pep508::Requirement; +use uv_pypi_types::{ResolutionMetadata, VerbatimParsedUrl}; + +fn core_metadata(c: &mut Criterion) { + let mut group = c.benchmark_group("core_metadata"); + for project in ["flask", "jupyterlab", "airflow"] { + let input = fs_err::read(fixture_path(&format!("{project}.metadata"))) + .expect("Failed to read core metadata"); + let metadata = + ResolutionMetadata::parse_metadata(&input).expect("Invalid core metadata fixture"); + let requirements: Vec = metadata + .requires_dist + .iter() + .map(ToString::to_string) + .collect(); + + group.throughput(Throughput::Bytes(input.len() as u64)); + group.bench_function(BenchmarkId::new("read", project), |b| { + b.iter(|| { + ResolutionMetadata::parse_metadata(black_box(&input)) + .expect("Failed to parse core metadata") + }); + }); + group.throughput(Throughput::Elements(requirements.len() as u64)); + group.bench_function(BenchmarkId::new("parse_requirements", project), |b| { + b.iter(|| { + black_box(&requirements) + .iter() + .map(|requirement| Requirement::::from_str(requirement)) + .collect::, _>>() + .expect("Failed to parse a wheel requirement") + }); + }); + group.bench_function(BenchmarkId::new("format_requirements", project), |b| { + b.iter(|| { + black_box(&metadata.requires_dist) + .iter() + .map(ToString::to_string) + .collect::>() + }); + }); + } + group.finish(); +} + +criterion_group!(metadata, core_metadata); +criterion_main!(metadata); diff --git a/scripts/benchmark/fixtures.json b/scripts/benchmark/fixtures.json index d68f6360ad9..f274a58cb51 100644 --- a/scripts/benchmark/fixtures.json +++ b/scripts/benchmark/fixtures.json @@ -1,4 +1,19 @@ [ + { + "filename": "flask.metadata", + "url": "https://files.pythonhosted.org/packages/ec/f9/7f9263c5695f4bd0023734af91bedb2ff8209e8de6ead162f35d8dc762fd/flask-3.1.2-py3-none-any.whl.metadata", + "sha256": "a1183adc300021ca0b02bee4cd380810a7e6f3fe07313469996988a6d758fe3b" + }, + { + "filename": "jupyterlab.metadata", + "url": "https://files.pythonhosted.org/packages/7e/01/44f35124896dd5c73b26705c25bb8af2089895b32f057a1e4a3488847333/jupyterlab-4.4.7-py3-none-any.whl.metadata", + "sha256": "5693246270849222b2c317cf8dcb3f1120fb62f1638c5ae9ccf0d127c0473e4b" + }, + { + "filename": "airflow.metadata", + "url": "https://files.pythonhosted.org/packages/f2/52/bac9f8a7cf28d7f9d57712ac26dc47936f9b3d55ab547d9c083ed255a27d/apache_airflow-2.9.3-py3-none-any.whl.metadata", + "sha256": "a2db3dd347bb1a2a9595c3802f446596d25ad9f8aade7ce1bc679a5838dd3b5c" + }, { "filename": "flask-3.1.2-py3-none-any.whl", "url": "https://files.pythonhosted.org/packages/ec/f9/7f9263c5695f4bd0023734af91bedb2ff8209e8de6ead162f35d8dc762fd/flask-3.1.2-py3-none-any.whl",