github.com/apache/arrow/go/v7@v7.0.1/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/v7/arrow/bitutil" 24 "github.com/apache/arrow/go/v7/parquet/internal/testutils" 25 "github.com/apache/arrow/go/v7/parquet/internal/utils" 26 ) 27 28 func randomBitsBuffer(nbits, setPct int64) []byte { 29 rag := testutils.NewRandomArrayGenerator(23) 30 prob := float64(0) 31 if setPct != -1 { 32 prob = float64(setPct) / 100.0 33 } 34 buf := make([]byte, int(bitutil.BytesForBits(nbits))) 35 rag.GenerateBitmap(buf, nbits, prob) 36 37 if setPct == -1 { 38 wr := bitutil.NewBitmapWriter(buf, 0, int(nbits)) 39 for i := int64(0); i < nbits; i++ { 40 if i%2 == 0 { 41 wr.Set() 42 } else { 43 wr.Clear() 44 } 45 wr.Next() 46 } 47 } 48 return buf 49 } 50 51 func testBitRunReader(rdr utils.BitRunReader) (setTotal int64) { 52 for { 53 br := rdr.NextRun() 54 if br.Len == 0 { 55 break 56 } 57 if br.Set { 58 setTotal += br.Len 59 } 60 } 61 return 62 } 63 64 func BenchmarkBitRunReader(b *testing.B) { 65 const numBits = 4096 66 for _, pct := range []int64{1, 0, 10, 25, 50, 60, 75, 99} { 67 buf := randomBitsBuffer(numBits, pct) 68 b.Run("set pct "+strconv.Itoa(int(pct)), func(b *testing.B) { 69 b.Run("linear", func(b *testing.B) { 70 b.SetBytes(numBits / 8) 71 for i := 0; i < b.N; i++ { 72 rdr := linearBitRunReader{bitutil.NewBitmapReader(buf, 0, numBits)} 73 testBitRunReader(rdr) 74 } 75 }) 76 b.Run("internal", func(b *testing.B) { 77 b.SetBytes(numBits / 8) 78 for i := 0; i < b.N; i++ { 79 rdr := utils.NewBitRunReader(buf, 0, numBits) 80 testBitRunReader(rdr) 81 } 82 }) 83 }) 84 } 85 } 86 87 func testSetBitRunReader(rdr utils.SetBitRunReader) (setTotal int64) { 88 for { 89 br := rdr.NextRun() 90 if br.Length == 0 { 91 break 92 } 93 setTotal += br.Length 94 } 95 return 96 } 97 98 func BenchmarkSetBitRunReader(b *testing.B) { 99 const numBits = 4096 100 for _, pct := range []int64{1, 0, 10, 25, 50, 60, 75, 99} { 101 buf := randomBitsBuffer(numBits, pct) 102 b.Run("set pct "+strconv.Itoa(int(pct)), func(b *testing.B) { 103 b.Run("reader", func(b *testing.B) { 104 b.SetBytes(numBits / 8) 105 for i := 0; i < b.N; i++ { 106 rdr := utils.NewSetBitRunReader(buf, 0, numBits) 107 testSetBitRunReader(rdr) 108 } 109 }) 110 b.Run("reverse rdr", func(b *testing.B) { 111 b.SetBytes(numBits / 8) 112 for i := 0; i < b.N; i++ { 113 rdr := utils.NewReverseSetBitRunReader(buf, 0, numBits) 114 testSetBitRunReader(rdr) 115 } 116 }) 117 }) 118 } 119 }