VictoriaMetrics/lib/logstorage/bitmap_timing_test.go

132 lines
2.9 KiB
Go
Raw Normal View History

2024-05-15 14:40:32 +00:00
package logstorage
import (
"testing"
)
2024-05-15 14:50:26 +00:00
func BenchmarkBitmapForEachSetBitReadonly(b *testing.B) {
2024-05-15 20:19:21 +00:00
const bitsLen = 64 * 1024
2024-05-15 14:50:26 +00:00
b.Run("no-zero-bits", func(b *testing.B) {
bm := getBitmap(bitsLen)
bm.setBits()
benchmarkBitmapForEachSetBitReadonly(b, bm)
putBitmap(bm)
})
b.Run("half-zero-bits", func(b *testing.B) {
bm := getBitmap(bitsLen)
bm.setBits()
bm.forEachSetBit(func(idx int) bool {
return idx%2 == 0
})
benchmarkBitmapForEachSetBitReadonly(b, bm)
putBitmap(bm)
})
b.Run("one-set-bit", func(b *testing.B) {
bm := getBitmap(bitsLen)
bm.setBits()
bm.forEachSetBit(func(idx int) bool {
return idx == bitsLen/2
})
benchmarkBitmapForEachSetBitReadonly(b, bm)
putBitmap(bm)
})
}
2024-05-15 14:40:32 +00:00
func BenchmarkBitmapForEachSetBit(b *testing.B) {
2024-05-15 20:19:21 +00:00
const bitsLen = 64 * 1024
2024-05-15 14:40:32 +00:00
b.Run("no-zero-bits-noclear", func(b *testing.B) {
bm := getBitmap(bitsLen)
bm.setBits()
benchmarkBitmapForEachSetBit(b, bm, false)
putBitmap(bm)
})
b.Run("no-zero-bits-clear", func(b *testing.B) {
bm := getBitmap(bitsLen)
bm.setBits()
benchmarkBitmapForEachSetBit(b, bm, true)
putBitmap(bm)
})
b.Run("half-zero-bits-noclear", func(b *testing.B) {
bm := getBitmap(bitsLen)
bm.setBits()
bm.forEachSetBit(func(idx int) bool {
return idx%2 == 0
})
benchmarkBitmapForEachSetBit(b, bm, false)
putBitmap(bm)
})
b.Run("half-zero-bits-clear", func(b *testing.B) {
bm := getBitmap(bitsLen)
bm.setBits()
bm.forEachSetBit(func(idx int) bool {
return idx%2 == 0
})
benchmarkBitmapForEachSetBit(b, bm, true)
putBitmap(bm)
})
b.Run("one-set-bit-noclear", func(b *testing.B) {
bm := getBitmap(bitsLen)
bm.setBits()
bm.forEachSetBit(func(idx int) bool {
return idx == bitsLen/2
})
benchmarkBitmapForEachSetBit(b, bm, false)
putBitmap(bm)
})
b.Run("one-set-bit-clear", func(b *testing.B) {
bm := getBitmap(bitsLen)
bm.setBits()
bm.forEachSetBit(func(idx int) bool {
return idx == bitsLen/2
})
benchmarkBitmapForEachSetBit(b, bm, true)
putBitmap(bm)
})
}
2024-05-15 14:50:26 +00:00
func benchmarkBitmapForEachSetBitReadonly(b *testing.B, bm *bitmap) {
b.SetBytes(int64(bm.bitsLen))
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
bmLocal := getBitmap(bm.bitsLen)
n := 0
for pb.Next() {
bmLocal.copyFrom(bm)
bmLocal.forEachSetBitReadonly(func(idx int) {
n++
})
}
putBitmap(bmLocal)
GlobalSink.Add(uint64(n))
})
}
2024-05-15 14:40:32 +00:00
func benchmarkBitmapForEachSetBit(b *testing.B, bm *bitmap, isClearBits bool) {
b.SetBytes(int64(bm.bitsLen))
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
bmLocal := getBitmap(bm.bitsLen)
2024-05-15 14:50:26 +00:00
n := 0
2024-05-15 14:40:32 +00:00
for pb.Next() {
bmLocal.copyFrom(bm)
bmLocal.forEachSetBit(func(idx int) bool {
2024-05-15 14:50:26 +00:00
n++
2024-05-15 14:40:32 +00:00
return !isClearBits
})
if isClearBits {
if !bmLocal.isZero() {
panic("BUG: bitmap must have no set bits")
}
} else {
if bmLocal.isZero() {
panic("BUG: bitmap must have some set bits")
}
}
}
putBitmap(bmLocal)
2024-05-15 14:50:26 +00:00
GlobalSink.Add(uint64(n))
2024-05-15 14:40:32 +00:00
})
}