github.com/petermattis/pebble@v0.0.0-20190905164901-ab51a2166067/ptable/column_test.go (about)

     1  // Copyright 2018 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  package ptable
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  	"time"
    11  
    12  	"golang.org/x/exp/rand"
    13  )
    14  
    15  func randNullBitmap(rng *rand.Rand, size int) NullBitmap {
    16  	var builder nullBitmapBuilder
    17  	for i := 0; i < size; i++ {
    18  		builder = builder.set(i, rng.Intn(2) == 0)
    19  	}
    20  	return makeNullBitmap(builder)
    21  }
    22  
    23  func BenchmarkNullBitmapGet(b *testing.B) {
    24  	const size = 4096
    25  	rng := rand.New(rand.NewSource(uint64(time.Now().UnixNano())))
    26  	bmap := randNullBitmap(rng, size)
    27  	b.ResetTimer()
    28  
    29  	var sum int
    30  	for i, k := 0, 0; i < b.N; i += k {
    31  		k = size
    32  		if k > b.N-i {
    33  			k = b.N - i
    34  		}
    35  		for j := 0; j < k; j++ {
    36  			if bmap.Null(j) {
    37  				sum++
    38  			}
    39  		}
    40  	}
    41  	if testing.Verbose() {
    42  		fmt.Println(sum)
    43  	}
    44  }
    45  
    46  func BenchmarkNullBitmapRank(b *testing.B) {
    47  	const size = 4096
    48  	rng := rand.New(rand.NewSource(uint64(time.Now().UnixNano())))
    49  	bmap := randNullBitmap(rng, size)
    50  	b.ResetTimer()
    51  
    52  	var sum int
    53  	for i, k := 0, 0; i < b.N; i += k {
    54  		k = size
    55  		if k > b.N-i {
    56  			k = b.N - i
    57  		}
    58  		for j := 0; j < k; j++ {
    59  			if r := bmap.Rank(j); r >= 0 {
    60  				sum++
    61  			}
    62  		}
    63  	}
    64  	if testing.Verbose() {
    65  		fmt.Println(sum)
    66  	}
    67  }