github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/codec/bench_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package codec 15 16 import ( 17 "testing" 18 19 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 20 "github.com/whtcorpsinc/milevadb/types" 21 "github.com/whtcorpsinc/milevadb/soliton/chunk" 22 ) 23 24 var valueCnt = 100 25 26 func composeEncodedData(size int) []byte { 27 values := make([]types.Causet, 0, size) 28 for i := 0; i < size; i++ { 29 values = append(values, types.NewCauset(i)) 30 } 31 bs, _ := EncodeValue(nil, nil, values...) 32 return bs 33 } 34 35 func BenchmarkDecodeWithSize(b *testing.B) { 36 b.StopTimer() 37 bs := composeEncodedData(valueCnt) 38 b.StartTimer() 39 for i := 0; i < b.N; i++ { 40 Decode(bs, valueCnt) 41 } 42 } 43 44 func BenchmarkDecodeWithOutSize(b *testing.B) { 45 b.StopTimer() 46 bs := composeEncodedData(valueCnt) 47 b.StartTimer() 48 for i := 0; i < b.N; i++ { 49 Decode(bs, 1) 50 } 51 } 52 53 func BenchmarkEncodeIntWithSize(b *testing.B) { 54 for i := 0; i < b.N; i++ { 55 data := make([]byte, 0, 8) 56 EncodeInt(data, 10) 57 } 58 } 59 60 func BenchmarkEncodeIntWithOutSize(b *testing.B) { 61 for i := 0; i < b.N; i++ { 62 EncodeInt(nil, 10) 63 } 64 } 65 66 func BenchmarkDecodeDecimal(b *testing.B) { 67 dec := &types.MyDecimal{} 68 dec.FromFloat64(1211.1211113) 69 precision, frac := dec.PrecisionAndFrac() 70 raw, _ := EncodeDecimal([]byte{}, dec, precision, frac) 71 b.ResetTimer() 72 for i := 0; i < b.N; i++ { 73 DecodeDecimal(raw) 74 } 75 } 76 77 func BenchmarkDecodeOneToChunk(b *testing.B) { 78 str := new(types.Causet) 79 *str = types.NewStringCauset("a") 80 var raw []byte 81 raw = append(raw, bytesFlag) 82 raw = EncodeBytes(raw, str.GetBytes()) 83 intType := types.NewFieldType(allegrosql.TypeLonglong) 84 b.ResetTimer() 85 causetDecoder := NewCausetDecoder(chunk.New([]*types.FieldType{intType}, 32, 32), nil) 86 for i := 0; i < b.N; i++ { 87 causetDecoder.DecodeOne(raw, 0, intType) 88 } 89 }