-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboxing_test.go
More file actions
58 lines (47 loc) · 991 Bytes
/
boxing_test.go
File metadata and controls
58 lines (47 loc) · 991 Bytes
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
package ring
import (
"testing"
)
// Pointer type to avoid int->any boxing
type testItem struct{ val int }
func BenchmarkWriterNoBoxing(b *testing.B) {
r, _ := NewShardedRing(1000000, 8)
config := WriteConfig{
Strategy: NextShard,
MaxRetries: 10,
}
writer := NewWriter(r, 0, config)
// Pre-allocate items to reuse
items := make([]*testItem, 1000)
for i := range items {
items[i] = &testItem{val: i}
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
writer.Write(items[i%1000]) // Pointer - no boxing
if i%100 == 99 {
for j := 0; j < 100; j++ {
r.TryRead()
}
}
}
}
func BenchmarkWriterWithBoxing(b *testing.B) {
r, _ := NewShardedRing(1000000, 8)
config := WriteConfig{
Strategy: NextShard,
MaxRetries: 10,
}
writer := NewWriter(r, 0, config)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
writer.Write(i) // int -> any boxing
if i%100 == 99 {
for j := 0; j < 100; j++ {
r.TryRead()
}
}
}
}