From 15c66abbe0cb05f187b71b607c38d0075a162e86 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Wed, 15 May 2024 16:40:32 +0200 Subject: [PATCH] wip --- lib/logstorage/bitmap_timing_test.go | 82 ++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 lib/logstorage/bitmap_timing_test.go diff --git a/lib/logstorage/bitmap_timing_test.go b/lib/logstorage/bitmap_timing_test.go new file mode 100644 index 000000000..6bbcc8c8a --- /dev/null +++ b/lib/logstorage/bitmap_timing_test.go @@ -0,0 +1,82 @@ +package logstorage + +import ( + "testing" +) + +func BenchmarkBitmapForEachSetBit(b *testing.B) { + const bitsLen = 64*1024 + + 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) + }) +} + +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) + for pb.Next() { + bmLocal.copyFrom(bm) + bmLocal.forEachSetBit(func(idx int) bool { + 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) + }) +}