github.com/outcaste-io/sroar@v0.0.0-20221229172112-1fb64f14314c/benchmark_test.go (about)

     1  /*
     2   * Copyright 2021 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package sroar
    18  
    19  import (
    20  	"math/rand"
    21  	"runtime"
    22  	"testing"
    23  
    24  	"github.com/RoaringBitmap/roaring/roaring64"
    25  )
    26  
    27  // go test -bench BenchmarkMemoryUsage -run -
    28  func BenchmarkMemoryUsage(b *testing.B) {
    29  	b.StopTimer()
    30  	bitmaps := make([]*Bitmap, 0, 10)
    31  
    32  	incr := uint64(1 << 16)
    33  	max := uint64(1<<32 - 1)
    34  	for x := 0; x < 10; x++ {
    35  		rb := NewBitmap()
    36  
    37  		var i uint64
    38  		for i = 0; i <= max-incr; i += incr {
    39  			rb.Set(i)
    40  		}
    41  
    42  		bitmaps = append(bitmaps, rb)
    43  	}
    44  
    45  	var stats runtime.MemStats
    46  	runtime.ReadMemStats(&stats)
    47  	b.Logf("HeapInUse: %d, HeapObjects: %d", stats.HeapInuse, stats.HeapObjects)
    48  	b.StartTimer()
    49  }
    50  
    51  // go test -bench BenchmarkIntersection -run -
    52  func BenchmarkIntersectionRoaring(b *testing.B) {
    53  	b.StopTimer()
    54  	r := rand.New(rand.NewSource(0))
    55  	s1 := NewBitmap()
    56  	sz := int64(150000)
    57  	initsize := 65000
    58  	for i := 0; i < initsize; i++ {
    59  		s1.Set(uint64(r.Int63n(sz)))
    60  	}
    61  
    62  	s2 := NewBitmap()
    63  	sz = int64(100000000)
    64  	initsize = 65000
    65  	for i := 0; i < initsize; i++ {
    66  		s2.Set(uint64(r.Int63n((sz))))
    67  	}
    68  	b.StartTimer()
    69  
    70  	card := 0
    71  	for j := 0; j < b.N; j++ {
    72  		s3 := And(s1, s2)
    73  		card = card + s3.GetCardinality()
    74  	}
    75  	b.Logf("card: %d\n", card)
    76  }
    77  
    78  // go test -bench BenchmarkSet -run -
    79  func BenchmarkSetRoaring(b *testing.B) {
    80  	b.StopTimer()
    81  	r := rand.New(rand.NewSource(0))
    82  	sz := int64(1000000)
    83  	s := NewBitmap()
    84  	b.StartTimer()
    85  	for i := 0; i < b.N; i++ {
    86  		s.Set(uint64(r.Int63n(sz)))
    87  	}
    88  }
    89  
    90  func BenchmarkMerge10K(b *testing.B) {
    91  	var bitmaps []*Bitmap
    92  	for i := 0; i < 10000; i++ {
    93  		bm := NewBitmap()
    94  		for j := 0; j < 1000; j++ {
    95  			x := rand.Uint64() % 1e8 // 10M.
    96  			bm.Set(x)
    97  		}
    98  		bitmaps = append(bitmaps, bm)
    99  	}
   100  
   101  	second := func() *Bitmap {
   102  		var res []*Bitmap
   103  		for i := 0; i < 100; i += 1 {
   104  			input := bitmaps[100*i : 100*i+100]
   105  			out := FastOr(input...)
   106  			res = append(res, out)
   107  		}
   108  		return FastOr(res...)
   109  	}
   110  
   111  	out := FastOr(bitmaps...)
   112  	b.Logf("Out: %s\n", out)
   113  	out2 := second()
   114  	if out2.GetCardinality() != out.GetCardinality() {
   115  		panic("Don't match")
   116  	}
   117  	out3 := FastParOr(8, bitmaps...)
   118  	if out3.GetCardinality() != out.GetCardinality() {
   119  		panic("Don't match")
   120  	}
   121  	b.Logf("card2: %d card3: %d", out2.GetCardinality(), out3.GetCardinality())
   122  
   123  	b.Run("fastor", func(b *testing.B) {
   124  		for i := 0; i < b.N; i++ {
   125  			_ = FastOr(bitmaps...)
   126  		}
   127  	})
   128  
   129  	b.Run("fastor-groups", func(b *testing.B) {
   130  		for i := 0; i < b.N; i++ {
   131  			_ = second()
   132  		}
   133  	})
   134  	b.Run("fastparor", func(b *testing.B) {
   135  		for i := 0; i < b.N; i++ {
   136  			_ = FastParOr(4, bitmaps...)
   137  		}
   138  	})
   139  }
   140  
   141  func BenchmarkRemoveRange(b *testing.B) {
   142  	bm := NewBitmap()
   143  	N := uint64(1e5)
   144  	for i := uint64(0); i < N; i++ {
   145  		bm.Set(uint64(i))
   146  	}
   147  
   148  	bench := func(b *testing.B, factor uint64) {
   149  		sz := uint64(N / factor)
   150  		cnt := uint64(N / sz)
   151  		for j := 0; j < b.N; j++ {
   152  			b.StopTimer()
   153  			bm2 := bm.Clone()
   154  			b.StartTimer()
   155  			for i := uint64(0); i < cnt; i++ {
   156  				bm2.RemoveRange(i*sz, (i+1)*sz)
   157  			}
   158  		}
   159  	}
   160  	b.Run("N/2", func(b *testing.B) {
   161  		bench(b, 2)
   162  	})
   163  	b.Run("N/4", func(b *testing.B) {
   164  		bench(b, 4)
   165  	})
   166  	b.Run("N/16", func(b *testing.B) {
   167  		bench(b, 16)
   168  	})
   169  	b.Run("N/256", func(b *testing.B) {
   170  		bench(b, 256)
   171  	})
   172  }
   173  
   174  func BenchmarkRemoveRangeRoaring64(b *testing.B) {
   175  	bm := roaring64.NewBitmap()
   176  	N := uint64(1e5)
   177  	for i := uint64(0); i < N; i++ {
   178  		bm.Add(uint64(i))
   179  	}
   180  
   181  	bench := func(b *testing.B, factor uint64) {
   182  		sz := uint64(N / factor)
   183  		cnt := uint64(N / sz)
   184  		for j := 0; j < b.N; j++ {
   185  			b.StopTimer()
   186  			bm2 := bm.Clone()
   187  			b.StartTimer()
   188  			for i := uint64(0); i < cnt; i++ {
   189  				bm2.RemoveRange(i*sz, (i+1)*sz)
   190  			}
   191  		}
   192  	}
   193  	b.Run("N/2", func(b *testing.B) {
   194  		bench(b, 2)
   195  	})
   196  	b.Run("N/4", func(b *testing.B) {
   197  		bench(b, 4)
   198  	})
   199  	b.Run("N/16", func(b *testing.B) {
   200  		bench(b, 16)
   201  	})
   202  	b.Run("N/256", func(b *testing.B) {
   203  		bench(b, 256)
   204  	})
   205  }
   206  
   207  func BenchmarkSelectSroar(b *testing.B) {
   208  	bm := NewBitmap()
   209  	N := uint64(1e5)
   210  	for i := uint64(0); i < N; i++ {
   211  		bm.Set(uint64(i))
   212  	}
   213  
   214  	b.ResetTimer()
   215  	for i := 0; i < b.N; i++ {
   216  		for j := uint64(0); j < N; j++ {
   217  			bm.Select(j)
   218  		}
   219  	}
   220  }
   221  
   222  func BenchmarkSelectRoaring64(b *testing.B) {
   223  	bm := roaring64.NewBitmap()
   224  	N := uint64(1e5)
   225  	for i := uint64(0); i < N; i++ {
   226  		bm.Add(uint64(i))
   227  	}
   228  
   229  	b.ResetTimer()
   230  	for i := 0; i < b.N; i++ {
   231  		for j := uint64(0); j < N; j++ {
   232  			bm.Select(j)
   233  		}
   234  	}
   235  }