github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/rowcodec/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 rowcodec_test
    15  
    16  import (
    17  	"testing"
    18  	"time"
    19  
    20  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    21  	"github.com/whtcorpsinc/milevadb/blockcodec"
    22  	"github.com/whtcorpsinc/milevadb/ekv"
    23  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    24  	"github.com/whtcorpsinc/milevadb/soliton/rowcodec"
    25  	"github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx"
    26  	"github.com/whtcorpsinc/milevadb/types"
    27  )
    28  
    29  func BenchmarkEncode(b *testing.B) {
    30  	b.ReportAllocs()
    31  	oldRow := types.MakeCausets(1, "abc", 1.1)
    32  	var xb rowcodec.CausetEncoder
    33  	var buf []byte
    34  	defCausIDs := []int64{1, 2, 3}
    35  	var err error
    36  	for i := 0; i < b.N; i++ {
    37  		buf, err = xb.Encode(nil, defCausIDs, oldRow, buf)
    38  		if err != nil {
    39  			b.Fatal(err)
    40  		}
    41  	}
    42  }
    43  
    44  func BenchmarkEncodeFromOldRow(b *testing.B) {
    45  	b.ReportAllocs()
    46  	oldRow := types.MakeCausets(1, "abc", 1.1)
    47  	oldRowData, err := blockcodec.EncodeOldRow(new(stmtctx.StatementContext), oldRow, []int64{1, 2, 3}, nil, nil)
    48  	if err != nil {
    49  		b.Fatal(err)
    50  	}
    51  	var xb rowcodec.CausetEncoder
    52  	var buf []byte
    53  	for i := 0; i < b.N; i++ {
    54  		buf, err = rowcodec.EncodeFromOldRow(&xb, nil, oldRowData, buf)
    55  		if err != nil {
    56  			b.Fatal(err)
    57  		}
    58  	}
    59  }
    60  
    61  func BenchmarkDecode(b *testing.B) {
    62  	b.ReportAllocs()
    63  	oldRow := types.MakeCausets(1, "abc", 1.1)
    64  	defCausIDs := []int64{-1, 2, 3}
    65  	tps := []*types.FieldType{
    66  		types.NewFieldType(allegrosql.TypeLonglong),
    67  		types.NewFieldType(allegrosql.TypeString),
    68  		types.NewFieldType(allegrosql.TypeDouble),
    69  	}
    70  	var xb rowcodec.CausetEncoder
    71  	xRowData, err := xb.Encode(nil, defCausIDs, oldRow, nil)
    72  	if err != nil {
    73  		b.Fatal(err)
    74  	}
    75  	defcaus := make([]rowcodec.DefCausInfo, len(tps))
    76  	for i, tp := range tps {
    77  		defcaus[i] = rowcodec.DefCausInfo{
    78  			ID: defCausIDs[i],
    79  			Ft: tp,
    80  		}
    81  	}
    82  	causetDecoder := rowcodec.NewChunkCausetDecoder(defcaus, []int64{-1}, nil, time.Local)
    83  	chk := chunk.NewChunkWithCapacity(tps, 1)
    84  	for i := 0; i < b.N; i++ {
    85  		chk.Reset()
    86  		err = causetDecoder.DecodeToChunk(xRowData, ekv.IntHandle(1), chk)
    87  		if err != nil {
    88  			b.Fatal(err)
    89  		}
    90  	}
    91  }