-
Notifications
You must be signed in to change notification settings - Fork 1
/
array_buffer_benchmark_test.go
97 lines (81 loc) · 1.79 KB
/
array_buffer_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
package flyline
import (
"context"
"runtime"
"testing"
)
func BenchmarkArrayBufferOneGoroutine(b *testing.B) {
runtime.GOMAXPROCS(1)
defer runtime.GOMAXPROCS(1)
benchmarkArrayBuffer(b, 1)
}
func BenchmarkArrayBufferTwoGoroutines(b *testing.B) {
runtime.GOMAXPROCS(2)
defer runtime.GOMAXPROCS(1)
benchmarkArrayBuffer(b, 1)
}
func BenchmarkArrayBufferThreeGoroutinesWithContendedWrite(b *testing.B) {
runtime.GOMAXPROCS(3)
defer runtime.GOMAXPROCS(1)
benchmarkArrayBuffer(b, 2)
}
func benchmarkArrayBuffer(b *testing.B, writers int64) {
iterations := int64(b.N)
maxReads := iterations * writers
buf := NewArrayBuffer(1024 * 16)
b.ReportAllocs()
b.ResetTimer()
for x := int64(0); x < writers; x++ {
go func() {
for i := int64(0); i < iterations; {
buf.Send(i)
i++
}
}()
}
buf.Close()
for i := int64(0); i < maxReads; i++ {
buf.Recv()
}
b.StopTimer()
buf.Sync(context.Background())
}
func BenchmarkNonBlockingOneGoroutine(b *testing.B) {
runtime.GOMAXPROCS(1)
defer runtime.GOMAXPROCS(1)
benchmarkNonBlocking(b, 1)
}
func BenchmarkNonBlockingTwoGoroutines(b *testing.B) {
runtime.GOMAXPROCS(2)
defer runtime.GOMAXPROCS(1)
benchmarkNonBlocking(b, 1)
}
func BenchmarkNonBlockingThreeGoroutinesWithContendedWrite(b *testing.B) {
runtime.GOMAXPROCS(3)
defer runtime.GOMAXPROCS(1)
benchmarkNonBlocking(b, 2)
}
func benchmarkNonBlocking(b *testing.B, writers int64) {
iterations := int64(b.N)
maxReads := iterations * writers
channel := make(chan int64, 1024*16)
b.ReportAllocs()
b.ResetTimer()
for x := int64(0); x < writers; x++ {
go func() {
for i := int64(0); i < iterations; {
select {
case channel <- i:
i++
default:
continue
}
}
}()
}
for i := int64(0); i < maxReads; i++ {
<-channel
}
b.StopTimer()
close(channel)
}