-
Notifications
You must be signed in to change notification settings - Fork 0
/
suffixtree_benchmark_test.go
103 lines (77 loc) · 2.33 KB
/
suffixtree_benchmark_test.go
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
package gostr_test
import (
"math/rand"
"testing"
"time"
"github.com/mailund/gostr"
"github.com/mailund/gostr/test"
)
func benchmarkConstruction(
b *testing.B,
constructor func(string) *gostr.SuffixTree,
n int) {
b.Helper()
seed := time.Now().UTC().UnixNano()
rng := rand.New(rand.NewSource(seed))
x := test.RandomStringN(n, "abcdefg", rng)
for i := 0; i < b.N; i++ {
constructor(x)
}
}
func BenchmarkNaive10000(b *testing.B) { benchmarkConstruction(b, gostr.NaiveST, 10000) }
func BenchmarkNaive100000(b *testing.B) { benchmarkConstruction(b, gostr.NaiveST, 100000) }
func BenchmarkNaive1000000(b *testing.B) { benchmarkConstruction(b, gostr.NaiveST, 1000000) }
func BenchmarkMcCreight10000(b *testing.B) { benchmarkConstruction(b, gostr.McCreight, 10000) }
func BenchmarkMcCreight100000(b *testing.B) { benchmarkConstruction(b, gostr.McCreight, 100000) }
func BenchmarkMcCreight1000000(b *testing.B) { benchmarkConstruction(b, gostr.McCreight, 1000000) }
func publicTraversal(n gostr.STNode) int {
switch n.NodeType {
case gostr.Leaf:
return n.Leaf().Index
case gostr.Inner:
val := 0
for _, child := range n.Inner().Children {
if !child.IsNil() {
val += publicTraversal(child)
}
}
return val
}
return 0 // Unreachable, but we need to return...
}
func visitorTraversal(n gostr.STNode) int {
res := 0
n.LeafIndices(func(idx int) { res += idx })
return res
}
func Test_Traversal(t *testing.T) {
seed := time.Now().UTC().UnixNano()
rng := rand.New(rand.NewSource(seed))
x := test.RandomStringRange(500, 1000, "abcdefg", rng)
st := gostr.NaiveST(x)
public := publicTraversal(st.Root)
visitor := 0
st.Root.LeafIndices(func(i int) { visitor += i })
if public != visitor {
t.Errorf("The public/visitor traversal gave different resuls: %d/%d",
public, visitor)
}
}
func benchmarkTraversal(b *testing.B, traversal func(gostr.STNode) int) {
b.Helper()
seed := time.Now().UTC().UnixNano()
rng := rand.New(rand.NewSource(seed))
x := test.RandomStringN(1000, "abcdefg", rng)
st := gostr.NaiveST(x)
traversal(st.Root) // first traversal sorts the children...
b.ResetTimer()
for i := 0; i < b.N; i++ {
traversal(st.Root)
}
}
func BenchmarkPublicTraversal(b *testing.B) {
benchmarkTraversal(b, publicTraversal)
}
func BenchmarkVisitorTraversal(b *testing.B) {
benchmarkTraversal(b, visitorTraversal)
}