gonum.org/v1/gonum@v0.15.1-0.20240517103525-f853624cb1bb/graph/topo/bench_test.go (about)

     1  // Copyright ©2015 The Gonum Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package topo
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"golang.org/x/exp/rand"
    12  
    13  	"gonum.org/v1/gonum/graph"
    14  	"gonum.org/v1/gonum/graph/graphs/gen"
    15  	"gonum.org/v1/gonum/graph/simple"
    16  )
    17  
    18  var (
    19  	gnpDirected_10_tenth   = gnpDirected(10, 0.1)
    20  	gnpDirected_100_tenth  = gnpDirected(100, 0.1)
    21  	gnpDirected_1000_tenth = gnpDirected(1000, 0.1)
    22  	gnpDirected_10_half    = gnpDirected(10, 0.5)
    23  	gnpDirected_100_half   = gnpDirected(100, 0.5)
    24  	gnpDirected_1000_half  = gnpDirected(1000, 0.5)
    25  
    26  	gnpUndirected_10_tenth  = gnpUndirected(10, 0.1)
    27  	gnpUndirected_100_tenth = gnpUndirected(100, 0.1)
    28  	gnpUndirected_10_half   = gnpUndirected(10, 0.5)
    29  	gnpUndirected_100_half  = gnpUndirected(100, 0.5)
    30  
    31  	pathDirected_10     = pathDirected(10)
    32  	pathDirected_1000   = pathDirected(1e3)
    33  	pathDirected_100000 = pathDirected(1e5)
    34  )
    35  
    36  func gnpDirected(n int, p float64) graph.Directed {
    37  	g := simple.NewDirectedGraph()
    38  	err := gen.Gnp(g, n, p, rand.NewSource(1))
    39  	if err != nil {
    40  		panic(fmt.Sprintf("topo: bad test: %v", err))
    41  	}
    42  	return g
    43  }
    44  
    45  func gnpUndirected(n int, p float64) graph.Undirected {
    46  	g := simple.NewUndirectedGraph()
    47  	err := gen.Gnp(g, n, p, rand.NewSource(1))
    48  	if err != nil {
    49  		panic(fmt.Sprintf("topo: bad test: %v", err))
    50  	}
    51  	return g
    52  }
    53  
    54  func pathDirected(n int) graph.Directed {
    55  	g := simple.NewDirectedGraph()
    56  	var idSet gen.IDSet
    57  	for i := 0; i < n; i++ {
    58  		idSet = append(idSet, int64(i))
    59  	}
    60  	gen.Path(g, idSet)
    61  	return g
    62  }
    63  
    64  func benchmarkTarjanSCC(b *testing.B, g graph.Directed) {
    65  	var sccs [][]graph.Node
    66  	for i := 0; i < b.N; i++ {
    67  		sccs = TarjanSCC(g)
    68  	}
    69  	if len(sccs) == 0 {
    70  		b.Fatal("unexpected number zero-sized SCC set")
    71  	}
    72  }
    73  
    74  func BenchmarkTarjanSCCGnp_10_tenth(b *testing.B) {
    75  	benchmarkTarjanSCC(b, gnpDirected_10_tenth)
    76  }
    77  func BenchmarkTarjanSCCGnp_100_tenth(b *testing.B) {
    78  	benchmarkTarjanSCC(b, gnpDirected_100_tenth)
    79  }
    80  func BenchmarkTarjanSCCGnp_1000_tenth(b *testing.B) {
    81  	benchmarkTarjanSCC(b, gnpDirected_1000_tenth)
    82  }
    83  func BenchmarkTarjanSCCGnp_10_half(b *testing.B) {
    84  	benchmarkTarjanSCC(b, gnpDirected_10_half)
    85  }
    86  func BenchmarkTarjanSCCGnp_100_half(b *testing.B) {
    87  	benchmarkTarjanSCC(b, gnpDirected_100_half)
    88  }
    89  func BenchmarkTarjanSCCGnp_1000_half(b *testing.B) {
    90  	benchmarkTarjanSCC(b, gnpDirected_1000_half)
    91  }
    92  
    93  func benchmarkDirectedCyclesIn(b *testing.B, g graph.Directed) {
    94  	for i := 0; i < b.N; i++ {
    95  		if len(DirectedCyclesIn(g)) == 0 {
    96  			b.Fatal("unexpected zero-sized cycles set")
    97  		}
    98  	}
    99  }
   100  
   101  func BenchmarkDirectedCyclesInGnp_10_tenth(b *testing.B) {
   102  	benchmarkDirectedCyclesIn(b, gnpDirected_10_tenth)
   103  }
   104  func BenchmarkDirectedCyclesInGnp_10_half(b *testing.B) {
   105  	benchmarkDirectedCyclesIn(b, gnpDirected_10_half)
   106  }
   107  
   108  func benchmarkUndirectedCyclesIn(b *testing.B, g graph.Undirected) {
   109  	for i := 0; i < b.N; i++ {
   110  		if len(UndirectedCyclesIn(g)) == 0 {
   111  			b.Fatal("unexpected zero-sized cycles set")
   112  		}
   113  	}
   114  }
   115  
   116  func BenchmarkUndirectedCyclesInGnp_10_tenth(b *testing.B) {
   117  	benchmarkUndirectedCyclesIn(b, gnpUndirected_10_tenth)
   118  }
   119  func BenchmarkUndirectedCyclesInGnp_100_tenth(b *testing.B) {
   120  	benchmarkUndirectedCyclesIn(b, gnpUndirected_100_tenth)
   121  }
   122  func BenchmarkUndirectedCyclesInGnp_10_half(b *testing.B) {
   123  	benchmarkUndirectedCyclesIn(b, gnpUndirected_10_half)
   124  }
   125  func BenchmarkUndirectedCyclesInGnp_100_half(b *testing.B) {
   126  	benchmarkUndirectedCyclesIn(b, gnpUndirected_100_half)
   127  }
   128  
   129  func benchmarkSort(b *testing.B, g graph.Directed) {
   130  	for i := 0; i < b.N; i++ {
   131  		_, err := Sort(g)
   132  		if err != nil {
   133  			b.FailNow()
   134  		}
   135  	}
   136  }
   137  
   138  func BenchmarkSortGnp_10_tenth(b *testing.B) {
   139  	benchmarkSort(b, gnpDirected_10_tenth)
   140  }
   141  func BenchmarkSortGnp_100_tenth(b *testing.B) {
   142  	benchmarkSort(b, gnpDirected_100_tenth)
   143  }
   144  func BenchmarkSortGnp_1000_tenth(b *testing.B) {
   145  	benchmarkSort(b, gnpDirected_1000_tenth)
   146  }
   147  func BenchmarkSortGnp_10_half(b *testing.B) {
   148  	benchmarkSort(b, gnpDirected_10_half)
   149  }
   150  func BenchmarkSortGnp_100_half(b *testing.B) {
   151  	benchmarkSort(b, gnpDirected_100_half)
   152  }
   153  func BenchmarkSortGnp_1000_half(b *testing.B) {
   154  	benchmarkSort(b, gnpDirected_1000_half)
   155  }
   156  func BenchmarkSortPath_10(b *testing.B) {
   157  	benchmarkSort(b, pathDirected_10)
   158  }
   159  func BenchmarkSortPath_1000(b *testing.B) {
   160  	benchmarkSort(b, pathDirected_1000)
   161  }
   162  func BenchmarkSortPath_100000(b *testing.B) {
   163  	benchmarkSort(b, pathDirected_100000)
   164  }
   165  
   166  func benchmarkSortStabilized(b *testing.B, g graph.Directed) {
   167  	for i := 0; i < b.N; i++ {
   168  		_, err := SortStabilized(g, nil)
   169  		if err != nil {
   170  			b.FailNow()
   171  		}
   172  	}
   173  }
   174  
   175  func BenchmarkSortStabilizedGnp_10_tenth(b *testing.B) {
   176  	benchmarkSortStabilized(b, gnpDirected_10_tenth)
   177  }
   178  func BenchmarkSortStabilizedGnp_100_tenth(b *testing.B) {
   179  	benchmarkSortStabilized(b, gnpDirected_100_tenth)
   180  }
   181  func BenchmarkSortStabilizedGnp_1000_tenth(b *testing.B) {
   182  	benchmarkSortStabilized(b, gnpDirected_1000_tenth)
   183  }
   184  func BenchmarkSortStabilizedGnp_10_half(b *testing.B) {
   185  	benchmarkSortStabilized(b, gnpDirected_10_half)
   186  }
   187  func BenchmarkSortStabilizedGnp_100_half(b *testing.B) {
   188  	benchmarkSortStabilized(b, gnpDirected_100_half)
   189  }
   190  func BenchmarkSortStabilizedGnp_1000_half(b *testing.B) {
   191  	benchmarkSortStabilized(b, gnpDirected_1000_half)
   192  }
   193  func BenchmarkSortStabilizedPath_10(b *testing.B) {
   194  	benchmarkSortStabilized(b, pathDirected_10)
   195  }
   196  func BenchmarkSortStabilizedPath_1000(b *testing.B) {
   197  	benchmarkSortStabilized(b, pathDirected_1000)
   198  }
   199  func BenchmarkSortStabilizedPath_100000(b *testing.B) {
   200  	benchmarkSortStabilized(b, pathDirected_100000)
   201  }