|
1 |
| -use criterion::{black_box, criterion_group, AxisScale, BenchmarkId, Criterion, PlotConfiguration}; |
| 1 | +use criterion::{criterion_group, Criterion}; |
2 | 2 | use itertools::Itertools;
|
3 | 3 | use portgraph::{algorithms::TopoConvexChecker, PortView};
|
| 4 | +use portgraph::{NodeIndex, PortGraph}; |
4 | 5 |
|
5 |
| -use super::generators::make_two_track_dag; |
6 |
| - |
7 |
| -fn bench_convex_construction(c: &mut Criterion) { |
8 |
| - let mut g = c.benchmark_group("initialize convex checker object"); |
9 |
| - g.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic)); |
10 |
| - |
11 |
| - for size in [100, 1_000, 10_000] { |
12 |
| - g.bench_with_input( |
13 |
| - BenchmarkId::new("initalize_convexity", size), |
14 |
| - &size, |
15 |
| - |b, size| { |
16 |
| - let graph = make_two_track_dag(*size); |
17 |
| - b.iter(|| black_box(TopoConvexChecker::new(&graph))) |
18 |
| - }, |
19 |
| - ); |
| 6 | +use crate::helpers::*; |
| 7 | + |
| 8 | +// ----------------------------------------------------------------------------- |
| 9 | +// Benchmark functions |
| 10 | +// ----------------------------------------------------------------------------- |
| 11 | + |
| 12 | +struct ConvexConstruction { |
| 13 | + graph: PortGraph, |
| 14 | +} |
| 15 | +impl SizedBenchmark for ConvexConstruction { |
| 16 | + fn name() -> &'static str { |
| 17 | + "initialize_convexity" |
| 18 | + } |
| 19 | + |
| 20 | + fn setup(size: usize) -> Self { |
| 21 | + let graph = make_two_track_dag(size); |
| 22 | + Self { graph } |
| 23 | + } |
| 24 | + |
| 25 | + fn run(&self) -> impl Sized { |
| 26 | + TopoConvexChecker::new(&self.graph) |
20 | 27 | }
|
21 |
| - g.finish(); |
22 | 28 | }
|
23 | 29 |
|
24 | 30 | /// We benchmark the worst case scenario, where the "subgraph" is the
|
25 | 31 | /// entire graph itself.
|
26 |
| -fn bench_convex_full(c: &mut Criterion) { |
27 |
| - let mut g = c.benchmark_group("Runtime convexity check. Full graph."); |
28 |
| - g.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic)); |
| 32 | +struct ConvexFull { |
| 33 | + checker: TopoConvexChecker<PortGraph>, |
| 34 | + nodes: Vec<NodeIndex>, |
| 35 | +} |
| 36 | +impl SizedBenchmark for ConvexFull { |
| 37 | + fn name() -> &'static str { |
| 38 | + "check_convexity_full" |
| 39 | + } |
29 | 40 |
|
30 |
| - for size in [100, 1_000, 10_000] { |
| 41 | + fn setup(size: usize) -> Self { |
31 | 42 | let graph = make_two_track_dag(size);
|
32 |
| - let checker = TopoConvexChecker::new(&graph); |
33 |
| - g.bench_with_input( |
34 |
| - BenchmarkId::new("check_convexity_full", size), |
35 |
| - &size, |
36 |
| - |b, _size| b.iter(|| black_box(checker.is_node_convex(graph.nodes_iter()))), |
37 |
| - ); |
| 43 | + let nodes = graph.nodes_iter().collect_vec(); |
| 44 | + let checker = TopoConvexChecker::new(graph); |
| 45 | + Self { checker, nodes } |
| 46 | + } |
| 47 | + |
| 48 | + fn run(&self) -> impl Sized { |
| 49 | + self.checker.is_node_convex(self.nodes.iter().copied()) |
38 | 50 | }
|
39 |
| - g.finish(); |
40 | 51 | }
|
41 | 52 |
|
42 | 53 | /// We benchmark the an scenario where the size of the "subgraph" is sub-linear on the size of the graph.
|
43 |
| -fn bench_convex_sparse(c: &mut Criterion) { |
44 |
| - let mut g = c.benchmark_group("Runtime convexity check. Sparse subgraph on an n^2 size graph."); |
45 |
| - g.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic)); |
46 |
| - |
47 |
| - for size in [100usize, 1_000, 5_000] { |
48 |
| - let graph_size = size.pow(2); |
49 |
| - let graph = make_two_track_dag(graph_size); |
50 |
| - let checker = TopoConvexChecker::new(&graph); |
51 |
| - let nodes = graph.nodes_iter().step_by(graph_size / size).collect_vec(); |
52 |
| - g.bench_with_input( |
53 |
| - BenchmarkId::new("check_convexity_sparse", size), |
54 |
| - &size, |
55 |
| - |b, _size| b.iter(|| black_box(checker.is_node_convex(nodes.iter().copied()))), |
56 |
| - ); |
| 54 | +struct ConvexSparse { |
| 55 | + checker: TopoConvexChecker<PortGraph>, |
| 56 | + nodes: Vec<NodeIndex>, |
| 57 | +} |
| 58 | +impl SizedBenchmark for ConvexSparse { |
| 59 | + fn name() -> &'static str { |
| 60 | + "check_convexity_sparse" |
| 61 | + } |
| 62 | + |
| 63 | + fn setup(size: usize) -> Self { |
| 64 | + let graph = make_two_track_dag(size); |
| 65 | + let subgraph_size = (size as f64).sqrt().floor() as usize; |
| 66 | + let nodes = graph |
| 67 | + .nodes_iter() |
| 68 | + .step_by(size / subgraph_size) |
| 69 | + .collect_vec(); |
| 70 | + let checker = TopoConvexChecker::new(graph); |
| 71 | + Self { checker, nodes } |
| 72 | + } |
| 73 | + |
| 74 | + fn run(&self) -> impl Sized { |
| 75 | + self.checker.is_node_convex(self.nodes.iter().copied()) |
57 | 76 | }
|
58 |
| - g.finish(); |
59 | 77 | }
|
60 | 78 |
|
| 79 | +// ----------------------------------------------------------------------------- |
| 80 | +// iai_callgrind definitions |
| 81 | +// ----------------------------------------------------------------------------- |
| 82 | + |
| 83 | +sized_iai_benchmark!(callgrind_convex_construction, ConvexConstruction); |
| 84 | +sized_iai_benchmark!(callgrind_convex_full, ConvexFull); |
| 85 | +sized_iai_benchmark!(callgrind_convex_sparse, ConvexSparse); |
| 86 | + |
| 87 | +iai_callgrind::library_benchmark_group!( |
| 88 | + name = callgrind_group; |
| 89 | + benchmarks = |
| 90 | + callgrind_convex_construction, |
| 91 | + callgrind_convex_full, |
| 92 | + callgrind_convex_sparse, |
| 93 | +); |
| 94 | + |
| 95 | +// ----------------------------------------------------------------------------- |
| 96 | +// Criterion definitions |
| 97 | +// ----------------------------------------------------------------------------- |
| 98 | + |
61 | 99 | criterion_group! {
|
62 |
| - name = benches; |
| 100 | + name = criterion_group; |
63 | 101 | config = Criterion::default();
|
64 | 102 | targets =
|
65 |
| - bench_convex_full, |
66 |
| - bench_convex_sparse, |
67 |
| - bench_convex_construction |
| 103 | + ConvexConstruction::criterion, |
| 104 | + ConvexFull::criterion, |
| 105 | + ConvexSparse::criterion, |
68 | 106 | }
|
0 commit comments