github.com/KinWaiYuen/client-go/v2@v2.5.4/internal/unionstore/memdb_bench_test.go (about) 1 // Copyright 2021 TiKV Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // NOTE: The code in this file is based on code from the 16 // TiDB project, licensed under the Apache License v 2.0 17 // 18 // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/unionstore/memdb_bench_test.go 19 // 20 21 // Copyright 2020 PingCAP, Inc. 22 // 23 // Licensed under the Apache License, Version 2.0 (the "License"); 24 // you may not use this file except in compliance with the License. 25 // You may obtain a copy of the License at 26 // 27 // http://www.apache.org/licenses/LICENSE-2.0 28 // 29 // Unless required by applicable law or agreed to in writing, software 30 // distributed under the License is distributed on an "AS IS" BASIS, 31 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 32 // See the License for the specific language governing permissions and 33 // limitations under the License. 34 35 package unionstore 36 37 import ( 38 "encoding/binary" 39 "math/rand" 40 "testing" 41 ) 42 43 const ( 44 keySize = 16 45 valueSize = 128 46 ) 47 48 func BenchmarkLargeIndex(b *testing.B) { 49 buf := make([][valueSize]byte, 10000000) 50 for i := range buf { 51 binary.LittleEndian.PutUint32(buf[i][:], uint32(i)) 52 } 53 db := newMemDB() 54 b.ResetTimer() 55 56 for i := range buf { 57 db.Set(buf[i][:keySize], buf[i][:]) 58 } 59 } 60 61 func BenchmarkPut(b *testing.B) { 62 buf := make([][valueSize]byte, b.N) 63 for i := range buf { 64 binary.BigEndian.PutUint32(buf[i][:], uint32(i)) 65 } 66 67 p := newMemDB() 68 b.ResetTimer() 69 70 for i := range buf { 71 p.Set(buf[i][:keySize], buf[i][:]) 72 } 73 } 74 75 func BenchmarkPutRandom(b *testing.B) { 76 buf := make([][valueSize]byte, b.N) 77 for i := range buf { 78 binary.LittleEndian.PutUint32(buf[i][:], uint32(rand.Int())) 79 } 80 81 p := newMemDB() 82 b.ResetTimer() 83 84 for i := range buf { 85 p.Set(buf[i][:keySize], buf[i][:]) 86 } 87 } 88 89 func BenchmarkGet(b *testing.B) { 90 buf := make([][valueSize]byte, b.N) 91 for i := range buf { 92 binary.BigEndian.PutUint32(buf[i][:], uint32(i)) 93 } 94 95 p := newMemDB() 96 for i := range buf { 97 p.Set(buf[i][:keySize], buf[i][:]) 98 } 99 100 b.ResetTimer() 101 for i := range buf { 102 p.Get(buf[i][:keySize]) 103 } 104 } 105 106 func BenchmarkGetRandom(b *testing.B) { 107 buf := make([][valueSize]byte, b.N) 108 for i := range buf { 109 binary.LittleEndian.PutUint32(buf[i][:], uint32(rand.Int())) 110 } 111 112 p := newMemDB() 113 for i := range buf { 114 p.Set(buf[i][:keySize], buf[i][:]) 115 } 116 117 b.ResetTimer() 118 for i := 0; i < b.N; i++ { 119 p.Get(buf[i][:keySize]) 120 } 121 } 122 123 var opCnt = 100000 124 125 func BenchmarkMemDbBufferSequential(b *testing.B) { 126 data := make([][]byte, opCnt) 127 for i := 0; i < opCnt; i++ { 128 data[i] = encodeInt(i) 129 } 130 buffer := newMemDB() 131 benchmarkSetGet(b, buffer, data) 132 b.ReportAllocs() 133 } 134 135 func BenchmarkMemDbBufferRandom(b *testing.B) { 136 data := make([][]byte, opCnt) 137 for i := 0; i < opCnt; i++ { 138 data[i] = encodeInt(i) 139 } 140 shuffle(data) 141 buffer := newMemDB() 142 benchmarkSetGet(b, buffer, data) 143 b.ReportAllocs() 144 } 145 146 func BenchmarkMemDbIter(b *testing.B) { 147 buffer := newMemDB() 148 benchIterator(b, buffer) 149 b.ReportAllocs() 150 } 151 152 func BenchmarkMemDbCreation(b *testing.B) { 153 for i := 0; i < b.N; i++ { 154 newMemDB() 155 } 156 b.ReportAllocs() 157 } 158 159 func shuffle(slc [][]byte) { 160 N := len(slc) 161 for i := 0; i < N; i++ { 162 // choose index uniformly in [i, N-1] 163 r := i + rand.Intn(N-i) 164 slc[r], slc[i] = slc[i], slc[r] 165 } 166 } 167 func benchmarkSetGet(b *testing.B, buffer *MemDB, data [][]byte) { 168 b.ResetTimer() 169 for i := 0; i < b.N; i++ { 170 for _, k := range data { 171 buffer.Set(k, k) 172 } 173 for _, k := range data { 174 buffer.Get(k) 175 } 176 } 177 } 178 179 func benchIterator(b *testing.B, buffer *MemDB) { 180 for k := 0; k < opCnt; k++ { 181 buffer.Set(encodeInt(k), encodeInt(k)) 182 } 183 b.ResetTimer() 184 for i := 0; i < b.N; i++ { 185 iter, err := buffer.Iter(nil, nil) 186 if err != nil { 187 b.Error(err) 188 } 189 for iter.Valid() { 190 iter.Next() 191 } 192 iter.Close() 193 } 194 }