|
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | + |
| 3 | +//! This bench monitors the performance of backtracking and term intersection. |
| 4 | +//! |
| 5 | +//! Dependencies are constructed in a way that all versions need to be tested before finding a solution. |
| 6 | +
|
| 7 | +use criterion::*; |
| 8 | +use pubgrub::OfflineDependencyProvider; |
| 9 | +use version_ranges::Ranges; |
| 10 | + |
| 11 | +/// This benchmark is a simplified reproduction of one of the patterns found in the `solana-*` crates from Cargo: |
| 12 | +/// * `solana-archiver-lib v1.1.12` depends on many layers of other solana crates with req `>= 1.1.12`. |
| 13 | +/// * each version `1.x.y` higher than `1.5.15` of a solana crate depends on other solana crates with req `= 1.x.y`. |
| 14 | +/// * `solana-crate-features` depends on `cc` with the `num_cpus` feature, which doesn't exist in recent versions of `cc`. |
| 15 | +fn backtracking_singletons(c: &mut Criterion, package_count: u32, version_count: u32) { |
| 16 | + let mut dependency_provider = OfflineDependencyProvider::<u32, Ranges<u32>>::new(); |
| 17 | + |
| 18 | + dependency_provider.add_dependencies(0u32, 0u32, [(1u32, Ranges::full())]); |
| 19 | + dependency_provider.add_dependencies(1u32, 0u32, []); |
| 20 | + |
| 21 | + for n in 1..package_count { |
| 22 | + for v in 1..version_count { |
| 23 | + dependency_provider.add_dependencies(n, v, [(n + 1, Ranges::singleton(v))]); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + c.bench_function("backtracking_singletons", |b| { |
| 28 | + b.iter(|| { |
| 29 | + let _ = pubgrub::resolve(&dependency_provider, 0u32, 0u32); |
| 30 | + }) |
| 31 | + }); |
| 32 | +} |
| 33 | + |
| 34 | +/// This benchmark is a simplified reproduction of one of the patterns found in the `solana-*` crates from Cargo: |
| 35 | +/// * `solana-archiver-lib v1.1.12` depends on many layers of other solana crates with req `>= 1.1.12`. |
| 36 | +/// * `solana-archiver-lib v1.1.12` also depends on `ed25519-dalek v1.0.0-pre.3`. |
| 37 | +/// * each version `1.x.y` higher than `1.5.15` of a solana crate depends on other solana crates with req `= 1.x.y`. |
| 38 | +/// * `solana-crate-features >= 1.2.17` depends on `ed25519-dalek v1.0.0-pre.4` or a higher incompatible version. |
| 39 | +fn backtracking_disjoint_versions(c: &mut Criterion, package_count: u32, version_count: u32) { |
| 40 | + let mut dependency_provider = OfflineDependencyProvider::<u32, Ranges<u32>>::new(); |
| 41 | + |
| 42 | + let root_deps = [(1u32, Ranges::full()), (u32::MAX, Ranges::singleton(0u32))]; |
| 43 | + dependency_provider.add_dependencies(0u32, 0u32, root_deps); |
| 44 | + |
| 45 | + dependency_provider.add_dependencies(1u32, 0u32, []); |
| 46 | + |
| 47 | + for n in 1..package_count { |
| 48 | + for v in 1..version_count { |
| 49 | + dependency_provider.add_dependencies(n, v, [(n + 1, Ranges::singleton(v))]); |
| 50 | + } |
| 51 | + } |
| 52 | + for v in 1..version_count { |
| 53 | + dependency_provider.add_dependencies(package_count, v, [(u32::MAX, Ranges::singleton(v))]); |
| 54 | + } |
| 55 | + |
| 56 | + for v in 0..version_count { |
| 57 | + dependency_provider.add_dependencies(u32::MAX, v, []); |
| 58 | + } |
| 59 | + |
| 60 | + c.bench_function("backtracking_disjoint_versions", |b| { |
| 61 | + b.iter(|| { |
| 62 | + let _ = pubgrub::resolve(&dependency_provider, 0u32, 0u32); |
| 63 | + }) |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +/// This benchmark is a simplified reproduction of one of the patterns found in the `solana-*` crates from Cargo: |
| 68 | +/// * `solana-archiver-lib v1.1.12` depends on many layers of other solana crates with req `>= 1.1.12`. |
| 69 | +/// * each version `1.x.y` lower than `1.5.14` of a solana crate depends on other solana crates with req `>= 1.x.y`. |
| 70 | +/// * `solana-crate-features` depends on `cc` with the `num_cpus` feature, which doesn't exist in recent versions of `cc`. |
| 71 | +fn backtracking_ranges(c: &mut Criterion, package_count: u32, version_count: u32) { |
| 72 | + let mut dependency_provider = OfflineDependencyProvider::<u32, Ranges<u32>>::new(); |
| 73 | + |
| 74 | + dependency_provider.add_dependencies(0u32, 0u32, [(1u32, Ranges::full())]); |
| 75 | + dependency_provider.add_dependencies(1u32, 0u32, []); |
| 76 | + |
| 77 | + for n in 1..package_count { |
| 78 | + for v in 1..version_count { |
| 79 | + let r = Ranges::higher_than(version_count - v); |
| 80 | + dependency_provider.add_dependencies(n, v, [(n + 1, r)]); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + c.bench_function("backtracking_ranges", |b| { |
| 85 | + b.iter(|| { |
| 86 | + let _ = pubgrub::resolve(&dependency_provider, 0u32, 0u32); |
| 87 | + }) |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +fn bench_group(c: &mut Criterion) { |
| 92 | + backtracking_singletons(c, 100, 500); |
| 93 | + backtracking_disjoint_versions(c, 300, 200); |
| 94 | + backtracking_ranges(c, 5, 200); |
| 95 | +} |
| 96 | + |
| 97 | +criterion_group!(benches, bench_group); |
| 98 | +criterion_main!(benches); |
0 commit comments