2020-02-23 12:35:47 +01:00
|
|
|
package persistentqueue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-04-05 18:19:58 +02:00
|
|
|
"sync"
|
2020-02-23 12:35:47 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkQueueThroughputSerial(b *testing.B) {
|
2021-04-05 18:19:58 +02:00
|
|
|
const iterationsCount = 100
|
2020-02-23 12:35:47 +01:00
|
|
|
for _, blockSize := range []int{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6} {
|
|
|
|
block := make([]byte, blockSize)
|
|
|
|
b.Run(fmt.Sprintf("block-size-%d", blockSize), func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(int64(blockSize) * iterationsCount)
|
|
|
|
path := fmt.Sprintf("bench-queue-throughput-serial-%d", blockSize)
|
|
|
|
mustDeleteDir(path)
|
2021-04-05 18:19:58 +02:00
|
|
|
q := mustOpen(path, "foobar", 0)
|
2020-02-23 12:35:47 +01:00
|
|
|
defer func() {
|
|
|
|
q.MustClose()
|
|
|
|
mustDeleteDir(path)
|
|
|
|
}()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
writeReadIteration(q, block, iterationsCount)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkQueueThroughputConcurrent(b *testing.B) {
|
2021-04-05 18:19:58 +02:00
|
|
|
const iterationsCount = 100
|
2020-02-23 12:35:47 +01:00
|
|
|
for _, blockSize := range []int{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6} {
|
|
|
|
block := make([]byte, blockSize)
|
|
|
|
b.Run(fmt.Sprintf("block-size-%d", blockSize), func(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(int64(blockSize) * iterationsCount)
|
|
|
|
path := fmt.Sprintf("bench-queue-throughput-concurrent-%d", blockSize)
|
|
|
|
mustDeleteDir(path)
|
2021-04-05 18:19:58 +02:00
|
|
|
q := mustOpen(path, "foobar", 0)
|
|
|
|
var qLock sync.Mutex
|
2020-02-23 12:35:47 +01:00
|
|
|
defer func() {
|
|
|
|
q.MustClose()
|
|
|
|
mustDeleteDir(path)
|
|
|
|
}()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
2021-04-05 18:19:58 +02:00
|
|
|
qLock.Lock()
|
2020-02-23 12:35:47 +01:00
|
|
|
writeReadIteration(q, block, iterationsCount)
|
2021-04-05 18:19:58 +02:00
|
|
|
qLock.Unlock()
|
2020-02-23 12:35:47 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 18:19:58 +02:00
|
|
|
func writeReadIteration(q *queue, block []byte, iterationsCount int) {
|
2020-02-23 12:35:47 +01:00
|
|
|
for i := 0; i < iterationsCount; i++ {
|
|
|
|
q.MustWriteBlock(block)
|
|
|
|
}
|
|
|
|
var ok bool
|
|
|
|
bb := bbPool.Get()
|
|
|
|
for i := 0; i < iterationsCount; i++ {
|
2021-04-05 18:19:58 +02:00
|
|
|
bb.B, ok = q.MustReadBlockNonblocking(bb.B[:0])
|
2020-02-23 12:35:47 +01:00
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("unexpected ok=false"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bbPool.Put(bb)
|
|
|
|
}
|
|
|
|
|
|
|
|
var bbPool bytesutil.ByteBufferPool
|