use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; use precedence_net::{DurationType, Network, NetworkBuilder, StartType}; const SIZES: [i32; 6] = [20, 100, 500, 2_500, 12_500, 62_500]; fn create_network_builder(size: i32) -> NetworkBuilder { let mut network_builder = Network::builder(); for i in 1..size { network_builder .add_extended_activity( &format!("{i}"), &format!("{i}"), 1.0, 2.0, 3.0, StartType::Earliest, DurationType::Expected, ) .unwrap(); } for i in 1..size - 1 { let current_ref = &format!("{i}"); let next_ref = &format!("{}", i + 1); network_builder.connect(current_ref, next_ref).unwrap(); } network_builder } pub fn critical_path_benchmark(c: &mut Criterion) { let mut group = c.benchmark_group("Critical Path"); for size in SIZES { group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &s| { let network = Network::try_from(create_network_builder(s)).unwrap(); b.iter(|| network.critical_path_activities()); }); } group.finish(); } pub fn add_activities_benchmark(c: &mut Criterion) { let mut group = c.benchmark_group("Add Activities"); for size in SIZES { group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &s| { b.iter(|| create_network_builder(s)); }); } group.finish(); } pub fn create_network_benchmark(c: &mut Criterion) { let mut group = c.benchmark_group("NetworkBuilder -> Network"); for size in SIZES { group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &s| { let mut network_builder = create_network_builder(s); b.iter(|| Network::try_from(&mut network_builder).unwrap()); }); } group.finish(); } pub fn create_network_builder_benchmark(c: &mut Criterion) { let mut group = c.benchmark_group("Network -> NeworkBuilder"); for size in SIZES { group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &s| { let network = Network::try_from(create_network_builder(s)).unwrap(); b.iter(move || NetworkBuilder::from(&network)); }); } group.finish(); } pub fn run_benchmarks(c: &mut Criterion) { add_activities_benchmark(c); create_network_benchmark(c); create_network_builder_benchmark(c); critical_path_benchmark(c); } criterion_group!(benches, run_benchmarks); criterion_main!(benches);