forked from RoaringBitmap/roaring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_memory_test.go
75 lines (65 loc) · 1.63 KB
/
benchmark_memory_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
//go:build go1.13
// +build go1.13
package roaring
import (
"math/rand"
"runtime"
"testing"
"github.com/bits-and-blooms/bitset"
)
// go test -bench BenchmarkMemoryUsage -run -
func BenchmarkMemoryUsage(b *testing.B) {
b.StopTimer()
bitmaps := make([]*Bitmap, 0, 10)
incr := uint32(1 << 16)
max := uint32(1<<32 - 1)
for x := 0; x < 10; x++ {
rb := NewBitmap()
var i uint32
for i = 0; i <= max-incr; i += incr {
rb.Add(i)
}
bitmaps = append(bitmaps, rb)
}
var stats runtime.MemStats
runtime.ReadMemStats(&stats)
b.ReportMetric(float64(stats.HeapInuse), "HeapInUse")
b.ReportMetric(float64(stats.HeapObjects), "HeapObjects")
b.StartTimer()
}
// go test -bench BenchmarkSize -run -
func BenchmarkSizeBitset(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(0))
s1 := bitset.New(0)
sz := 150000
initsize := 65000
for i := 0; i < initsize; i++ {
s1.Set(uint(r.Int31n(int32(sz))))
}
s2 := bitset.New(0)
sz = 100000000
initsize = 65000
for i := 0; i < initsize; i++ {
s2.Set(uint(r.Int31n(int32(sz))))
}
b.ReportMetric(float64(s1.BinaryStorageSize()+s2.BinaryStorageSize())/(1024.0*1024), "BitsetSizeInMB")
}
// go test -bench BenchmarkSize -run -
func BenchmarkSizeRoaring(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(0))
s1 := NewBitmap()
sz := 150000
initsize := 65000
for i := 0; i < initsize; i++ {
s1.Add(uint32(r.Int31n(int32(sz))))
}
s2 := NewBitmap()
sz = 100000000
initsize = 65000
for i := 0; i < initsize; i++ {
s2.Add(uint32(r.Int31n(int32(sz))))
}
b.ReportMetric(float64(s1.GetSerializedSizeInBytes()+s2.GetSerializedSizeInBytes())/(1024.0*1024), "RoaringSizeInMB")
}