github.com/apache/arrow/go/v14@v14.0.2/parquet/internal/utils/bit_benchmark_test.go (about) 1 // Licensed to the Apache Software Foundation (ASF) under one 2 // or more contributor license agreements. See the NOTICE file 3 // distributed with this work for additional information 4 // regarding copyright ownership. The ASF licenses this file 5 // to you under the Apache License, Version 2.0 (the 6 // "License"); you may not use this file except in compliance 7 // with the License. You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 package utils_test 18 19 import ( 20 "strconv" 21 "testing" 22 23 "github.com/apache/arrow/go/v14/arrow/bitutil" 24 "github.com/apache/arrow/go/v14/internal/bitutils" 25 "github.com/apache/arrow/go/v14/parquet/internal/testutils" 26 ) 27 28 type linearBitRunReader struct { 29 reader *bitutil.BitmapReader 30 } 31 32 func (l linearBitRunReader) NextRun() bitutils.BitRun { 33 r := bitutils.BitRun{0, l.reader.Set()} 34 for l.reader.Pos() < l.reader.Len() && l.reader.Set() == r.Set { 35 r.Len++ 36 l.reader.Next() 37 } 38 return r 39 } 40 41 func randomBitsBuffer(nbits, setPct int64) []byte { 42 rag := testutils.NewRandomArrayGenerator(23) 43 prob := float64(0) 44 if setPct != -1 { 45 prob = float64(setPct) / 100.0 46 } 47 buf := make([]byte, int(bitutil.BytesForBits(nbits))) 48 rag.GenerateBitmap(buf, nbits, prob) 49 50 if setPct == -1 { 51 wr := bitutil.NewBitmapWriter(buf, 0, int(nbits)) 52 for i := int64(0); i < nbits; i++ { 53 if i%2 == 0 { 54 wr.Set() 55 } else { 56 wr.Clear() 57 } 58 wr.Next() 59 } 60 } 61 return buf 62 } 63 64 func testBitRunReader(rdr bitutils.BitRunReader) (setTotal int64) { 65 for { 66 br := rdr.NextRun() 67 if br.Len == 0 { 68 break 69 } 70 if br.Set { 71 setTotal += br.Len 72 } 73 } 74 return 75 } 76 77 func BenchmarkBitRunReader(b *testing.B) { 78 const numBits = 4096 79 for _, pct := range []int64{1, 0, 10, 25, 50, 60, 75, 99} { 80 buf := randomBitsBuffer(numBits, pct) 81 b.Run("set pct "+strconv.Itoa(int(pct)), func(b *testing.B) { 82 b.Run("linear", func(b *testing.B) { 83 b.SetBytes(numBits / 8) 84 for i := 0; i < b.N; i++ { 85 rdr := linearBitRunReader{bitutil.NewBitmapReader(buf, 0, numBits)} 86 testBitRunReader(rdr) 87 } 88 }) 89 b.Run("internal", func(b *testing.B) { 90 b.SetBytes(numBits / 8) 91 for i := 0; i < b.N; i++ { 92 rdr := bitutils.NewBitRunReader(buf, 0, numBits) 93 testBitRunReader(rdr) 94 } 95 }) 96 }) 97 } 98 } 99 100 func testSetBitRunReader(rdr bitutils.SetBitRunReader) (setTotal int64) { 101 for { 102 br := rdr.NextRun() 103 if br.Length == 0 { 104 break 105 } 106 setTotal += br.Length 107 } 108 return 109 } 110 111 func BenchmarkSetBitRunReader(b *testing.B) { 112 const numBits = 4096 113 for _, pct := range []int64{1, 0, 10, 25, 50, 60, 75, 99} { 114 buf := randomBitsBuffer(numBits, pct) 115 b.Run("set pct "+strconv.Itoa(int(pct)), func(b *testing.B) { 116 b.Run("reader", func(b *testing.B) { 117 b.SetBytes(numBits / 8) 118 for i := 0; i < b.N; i++ { 119 rdr := bitutils.NewSetBitRunReader(buf, 0, numBits) 120 testSetBitRunReader(rdr) 121 } 122 }) 123 b.Run("reverse rdr", func(b *testing.B) { 124 b.SetBytes(numBits / 8) 125 for i := 0; i < b.N; i++ { 126 rdr := bitutils.NewReverseSetBitRunReader(buf, 0, numBits) 127 testSetBitRunReader(rdr) 128 } 129 }) 130 }) 131 } 132 }