github.com/gopherd/gonum@v0.0.4/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  	"github.com/gopherd/gonum/graph"
    12  	"github.com/gopherd/gonum/graph/graphs/gen"
    13  	"github.com/gopherd/gonum/graph/simple"
    14  )
    15  
    16  var (
    17  	gnpDirected_10_tenth   = gnpDirected(10, 0.1)
    18  	gnpDirected_100_tenth  = gnpDirected(100, 0.1)
    19  	gnpDirected_1000_tenth = gnpDirected(1000, 0.1)
    20  	gnpDirected_10_half    = gnpDirected(10, 0.5)
    21  	gnpDirected_100_half   = gnpDirected(100, 0.5)
    22  	gnpDirected_1000_half  = gnpDirected(1000, 0.5)
    23  )
    24  
    25  func gnpDirected(n int, p float64) graph.Directed {
    26  	g := simple.NewDirectedGraph()
    27  	err := gen.Gnp(g, n, p, nil)
    28  	if err != nil {
    29  		panic(fmt.Sprintf("topo: bad test: %v", err))
    30  	}
    31  	return g
    32  }
    33  
    34  func benchmarkTarjanSCC(b *testing.B, g graph.Directed) {
    35  	var sccs [][]graph.Node
    36  	for i := 0; i < b.N; i++ {
    37  		sccs = TarjanSCC(g)
    38  	}
    39  	if len(sccs) == 0 {
    40  		b.Fatal("unexpected number zero-sized SCC set")
    41  	}
    42  }
    43  
    44  func BenchmarkTarjanSCCGnp_10_tenth(b *testing.B) {
    45  	benchmarkTarjanSCC(b, gnpDirected_10_tenth)
    46  }
    47  func BenchmarkTarjanSCCGnp_100_tenth(b *testing.B) {
    48  	benchmarkTarjanSCC(b, gnpDirected_100_tenth)
    49  }
    50  func BenchmarkTarjanSCCGnp_1000_tenth(b *testing.B) {
    51  	benchmarkTarjanSCC(b, gnpDirected_1000_tenth)
    52  }
    53  func BenchmarkTarjanSCCGnp_10_half(b *testing.B) {
    54  	benchmarkTarjanSCC(b, gnpDirected_10_half)
    55  }
    56  func BenchmarkTarjanSCCGnp_100_half(b *testing.B) {
    57  	benchmarkTarjanSCC(b, gnpDirected_100_half)
    58  }
    59  func BenchmarkTarjanSCCGnp_1000_half(b *testing.B) {
    60  	benchmarkTarjanSCC(b, gnpDirected_1000_half)
    61  }