File: bench_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (117 lines) | stat: -rw-r--r-- 3,572 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright ©2024 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package dot

import (
	"fmt"
	"testing"

	"golang.org/x/exp/rand"

	"gonum.org/v1/gonum/graph"
	"gonum.org/v1/gonum/graph/graphs/gen"
	"gonum.org/v1/gonum/graph/multi"
	"gonum.org/v1/gonum/graph/simple"
)

var (
	gnpDirected_10_tenth   = gnpDirected(10, 0.1)
	gnpDirected_100_tenth  = gnpDirected(100, 0.1)
	gnpDirected_1000_tenth = gnpDirected(1000, 0.1)
	gnpDirected_10_half    = gnpDirected(10, 0.5)
	gnpDirected_100_half   = gnpDirected(100, 0.5)
	gnpDirected_1000_half  = gnpDirected(1000, 0.5)

	powerLawMultiDirected_10_tenth   = powerLawMultiDirected(10, 1)
	powerLawMultiDirected_100_tenth  = powerLawMultiDirected(100, 10)
	powerLawMultiDirected_1000_tenth = powerLawMultiDirected(1000, 100)
	powerLawMultiDirected_10_half    = powerLawMultiDirected(10, 5)
	powerLawMultiDirected_100_half   = powerLawMultiDirected(100, 50)
	powerLawMultiDirected_1000_half  = powerLawMultiDirected(1000, 500)
)

func gnpDirected(n int, p float64) graph.Directed {
	g := simple.NewDirectedGraph()
	err := gen.Gnp(g, n, p, rand.NewSource(1))
	if err != nil {
		panic(fmt.Sprintf("dot: bad test: %v", err))
	}
	return g
}

func powerLawMultiDirected(n, d int) graph.DirectedMultigraph {
	g := multi.NewDirectedGraph()
	err := gen.PowerLaw(g, n, d, rand.NewSource(1))
	if err != nil {
		panic(fmt.Sprintf("dot: bad test: %v", err))
	}
	return g
}

func benchmarkUnmarshal(b *testing.B, g graph.Directed) {
	marshalled, err := Marshal(g, "g", "", "")
	if err != nil {
		b.Fatalf("dot: bad Marshal input: %v", err)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if err := Unmarshal(marshalled, simple.NewDirectedGraph()); err != nil {
			b.Fatalf("dot: bad Unmarshal input: %v", err)
		}
	}
}

func BenchmarkUnmarshalGnp_10_tenth(b *testing.B) {
	benchmarkUnmarshal(b, gnpDirected_10_tenth)
}
func BenchmarkUnmarshalGnp_100_tenth(b *testing.B) {
	benchmarkUnmarshal(b, gnpDirected_100_tenth)
}
func BenchmarkUnmarshalGnp_1000_tenth(b *testing.B) {
	benchmarkUnmarshal(b, gnpDirected_1000_tenth)
}
func BenchmarkUnmarshalGnp_10_half(b *testing.B) {
	benchmarkUnmarshal(b, gnpDirected_10_half)
}
func BenchmarkUnmarshalGnp_100_half(b *testing.B) {
	benchmarkUnmarshal(b, gnpDirected_100_half)
}
func BenchmarkUnmarshalGnp_1000_half(b *testing.B) {
	benchmarkUnmarshal(b, gnpDirected_1000_half)
}

func benchmarkUnmarshalMulti(b *testing.B, g graph.DirectedMultigraph) {
	marshalled, err := MarshalMulti(g, "g", "", "")
	if err != nil {
		b.Fatalf("dot: bad Marshal input: %v", err)
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if err := UnmarshalMulti(marshalled, multi.NewDirectedGraph()); err != nil {
			b.Fatalf("dot: bad Unmarshal input: %v", err)
		}
	}
}

func BenchmarkUnmarshalMultiPowerLaw_10_tenth(b *testing.B) {
	benchmarkUnmarshalMulti(b, powerLawMultiDirected_10_tenth)
}
func BenchmarkUnmarshalMultiPowerLaw_100_tenth(b *testing.B) {
	benchmarkUnmarshalMulti(b, powerLawMultiDirected_100_tenth)
}
func BenchmarkUnmarshalMultiPowerLaw_1000_tenth(b *testing.B) {
	benchmarkUnmarshalMulti(b, powerLawMultiDirected_1000_tenth)
}
func BenchmarkUnmarshalMultiPowerLaw_10_half(b *testing.B) {
	benchmarkUnmarshalMulti(b, powerLawMultiDirected_10_half)
}
func BenchmarkUnmarshalMultiPowerLaw_100_half(b *testing.B) {
	benchmarkUnmarshalMulti(b, powerLawMultiDirected_100_half)
}
func BenchmarkUnmarshalMultiPowerLaw_1000_half(b *testing.B) {
	benchmarkUnmarshalMulti(b, powerLawMultiDirected_1000_half)
}