vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/bench_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tabletserver
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"testing"
    23  
    24  	"context"
    25  
    26  	"vitess.io/vitess/go/sqltypes"
    27  
    28  	querypb "vitess.io/vitess/go/vt/proto/query"
    29  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    30  )
    31  
    32  // Benchmark run on 6/27/17, with optimized byte-level operations
    33  // using bytes2.Buffer:
    34  // BenchmarkExecuteVarBinary-4          300           4751125 ns/op
    35  
    36  // Benchmark run on 6/25/17, prior to improvements:
    37  // BenchmarkExecuteVarBinary-4          100          14610045 ns/op
    38  // BenchmarkExecuteExpression-4        1000           1047798 ns/op
    39  
    40  var benchQuery = "select a from test_table where v = :vtg1 and v0 = :vtg2 and v1 = :vtg3 and v2 = :vtg4 and v3 = :vtg5 and v4 = :vtg6 and v5 = :vtg7 and v6 = :vtg8 and v7 = :vtg9 and v8 = :vtg10 and v9 = :vtg11"
    41  var benchVarValue []byte
    42  
    43  func init() {
    44  	// benchQuerySize is the approximate size of the query.
    45  	// This code is in sync with bench_test.go in vtgate.
    46  	benchQuerySize := 1000000
    47  
    48  	// Size of value is 1/10 size of query. Then we add
    49  	// 10 such values to the where clause.
    50  	baseval := &bytes.Buffer{}
    51  	for i := 0; i < benchQuerySize/100; i++ {
    52  		baseval.WriteString("\\'123456789")
    53  	}
    54  	benchVarValue = baseval.Bytes()
    55  }
    56  
    57  func BenchmarkExecuteVarBinary(b *testing.B) {
    58  	db, tsv := setupTabletServerTest(nil, "")
    59  	defer db.Close()
    60  	defer tsv.StopService()
    61  
    62  	// sql that will be executed in this test
    63  	bv := map[string]*querypb.BindVariable{
    64  		"vtg1": sqltypes.Int64BindVariable(1),
    65  	}
    66  	for i := 2; i <= 11; i++ {
    67  		bv[fmt.Sprintf("vtg%d", i)] = sqltypes.BytesBindVariable(benchVarValue)
    68  	}
    69  
    70  	target := querypb.Target{TabletType: topodatapb.TabletType_PRIMARY}
    71  	db.SetAllowAll(true)
    72  	for i := 0; i < b.N; i++ {
    73  		if _, err := tsv.Execute(context.Background(), &target, benchQuery, bv, 0, 0, nil); err != nil {
    74  			panic(err)
    75  		}
    76  	}
    77  }
    78  
    79  func BenchmarkExecuteExpression(b *testing.B) {
    80  	db, tsv := setupTabletServerTest(nil, "")
    81  	defer db.Close()
    82  	defer tsv.StopService()
    83  
    84  	// sql that will be executed in this test
    85  	bv := map[string]*querypb.BindVariable{
    86  		"vtg1": sqltypes.Int64BindVariable(1),
    87  	}
    88  	for i := 2; i <= 11; i++ {
    89  		bv[fmt.Sprintf("vtg%d", i)] = &querypb.BindVariable{
    90  			Type:  querypb.Type_EXPRESSION,
    91  			Value: benchVarValue,
    92  		}
    93  	}
    94  
    95  	target := querypb.Target{TabletType: topodatapb.TabletType_PRIMARY}
    96  	db.SetAllowAll(true)
    97  	for i := 0; i < b.N; i++ {
    98  		if _, err := tsv.Execute(context.Background(), &target, benchQuery, bv, 0, 0, nil); err != nil {
    99  			panic(err)
   100  		}
   101  	}
   102  }