forked from cloudwego/shmipc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_test.go
179 lines (157 loc) · 4.6 KB
/
queue_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
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
/*
* Copyright 2023 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package shmipc
import (
"fmt"
_ "net/http/pprof"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
queueCap = 1000000
parallelism = 100
)
func TestQueueManager_CreateMapping(t *testing.T) {
path := "/tmp/ipc.queue"
qm1, err := createQueueManager(path, 8192)
assert.Equal(t, nil, err)
qm2, err := mappingQueueManager(path)
assert.Equal(t, nil, err)
assert.Equal(t, nil, qm1.sendQueue.put(queueElement{}))
_, err = qm2.recvQueue.pop()
assert.Equal(t, nil, err)
assert.Equal(t, nil, qm2.sendQueue.put(queueElement{}))
_, err = qm1.recvQueue.pop()
assert.Equal(t, nil, err)
qm1.unmap()
}
func TestQueueOperate(t *testing.T) {
q := createQueue(defaultQueueCap)
fmt.Println("-----------test queue operate ----------------")
assert.Equal(t, true, q.isEmpty(), "queue should be empty")
assert.Equal(t, false, q.isFull(), "queue is not full")
assert.Equal(t, int64(0), q.size(), "queue size should be 0")
putCount, popCount := 0, 0
var err error
for i := 0; i < defaultQueueCap; i++ {
err = q.put(queueElement{seqID: uint32(i), offsetInShmBuf: uint32(i), status: uint32(i)})
assert.Equal(t, nil, err)
putCount++
}
err = q.put(queueElement{1, 1, 1})
assert.Equal(t, ErrQueueFull, err)
assert.Equal(t, true, q.isFull(), "queue should be full")
assert.Equal(t, false, q.isEmpty(), "queue is not empty")
assert.Equal(t, int64(putCount), q.size(), "queue size")
for i := 0; i < defaultQueueCap; i++ {
e, err := q.pop()
assert.Equal(t, nil, err)
popCount++
assert.Equal(t, i, int(e.seqID), "queue pop verify seqID")
assert.Equal(t, i, int(e.offsetInShmBuf), "queue pop verify offset")
assert.Equal(t, i, int(e.status), "queue pop verify offset")
}
_, err = q.pop()
assert.Equal(t, errQueueEmpty, err)
assert.Equal(t, false, q.isFull(), "queue is not full")
assert.Equal(t, true, q.isEmpty(), "queue should be empty")
assert.Equal(t, int64(0), q.size(), "queue size")
fmt.Println("-----------test queue status ----------------")
assert.Equal(t, false, q.consumerIsWorking(), "consumer should be not working")
q.markWorking()
assert.Equal(t, true, q.consumerIsWorking(), "consumer should be working")
q.markNotWorking()
assert.Equal(t, false, q.consumerIsWorking(), "consumer should be not working")
_ = q.put(queueElement{1, 1, 1})
q.markNotWorking()
assert.Equal(t, true, q.consumerIsWorking(), "consumer should be working")
}
func TestQueueMultiProducerAndSingleConsumer(t *testing.T) {
fmt.Println("-----------test queue multi-producer single consumer ----------------")
q := createQueue(uint32(queueCap))
var wg sync.WaitGroup
popCount := 0
for i := 0; i < parallelism; i++ {
//producer
go func() {
for k := 0; k < queueCap/parallelism; k++ {
wg.Add(1)
if err := q.put(queueElement{seqID: 1, offsetInShmBuf: 1, status: 1}); err != nil {
panic(err)
}
}
}()
}
//consumer
for popCount != queueCap {
_, err := q.pop()
if err == nil {
wg.Done()
popCount++
} else {
time.Sleep(time.Microsecond)
}
}
wg.Wait()
}
func BenchmarkQueuePut(b *testing.B) {
q := createQueue(uint32(b.N))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = q.put(queueElement{seqID: uint32(i), offsetInShmBuf: uint32(i)})
}
}
func BenchmarkQueuePop(b *testing.B) {
q := createQueue(uint32(b.N))
for i := 0; i < b.N; i++ {
_ = q.put(queueElement{seqID: uint32(i), offsetInShmBuf: uint32(i)})
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
q.pop()
}
}
func BenchmarkQueueMultiPut(b *testing.B) {
b.SetParallelism(50)
q := createQueue(uint32(b.N))
b.ResetTimer()
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
c := 0
for pb.Next() {
c++
_ = q.put(queueElement{seqID: uint32(c), offsetInShmBuf: uint32(c)})
}
})
}
func BenchmarkQueueMultiPop(b *testing.B) {
b.SetParallelism(50)
q := createQueue(uint32(b.N))
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = q.put(queueElement{seqID: uint32(i), offsetInShmBuf: uint32(i)})
}
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
q.pop()
}
})
}