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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
package roaring
import (
"archive/zip"
"bytes"
"fmt"
"io"
"os"
"path"
"strconv"
"strings"
"testing"
)
// To run these benchmarks, type BENCH_REAL_DATA=1 go test -bench BenchmarkRealData -run -
var benchRealData = false
var realDatasets = []string{
"census-income_srt", "census-income", "census1881_srt", "census1881",
"dimension_003", "dimension_008", "dimension_033", "uscensus2000", "weather_sept_85_srt", "weather_sept_85",
"wikileaks-noquotes_srt", "wikileaks-noquotes",
}
func init() {
if envStr, ok := os.LookupEnv("BENCH_REAL_DATA"); ok {
v, err := strconv.ParseBool(envStr)
if err != nil {
v = false
}
benchRealData = v
}
}
func retrieveRealDataBitmaps(datasetName string, optimize bool) ([]*Bitmap, error) {
gopath, ok := os.LookupEnv("GOPATH")
if !ok {
return nil, fmt.Errorf("GOPATH not set. It's required to locate real-roaring-datasets. Set GOPATH or disable BENCH_REAL_DATA")
}
basePath := path.Join(gopath, "src", "github.com", "RoaringBitmap", "real-roaring-datasets")
if _, err := os.Stat(basePath); os.IsNotExist(err) {
return nil, fmt.Errorf("real-roaring-datasets does not exist. Run `go get github.com/RoaringBitmap/real-roaring-datasets`")
}
datasetPath := path.Join(basePath, datasetName+".zip")
if _, err := os.Stat(datasetPath); os.IsNotExist(err) {
return nil, fmt.Errorf("dataset %s does not exist, tried path: %s", datasetName, datasetPath)
}
zipFile, err := zip.OpenReader(datasetPath)
if err != nil {
return nil, fmt.Errorf("error opening dataset %s zipfile, cause: %v", datasetPath, err)
}
defer zipFile.Close()
var largestFileSize uint64
for _, f := range zipFile.File {
if f.UncompressedSize64 > largestFileSize {
largestFileSize = f.UncompressedSize64
}
}
bitmaps := make([]*Bitmap, len(zipFile.File))
buf := make([]byte, largestFileSize)
var bufStep uint64 = 32768 // apparently the largest buffer zip can read
for i, f := range zipFile.File {
r, err := f.Open()
if err != nil {
return nil, fmt.Errorf("failed to read bitmap file %s from dataset %s, cause: %v", f.Name, datasetName, err)
}
var totalReadBytes uint64
for {
var endOffset uint64
if f.UncompressedSize64 < totalReadBytes+bufStep {
endOffset = f.UncompressedSize64
} else {
endOffset = totalReadBytes + bufStep
}
readBytes, err := r.Read(buf[totalReadBytes:endOffset])
totalReadBytes += uint64(readBytes)
if err == io.EOF {
r.Close()
break
} else if err != nil {
r.Close()
return nil, fmt.Errorf("could not read content of file %s from dataset %s, cause: %v", f.Name, datasetName, err)
}
}
elemsAsBytes := bytes.Split(buf[:totalReadBytes], []byte{44}) // 44 is a comma
b := NewBitmap()
for _, elemBytes := range elemsAsBytes {
elemStr := strings.TrimSpace(string(elemBytes))
e, err := strconv.ParseUint(elemStr, 10, 32)
if err != nil {
r.Close()
return nil, fmt.Errorf("could not parse %s as uint32. Reading %s from %s. Cause: %v", elemStr, f.Name, datasetName, err)
}
b.Add(uint32(e))
}
if optimize {
b.RunOptimize()
}
bitmaps[i] = b
}
return bitmaps, nil
}
func benchmarkRealDataAggregate(b *testing.B, aggregator func(b []*Bitmap) uint64) {
if !benchRealData {
b.SkipNow()
}
for _, dataset := range realDatasets {
b.Run(dataset, func(b *testing.B) {
bitmaps, err := retrieveRealDataBitmaps(dataset, true)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
aggregator(bitmaps)
}
})
}
}
func BenchmarkRealDataNext(b *testing.B) {
benchmarkRealDataAggregate(b, func(bitmaps []*Bitmap) uint64 {
tot := uint64(0)
for _, b := range bitmaps {
it := b.Iterator()
for it.HasNext() {
tot += uint64(it.Next())
}
}
return tot
})
}
func BenchmarkRealDataNextMany(b *testing.B) {
benchmarkRealDataAggregate(b, func(bitmaps []*Bitmap) uint64 {
tot := uint64(0)
buf := make([]uint32, 4096)
for _, b := range bitmaps {
it := b.ManyIterator()
for n := it.NextMany(buf); n != 0; n = it.NextMany(buf) {
for _, v := range buf[:n] {
tot += uint64(v)
}
}
}
return tot
})
}
func BenchmarkRealDataParOr(b *testing.B) {
benchmarkRealDataAggregate(b, func(bitmaps []*Bitmap) uint64 {
return ParOr(0, bitmaps...).GetCardinality()
//return ParHeapOr(0, bitmaps...).GetCardinality()
})
}
func BenchmarkRealDataParHeapOr(b *testing.B) {
benchmarkRealDataAggregate(b, func(bitmaps []*Bitmap) uint64 {
return ParHeapOr(0, bitmaps...).GetCardinality()
})
}
func BenchmarkRealDataFastOr(b *testing.B) {
benchmarkRealDataAggregate(b, func(bitmaps []*Bitmap) uint64 {
return FastOr(bitmaps...).GetCardinality()
})
}
|