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

     1  // Copyright ©2024 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 dot
     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/multi"
    16  	"gonum.org/v1/gonum/graph/simple"
    17  )
    18  
    19  var (
    20  	gnpDirected_10_tenth   = gnpDirected(10, 0.1)
    21  	gnpDirected_100_tenth  = gnpDirected(100, 0.1)
    22  	gnpDirected_1000_tenth = gnpDirected(1000, 0.1)
    23  	gnpDirected_10_half    = gnpDirected(10, 0.5)
    24  	gnpDirected_100_half   = gnpDirected(100, 0.5)
    25  	gnpDirected_1000_half  = gnpDirected(1000, 0.5)
    26  
    27  	powerLawMultiDirected_10_tenth   = powerLawMultiDirected(10, 1)
    28  	powerLawMultiDirected_100_tenth  = powerLawMultiDirected(100, 10)
    29  	powerLawMultiDirected_1000_tenth = powerLawMultiDirected(1000, 100)
    30  	powerLawMultiDirected_10_half    = powerLawMultiDirected(10, 5)
    31  	powerLawMultiDirected_100_half   = powerLawMultiDirected(100, 50)
    32  	powerLawMultiDirected_1000_half  = powerLawMultiDirected(1000, 500)
    33  )
    34  
    35  func gnpDirected(n int, p float64) graph.Directed {
    36  	g := simple.NewDirectedGraph()
    37  	err := gen.Gnp(g, n, p, rand.NewSource(1))
    38  	if err != nil {
    39  		panic(fmt.Sprintf("dot: bad test: %v", err))
    40  	}
    41  	return g
    42  }
    43  
    44  func powerLawMultiDirected(n, d int) graph.DirectedMultigraph {
    45  	g := multi.NewDirectedGraph()
    46  	err := gen.PowerLaw(g, n, d, rand.NewSource(1))
    47  	if err != nil {
    48  		panic(fmt.Sprintf("dot: bad test: %v", err))
    49  	}
    50  	return g
    51  }
    52  
    53  func benchmarkUnmarshal(b *testing.B, g graph.Directed) {
    54  	marshalled, err := Marshal(g, "g", "", "")
    55  	if err != nil {
    56  		b.Fatalf("dot: bad Marshal input: %v", err)
    57  	}
    58  
    59  	b.ResetTimer()
    60  	for i := 0; i < b.N; i++ {
    61  		if err := Unmarshal(marshalled, simple.NewDirectedGraph()); err != nil {
    62  			b.Fatalf("dot: bad Unmarshal input: %v", err)
    63  		}
    64  	}
    65  }
    66  
    67  func BenchmarkUnmarshalGnp_10_tenth(b *testing.B) {
    68  	benchmarkUnmarshal(b, gnpDirected_10_tenth)
    69  }
    70  func BenchmarkUnmarshalGnp_100_tenth(b *testing.B) {
    71  	benchmarkUnmarshal(b, gnpDirected_100_tenth)
    72  }
    73  func BenchmarkUnmarshalGnp_1000_tenth(b *testing.B) {
    74  	benchmarkUnmarshal(b, gnpDirected_1000_tenth)
    75  }
    76  func BenchmarkUnmarshalGnp_10_half(b *testing.B) {
    77  	benchmarkUnmarshal(b, gnpDirected_10_half)
    78  }
    79  func BenchmarkUnmarshalGnp_100_half(b *testing.B) {
    80  	benchmarkUnmarshal(b, gnpDirected_100_half)
    81  }
    82  func BenchmarkUnmarshalGnp_1000_half(b *testing.B) {
    83  	benchmarkUnmarshal(b, gnpDirected_1000_half)
    84  }
    85  
    86  func benchmarkUnmarshalMulti(b *testing.B, g graph.DirectedMultigraph) {
    87  	marshalled, err := MarshalMulti(g, "g", "", "")
    88  	if err != nil {
    89  		b.Fatalf("dot: bad Marshal input: %v", err)
    90  	}
    91  
    92  	b.ResetTimer()
    93  	for i := 0; i < b.N; i++ {
    94  		if err := UnmarshalMulti(marshalled, multi.NewDirectedGraph()); err != nil {
    95  			b.Fatalf("dot: bad Unmarshal input: %v", err)
    96  		}
    97  	}
    98  }
    99  
   100  func BenchmarkUnmarshalMultiPowerLaw_10_tenth(b *testing.B) {
   101  	benchmarkUnmarshalMulti(b, powerLawMultiDirected_10_tenth)
   102  }
   103  func BenchmarkUnmarshalMultiPowerLaw_100_tenth(b *testing.B) {
   104  	benchmarkUnmarshalMulti(b, powerLawMultiDirected_100_tenth)
   105  }
   106  func BenchmarkUnmarshalMultiPowerLaw_1000_tenth(b *testing.B) {
   107  	benchmarkUnmarshalMulti(b, powerLawMultiDirected_1000_tenth)
   108  }
   109  func BenchmarkUnmarshalMultiPowerLaw_10_half(b *testing.B) {
   110  	benchmarkUnmarshalMulti(b, powerLawMultiDirected_10_half)
   111  }
   112  func BenchmarkUnmarshalMultiPowerLaw_100_half(b *testing.B) {
   113  	benchmarkUnmarshalMulti(b, powerLawMultiDirected_100_half)
   114  }
   115  func BenchmarkUnmarshalMultiPowerLaw_1000_half(b *testing.B) {
   116  	benchmarkUnmarshalMulti(b, powerLawMultiDirected_1000_half)
   117  }