-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
275 lines (243 loc) · 8.46 KB
/
Copy pathbenchmark_test.go
File metadata and controls
275 lines (243 loc) · 8.46 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2018-2026 Burak Sezer
// All rights reserved.
//
// This code is licensed under the MIT License.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package consistent
import (
"fmt"
"testing"
)
// On the read path, building a key costs more than the call we want to measure.
// So the benchmarks read their keys from a pool. The pool is filled before the
// timer starts. Its size is a power of two, so the loop can mask the counter
// instead of dividing it.
const (
benchKeyPoolSize = 4096
benchKeyPoolMask = benchKeyPoolSize - 1
)
// Member counts for the calls that walk or copy the member map.
var benchMemberCounts = []int{8, 64, 512}
// LocateKey is O(1) in the member count. These two sizes are far apart, so both
// must give the same result. If they differ, the lookup walks the ring.
var benchLocateKeyMemberCounts = []int{8, 512}
// The cost of distributePartitions grows with PartitionCount. So the write path
// benchmarks vary it together with the member count.
var (
benchWritePartitionCounts = []int{23, 271}
benchWriteMemberCounts = []int{8, 64}
)
// The compiler may drop a call if nobody uses its result. These sinks prevent
// that.
var (
benchMemberSink Member
benchMembersSink []Member
benchFloatSink float64
benchLoadsSink map[string]float64
benchIntSink int
benchErrSink error
)
func benchConfig(partitionCount int) Config {
cfg := newConfig()
cfg.PartitionCount = partitionCount
return cfg
}
func benchMembers(count int) []Member {
members := make([]Member, count)
for i := 0; i < count; i++ {
members[i] = testMember(fmt.Sprintf("node%d.olric", i))
}
return members
}
func benchKeys() [][]byte {
keys := make([][]byte, benchKeyPoolSize)
for i := range keys {
keys[i] = []byte(fmt.Sprintf("key%d", i))
}
return keys
}
// benchRing builds a ring with count members and the default test config.
func benchRing(count int) *Consistent {
return New(benchMembers(count), newConfig())
}
// runWritePath runs fn for every partition count and member count pair.
func runWritePath(b *testing.B, fn func(b *testing.B, cfg Config, memberCount int)) {
b.Helper()
for _, partitionCount := range benchWritePartitionCounts {
for _, memberCount := range benchWriteMemberCounts {
name := fmt.Sprintf("partitions=%d/members=%d", partitionCount, memberCount)
b.Run(name, func(b *testing.B) {
fn(b, benchConfig(partitionCount), memberCount)
})
}
}
}
// runMemberCounts runs fn for every member count. The ring is ready before fn
// starts.
func runMemberCounts(b *testing.B, counts []int, fn func(b *testing.B, c *Consistent)) {
b.Helper()
for _, memberCount := range counts {
b.Run(fmt.Sprintf("members=%d", memberCount), func(b *testing.B) {
fn(b, benchRing(memberCount))
})
}
}
// BenchmarkNew measures the setup cost. New calls add once per member, then
// distributePartitions once. Each iteration starts from an empty ring, so the
// cost per iteration stays the same.
func BenchmarkNew(b *testing.B) {
runWritePath(b, func(b *testing.B, cfg Config, memberCount int) {
members := benchMembers(memberCount)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchIntSink = len(New(members, cfg).partitions)
}
})
}
// BenchmarkAddRemove adds and removes one member on a ring that already holds
// memberCount members. The size stays between memberCount and memberCount+1.
// Remove never sees an empty ring, so both calls run distributePartitions.
//
// There is no separate benchmark for Add or Remove. Both change the ring. In a
// loop with only one of them, the ring would grow or shrink on every iteration,
// and ns/op would be useless. BenchmarkNew shows the cost of an insert.
func BenchmarkAddRemove(b *testing.B) {
runWritePath(b, func(b *testing.B, cfg Config, memberCount int) {
c := New(benchMembers(memberCount), cfg)
// The names come from a small pool. This gives different ring
// positions without an allocation per iteration.
const namePoolSize = 64
names := make([]testMember, namePoolSize)
for i := range names {
names[i] = testMember(fmt.Sprintf("churn%d.olric", i))
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
member := names[i%namePoolSize]
c.Add(member)
c.Remove(member.String())
}
})
}
// BenchmarkFindPartitionID measures one hash call. It takes no lock. Use it as
// the baseline for the benchmarks below.
func BenchmarkFindPartitionID(b *testing.B) {
c := benchRing(8)
keys := benchKeys()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchIntSink = c.FindPartitionID(keys[i&benchKeyPoolMask])
}
}
// BenchmarkLocateKey measures one hash plus one map read under RLock.
func BenchmarkLocateKey(b *testing.B) {
runMemberCounts(b, benchLocateKeyMemberCounts, func(b *testing.B, c *Consistent) {
keys := benchKeys()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchMemberSink = c.LocateKey(keys[i&benchKeyPoolMask])
}
})
}
// BenchmarkGetPartitionOwner measures the same lookup without the hash.
func BenchmarkGetPartitionOwner(b *testing.B) {
cfg := newConfig()
c := New(benchMembers(8), cfg)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchMemberSink = c.GetPartitionOwner(i % cfg.PartitionCount)
}
}
// BenchmarkAverageLoad measures a few arithmetic operations under RLock.
func BenchmarkAverageLoad(b *testing.B) {
c := benchRing(8)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchFloatSink = c.AverageLoad()
}
}
// BenchmarkGetClosestN varies the member count. getClosestN hashes and sorts
// all members on every call, so the cost grows with that number.
func BenchmarkGetClosestN(b *testing.B) {
runMemberCounts(b, benchMemberCounts, func(b *testing.B, c *Consistent) {
keys := benchKeys()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchMembersSink, benchErrSink = c.GetClosestN(keys[i&benchKeyPoolMask], 3)
}
})
}
// BenchmarkGetMembers measures the slice copy. Every call makes a new one.
func BenchmarkGetMembers(b *testing.B) {
runMemberCounts(b, benchMemberCounts, func(b *testing.B, c *Consistent) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchMembersSink = c.GetMembers()
}
})
}
// BenchmarkLoadDistribution measures the map copy. Every call makes a new one.
//
// The loads map has one entry per member that owns a partition. So its size
// cannot pass PartitionCount. The default test config has 23 partitions. With
// that config, the numbers for 64 and 512 members would be equal. This
// benchmark uses more partitions than members instead. Now the member count
// sets the size of the copy.
func BenchmarkLoadDistribution(b *testing.B) {
cfg := benchConfig(1031)
for _, memberCount := range benchMemberCounts {
b.Run(fmt.Sprintf("members=%d", memberCount), func(b *testing.B) {
c := New(benchMembers(memberCount), cfg)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchLoadsSink = c.LoadDistribution()
}
})
}
}
// BenchmarkLocateKeyParallel shows how the read path behaves under load. All
// readers share one RWMutex. Each goroutine keeps its own counter for the key
// pool, so the result shows the cost of the lock and nothing else.
//
// Run it with -cpu=1,2,4,8 to see the trend.
func BenchmarkLocateKeyParallel(b *testing.B) {
c := benchRing(8)
keys := benchKeys()
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
i := 0
var member Member
for pb.Next() {
member = c.LocateKey(keys[i&benchKeyPoolMask])
i++
}
benchMemberSink = member
})
}