-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_benchmark_test.go
More file actions
163 lines (139 loc) · 3.5 KB
/
queue_benchmark_test.go
File metadata and controls
163 lines (139 loc) · 3.5 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
package ring
import (
"fmt"
"runtime"
"sync"
"sync/atomic"
"testing"
)
// BenchmarkQueue_CompareGoImplementations сравнивает нашу реализацию с другими Go вариантами
func BenchmarkQueue_CompareGoImplementations(b *testing.B) {
b.Run("RingQueue Capacity: 256 Reader: 1", func(b *testing.B) {
benchmarkOurImplementation(b, 256, 1, 1)
})
b.Run("RingQueue Capacity: 256 Reader: 2", func(b *testing.B) {
benchmarkOurImplementation(b, 256, 1, 2)
})
b.Run("RingQueue Capacity: 256 Reader: 4", func(b *testing.B) {
benchmarkOurImplementation(b, 256, 1, 4)
})
b.Run("GoChannels Capacity: 256 Reader: 1", func(b *testing.B) {
benchmarkGoChannels(b, 256, 1, 1)
})
b.Run("GoChannels Capacity: 256 Reader: 2", func(b *testing.B) {
benchmarkGoChannels(b, 256, 1, 2)
})
b.Run("GoChannels Capacity: 256 Reader: 4", func(b *testing.B) {
benchmarkGoChannels(b, 256, 1, 4)
})
b.Run("RingQueue Capacity: 1024 Reader: 1", func(b *testing.B) {
benchmarkOurImplementation(b, 1024, 1, 1)
})
b.Run("RingQueue Capacity: 1024 Reader: 2", func(b *testing.B) {
benchmarkOurImplementation(b, 1024, 1, 2)
})
b.Run("RingQueue Capacity: 1024 Reader: 4", func(b *testing.B) {
benchmarkOurImplementation(b, 1024, 1, 4)
})
b.Run("GoChannels Capacity: 1024 Reader: 1", func(b *testing.B) {
benchmarkGoChannels(b, 1024, 1, 1)
})
b.Run("GoChannels Capacity: 1024 Reader: 2", func(b *testing.B) {
benchmarkGoChannels(b, 1024, 1, 2)
})
b.Run("GoChannels Capacity: 1024 Reader: 4", func(b *testing.B) {
benchmarkGoChannels(b, 1024, 1, 4)
})
}
// Наша реализация
func benchmarkOurImplementation(b *testing.B, capacity int, numProducers, numConsumers int) {
q, err := Queue[int](uint64(capacity))
if err != nil {
b.Fatalf("Failed to create queue: %v", err)
}
var consumed int64
var wg sync.WaitGroup
// Consumers
for i := 0; i < numConsumers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
if _, success := q.Dequeue(); success {
if atomic.AddInt64(&consumed, 1) >= int64(b.N) {
return
}
}
if atomic.LoadInt64(&consumed) >= int64(b.N) {
return
}
}
}()
}
b.ReportAllocs()
b.ResetTimer()
// Producers
for i := 0; i < numProducers; i++ {
wg.Add(1)
go func(producerID int) {
defer wg.Done()
itemsPerProducer := b.N / numProducers
for j := 0; j < itemsPerProducer; j++ {
val := producerID*itemsPerProducer + j
if err = q.MustEnqueue(val); err != nil {
fmt.Printf("Producer %d failed to enqueue item %d: %v\n", producerID, val, err)
}
}
}(i)
}
wg.Wait()
}
// Go channels (baseline)
func benchmarkGoChannels(b *testing.B, capacity int, numProducers, numConsumers int) {
ch := make(chan int, capacity)
var consumed int64
var wg sync.WaitGroup
// Consumers
for i := 0; i < numConsumers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case val := <-ch:
_ = val
if atomic.AddInt64(&consumed, 1) >= int64(b.N) {
return
}
default:
if atomic.LoadInt64(&consumed) >= int64(b.N) {
return
}
}
}
}()
}
b.ReportAllocs()
b.ResetTimer()
// Producers
for i := 0; i < numProducers; i++ {
wg.Add(1)
go func(producerID int) {
defer wg.Done()
itemsPerProducer := b.N / numProducers
for j := 0; j < itemsPerProducer; j++ {
val := producerID*itemsPerProducer + j
for {
select {
case ch <- val:
goto next
default:
runtime.Gosched()
}
}
next:
}
}(i)
}
wg.Wait()
}