From 1bc1de45aa9f98d2063661226bf523f992d96fce Mon Sep 17 00:00:00 2001 From: Jonathan Bluett-Duncan Date: Wed, 20 Mar 2024 16:54:07 +0000 Subject: [PATCH] graph: add benchmarks for topo.Sort(Stabilized) --- graph/topo/bench_test.go | 94 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 2 deletions(-) diff --git a/graph/topo/bench_test.go b/graph/topo/bench_test.go index 82cdd600..ced7495a 100644 --- a/graph/topo/bench_test.go +++ b/graph/topo/bench_test.go @@ -27,6 +27,10 @@ var ( gnpUndirected_100_tenth = gnpUndirected(100, 0.1) gnpUndirected_10_half = gnpUndirected(10, 0.5) gnpUndirected_100_half = gnpUndirected(100, 0.5) + + pathDirected_10 = pathDirected(10) + pathDirected_1000 = pathDirected(1e3) + pathDirected_100000 = pathDirected(1e5) ) func gnpDirected(n int, p float64) graph.Directed { @@ -47,6 +51,16 @@ func gnpUndirected(n int, p float64) graph.Undirected { return g } +func pathDirected(n int) graph.Directed { + g := simple.NewDirectedGraph() + var idSet gen.IDSet + for i := 0; i < n; i++ { + idSet = append(idSet, int64(i)) + } + gen.Path(g, idSet) + return g +} + func benchmarkTarjanSCC(b *testing.B, g graph.Directed) { var sccs [][]graph.Node for i := 0; i < b.N; i++ { @@ -78,7 +92,9 @@ func BenchmarkTarjanSCCGnp_1000_half(b *testing.B) { func benchmarkDirectedCyclesIn(b *testing.B, g graph.Directed) { for i := 0; i < b.N; i++ { - DirectedCyclesIn(g) + if len(DirectedCyclesIn(g)) == 0 { + b.Fatal("unexpected zero-sized cycles set") + } } } @@ -91,7 +107,9 @@ func BenchmarkDirectedCyclesInGnp_10_half(b *testing.B) { func benchmarkUndirectedCyclesIn(b *testing.B, g graph.Undirected) { for i := 0; i < b.N; i++ { - UndirectedCyclesIn(g) + if len(UndirectedCyclesIn(g)) == 0 { + b.Fatal("unexpected zero-sized cycles set") + } } } @@ -107,3 +125,75 @@ func BenchmarkUndirectedCyclesInGnp_10_half(b *testing.B) { func BenchmarkUndirectedCyclesInGnp_100_half(b *testing.B) { benchmarkUndirectedCyclesIn(b, gnpUndirected_100_half) } + +func benchmarkSort(b *testing.B, g graph.Directed) { + for i := 0; i < b.N; i++ { + if _, err := Sort(g); err != nil { + b.FailNow() + } + } +} + +func BenchmarkSortGnp_10_tenth(b *testing.B) { + benchmarkSort(b, gnpDirected_10_tenth) +} +func BenchmarkSortGnp_100_tenth(b *testing.B) { + benchmarkSort(b, gnpDirected_100_tenth) +} +func BenchmarkSortGnp_1000_tenth(b *testing.B) { + benchmarkSort(b, gnpDirected_1000_tenth) +} +func BenchmarkSortGnp_10_half(b *testing.B) { + benchmarkSort(b, gnpDirected_10_half) +} +func BenchmarkSortGnp_100_half(b *testing.B) { + benchmarkSort(b, gnpDirected_100_half) +} +func BenchmarkSortGnp_1000_half(b *testing.B) { + benchmarkSort(b, gnpDirected_1000_half) +} +func BenchmarkSortPath_10(b *testing.B) { + benchmarkSort(b, pathDirected_10) +} +func BenchmarkSortPath_1000(b *testing.B) { + benchmarkSort(b, pathDirected_1000) +} +func BenchmarkSortPath_100000(b *testing.B) { + benchmarkSort(b, pathDirected_100000) +} + +func benchmarkSortStabilized(b *testing.B, g graph.Directed) { + for i := 0; i < b.N; i++ { + if _, err := SortStabilized(g, nil); err != nil { + b.FailNow() + } + } +} + +func BenchmarkSortStabilizedGnp_10_tenth(b *testing.B) { + benchmarkSortStabilized(b, gnpDirected_10_tenth) +} +func BenchmarkSortStabilizedGnp_100_tenth(b *testing.B) { + benchmarkSortStabilized(b, gnpDirected_100_tenth) +} +func BenchmarkSortStabilizedGnp_1000_tenth(b *testing.B) { + benchmarkSortStabilized(b, gnpDirected_1000_tenth) +} +func BenchmarkSortStabilizedGnp_10_half(b *testing.B) { + benchmarkSortStabilized(b, gnpDirected_10_half) +} +func BenchmarkSortStabilizedGnp_100_half(b *testing.B) { + benchmarkSortStabilized(b, gnpDirected_100_half) +} +func BenchmarkSortStabilizedGnp_1000_half(b *testing.B) { + benchmarkSortStabilized(b, gnpDirected_1000_half) +} +func BenchmarkSortStabilizedPath_10(b *testing.B) { + benchmarkSortStabilized(b, pathDirected_10) +} +func BenchmarkSortStabilizedPath_1000(b *testing.B) { + benchmarkSortStabilized(b, pathDirected_1000) +} +func BenchmarkSortStabilizedPath_100000(b *testing.B) { + benchmarkSortStabilized(b, pathDirected_100000) +}