Skip to content

Commit 2cb1881

Browse files
committed
test.GUIDFactory to avoid ErrSequenceExpired on benchmarks
1 parent 75c12ba commit 2cb1881

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

internal/test/guids.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package test
2+
3+
import (
4+
"sync/atomic"
5+
)
6+
7+
// GUIDFactory is an atomic sequence that can be used for MessageID's for benchmarks
8+
// to avoid ErrSequenceExpired when creating a large number of messages
9+
type GUIDFactory struct {
10+
n int64
11+
}
12+
13+
func (gf *GUIDFactory) NextMessageID() int64 {
14+
return atomic.AddInt64(&gf.n, 1)
15+
}

nsqd/topic_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,12 @@ func BenchmarkTopicPut(b *testing.B) {
239239
_, _, nsqd := mustStartNSQD(opts)
240240
defer os.RemoveAll(opts.DataPath)
241241
defer nsqd.Exit()
242+
gf := &test.GUIDFactory{}
242243
b.StartTimer()
243244

244245
for i := 0; i <= b.N; i++ {
245246
topic := nsqd.GetOrCreateTopic(topicName)
246-
msg := NewMessage(topic.GenerateID(), []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaa"))
247+
msg := NewMessage(guid(gf.NextMessageID()).Hex(), []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaa"))
247248
topic.PutMessage(msg)
248249
}
249250
}
@@ -260,11 +261,12 @@ func BenchmarkTopicToChannelPut(b *testing.B) {
260261
defer os.RemoveAll(opts.DataPath)
261262
defer nsqd.Exit()
262263
channel := nsqd.GetOrCreateTopic(topicName).GetOrCreateChannel(channelName)
264+
gf := &test.GUIDFactory{}
263265
b.StartTimer()
264266

265267
for i := 0; i <= b.N; i++ {
266268
topic := nsqd.GetOrCreateTopic(topicName)
267-
msg := NewMessage(topic.GenerateID(), []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaa"))
269+
msg := NewMessage(guid(gf.NextMessageID()).Hex(), []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaa"))
268270
topic.PutMessage(msg)
269271
}
270272

@@ -290,6 +292,7 @@ func BenchmarkTopicMessagePump(b *testing.B) {
290292
topic := nsqd.GetOrCreateTopic(topicName)
291293
ch := topic.GetOrCreateChannel("ch")
292294
ctx, cancel := context.WithCancel(context.Background())
295+
gf := &test.GUIDFactory{}
293296

294297
var wg sync.WaitGroup
295298
for i := 0; i < runtime.GOMAXPROCS(0); i++ {
@@ -308,7 +311,7 @@ func BenchmarkTopicMessagePump(b *testing.B) {
308311

309312
b.StartTimer()
310313
for i := 0; i <= b.N; i++ {
311-
msg := NewMessage(topic.GenerateID(), []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaa"))
314+
msg := NewMessage(guid(gf.NextMessageID()).Hex(), []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaa"))
312315
topic.PutMessage(msg)
313316
}
314317
cancel()

0 commit comments

Comments
 (0)