-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmarks_suite.go
131 lines (114 loc) · 3.38 KB
/
benchmarks_suite.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package test
import (
"context"
"fmt"
"sort"
"testing"
core "github.com/dcnetio/gothreads-lib/core/logstore"
"github.com/dcnetio/gothreads-lib/core/thread"
pstore "github.com/libp2p/go-libp2p/core/peerstore"
)
var threadstoreBenchmarks = map[string]func(core.Logstore, chan *logpair) func(*testing.B){
"AddAddrs": benchmarkAddAddrs,
"SetAddrs": benchmarkSetAddrs,
"GetAddrs": benchmarkGetAddrs,
// The in-between get allows us to benchmark the read-through cache.
"AddGetAndClearAddrs": benchmarkAddGetAndClearAddrs,
// Calls LogsWithAddr on a threadstore with 1000 logs.
"Get1000LogsWithAddrs": benchmarkGet1000LogsWithAddrs,
}
func BenchmarkLogstore(b *testing.B, factory LogstoreFactory, variant string) {
// Parameterises benchmarks to tackle logs with 1, 10, 100 multiaddrs.
params := []struct {
n int
ch chan *logpair
}{
{1, make(chan *logpair, 100)},
{10, make(chan *logpair, 100)},
{100, make(chan *logpair, 100)},
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Start all test log producing goroutines, where each produces logs with as many
// multiaddrs as the n field in the param struct.
for _, p := range params {
go AddressProducer(ctx, b, p.ch, p.n)
}
// So tests are always run in the same order.
ordernames := make([]string, 0, len(threadstoreBenchmarks))
for name := range threadstoreBenchmarks {
ordernames = append(ordernames, name)
}
sort.Strings(ordernames)
for _, name := range ordernames {
bench := threadstoreBenchmarks[name]
for _, p := range params {
// Create a new threadstore.
ts, closeFunc := factory()
// Run the test.
b.Run(fmt.Sprintf("%s-%dAddrs-%s", name, p.n, variant), bench(ts, p.ch))
// Cleanup.
if closeFunc != nil {
closeFunc()
}
}
}
}
func benchmarkAddAddrs(ls core.Logstore, addrs chan *logpair) func(*testing.B) {
return func(b *testing.B) {
tid := thread.NewIDV1(thread.Raw, 24)
b.ResetTimer()
for i := 0; i < b.N; i++ {
pp := <-addrs
_ = ls.AddAddrs(tid, pp.ID, pp.Addr, pstore.PermanentAddrTTL)
}
}
}
func benchmarkSetAddrs(ls core.Logstore, addrs chan *logpair) func(*testing.B) {
return func(b *testing.B) {
tid := thread.NewIDV1(thread.Raw, 24)
b.ResetTimer()
for i := 0; i < b.N; i++ {
pp := <-addrs
_ = ls.SetAddrs(tid, pp.ID, pp.Addr, pstore.PermanentAddrTTL)
}
}
}
func benchmarkGetAddrs(ls core.Logstore, addrs chan *logpair) func(*testing.B) {
return func(b *testing.B) {
tid := thread.NewIDV1(thread.Raw, 24)
pp := <-addrs
_ = ls.SetAddrs(tid, pp.ID, pp.Addr, pstore.PermanentAddrTTL)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = ls.Addrs(tid, pp.ID)
}
}
}
func benchmarkAddGetAndClearAddrs(ls core.Logstore, addrs chan *logpair) func(*testing.B) {
return func(b *testing.B) {
tid := thread.NewIDV1(thread.Raw, 24)
b.ResetTimer()
for i := 0; i < b.N; i++ {
pp := <-addrs
_ = ls.AddAddrs(tid, pp.ID, pp.Addr, pstore.PermanentAddrTTL)
_, _ = ls.Addrs(tid, pp.ID)
_ = ls.ClearAddrs(tid, pp.ID)
}
}
}
func benchmarkGet1000LogsWithAddrs(ls core.Logstore, addrs chan *logpair) func(*testing.B) {
return func(b *testing.B) {
tid := thread.NewIDV1(thread.Raw, 24)
var logs = make([]*logpair, 1000)
for i := range logs {
pp := <-addrs
_ = ls.AddAddrs(tid, pp.ID, pp.Addr, pstore.PermanentAddrTTL)
logs[i] = pp
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = ls.LogsWithAddrs(tid)
}
}
}