forked from JustinTimperio/gpq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpq_test.go
230 lines (206 loc) · 5.02 KB
/
gpq_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
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
package gpq_test
import (
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/JustinTimperio/gpq"
"github.com/JustinTimperio/gpq/schema"
"github.com/dgraph-io/badger/v4"
)
func TestGPQ(t *testing.T) {
var (
total int = 10000000
print bool = false
sent uint64
timedOut uint64
received uint64
missed int64
hits int64
emptyMessage int64
defaultMessageOptions = schema.EnQueueOptions{
ShouldEscalate: false,
EscalationRate: time.Duration(time.Second),
CanTimeout: false,
Timeout: time.Duration(time.Second * 10),
}
)
opts := schema.GPQOptions{
NumberOfBuckets: 10,
DiskCacheEnabled: false,
DiskCachePath: "/tmp/gpq/test",
DiskCacheCompression: false,
DiskEncryptionEnabled: false,
DiskEncryptionKey: []byte("12345678901234567890123456789012"),
LazyDiskCacheEnabled: true,
LazyDiskBatchSize: 50000,
}
// Create a pprof file
f, err := os.Create("profile.pprof")
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Start CPU profiling
err = pprof.StartCPUProfile(f)
if err != nil {
log.Fatal(err)
}
defer pprof.StopCPUProfile()
// Create pprof mutex file
fm, err := os.Create("profile.mutex")
if err != nil {
log.Fatal(err)
}
defer fm.Close()
// Start mutex profiling
runtime.SetMutexProfileFraction(1)
defer func() {
p := pprof.Lookup("mutex")
if p == nil {
log.Fatal("could not capture mutex profile")
}
// Create pprof mutex file
fm, err := os.Create("profile.mutex")
if err != nil {
log.Fatal(err)
}
defer fm.Close()
if err := p.WriteTo(fm, 0); err != nil {
log.Fatal("could not write mutex profile: ", err)
}
}()
requeued, queue, err := gpq.NewGPQ[int](opts)
if err != nil {
log.Fatalln(err)
}
wg := &sync.WaitGroup{}
go func() {
for atomic.LoadUint64(&queue.NonEmptyBuckets.ObjectsInQueue) > 0 || atomic.LoadUint64(&received) < uint64(total) {
time.Sleep(1 * time.Second)
to, es, err := queue.Prioritize()
if err != nil {
}
atomic.AddUint64(&timedOut, to)
log.Println("Hits:", hits, "Misses:", missed, "Sent:", sent, "Received:", missed+hits, "Timed Out:", timedOut, "Empty Messages:", emptyMessage, "Escalated:", es, "Prioritized:", to)
}
}()
timer := time.Now()
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
for i := 0; i < total/10; i++ {
p := i % 10
timer := time.Now()
err := queue.EnQueue(
i,
int64(p),
defaultMessageOptions,
)
if err != nil {
log.Fatalln(err)
}
if print {
log.Println("EnQueue", p, time.Since(timer))
}
atomic.AddUint64(&sent, 1)
}
}()
}
wg.Add(2)
for i := 0; i < 2; i++ {
go func() {
defer wg.Done()
var lastPriority int64
for retries := int64(0); retries < 10; retries++ {
for atomic.LoadUint64(&queue.NonEmptyBuckets.ObjectsInQueue) > 0 || atomic.LoadUint64(&received)+atomic.LoadUint64(&timedOut) < uint64(total) {
timer := time.Now()
priority, item, err := queue.DeQueue()
if err != nil {
if err.Error() == "No items in any queue" || err.Error() == "Core Priority Queue Error: No items found in the queue" {
atomic.AddInt64(&emptyMessage, 1)
time.Sleep(10 * time.Millisecond)
lastPriority = 0
continue
}
log.Fatalln(err)
}
atomic.AddUint64(&received, 1)
if print {
log.Println("DeQueue", priority, received, item, time.Since(timer))
}
if lastPriority > priority {
missed++
} else {
hits++
}
lastPriority = priority
atomic.StoreInt64(&retries, 0)
}
time.Sleep(10 * time.Millisecond)
}
}()
}
wg.Wait()
fmt.Println(
" Total:", total, "\n",
"Sent:", atomic.LoadUint64(&sent), "\n",
"Received:", atomic.LoadUint64(&received), "\n",
"Requeued:", requeued, "\n",
"Timed Out:", atomic.LoadUint64(&timedOut), "\n",
"Finished in:", time.Since(timer), "\n",
"Out Of Order:", missed, "\n",
"In Order:", hits,
)
// Wait for all db sessions to sync to disk
queue.Close()
}
func TestNumberOfItems(t *testing.T) {
var total int
opts := badger.DefaultOptions("/tmp/gpq/test")
opts.Logger = nil
db, err := badger.Open(opts)
if err != nil {
t.Fatal(err)
}
err = db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
val := it.Item()
val.Value(func(v []byte) error {
/*
// Decode the item
var buf bytes.Buffer
buf.Write(v)
obj := schema.Item[int]{}
err = gob.NewDecoder(&buf).Decode(&obj)
if err != nil {
t.Fatal(err)
}
jsonObj, err := json.Marshal(obj)
if err != nil {
t.Fatal(err)
}
fmt.Println(string(jsonObj))
*/
return nil
})
total++
}
return nil
})
if err != nil {
t.Fatal(err)
}
t.Log("Total items in badgerDB", total)
return
}