vitess.io/vitess@v0.16.2/go/vt/vtgate/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 vtgate 18 19 import ( 20 "bytes" 21 "fmt" 22 "testing" 23 24 "context" 25 26 topodatapb "vitess.io/vitess/go/vt/proto/topodata" 27 vtgatepb "vitess.io/vitess/go/vt/proto/vtgate" 28 ) 29 30 // Benchmark run on 6/27/17, with optimized byte-level operations 31 // using bytes2.Buffer: 32 // BenchmarkWithNormalizer-4 300 5694660 ns/op 33 34 // Benchmark run on 6/25/17, prior to improvements: 35 // BenchmarkWithNormalizer-4 100 10629161 ns/op 36 // BenchmarkWithoutNormalizer-4 200000 8584 ns/op 37 38 var benchQuery string 39 40 func init() { 41 // benchQuerySize is the approximate size of the query. 42 benchQuerySize := 1000000 43 44 // Size of value is 1/10 size of query. Then we add 45 // 10 such values to the where clause. 46 baseval := &bytes.Buffer{} 47 for i := 0; i < benchQuerySize/100; i++ { 48 // Add an escape character: This will force the upcoming 49 // tokenizer improvement to still create a copy of the string. 50 // Then we can see if avoiding the copy will be worth it. 51 baseval.WriteString("\\'123456789") 52 } 53 54 buf := &bytes.Buffer{} 55 buf.WriteString("select a from t1 where v = 1") 56 for i := 0; i < 10; i++ { 57 fmt.Fprintf(buf, " and v%d = '%d%s'", i, i, baseval.String()) 58 } 59 benchQuery = buf.String() 60 // fmt.Printf("len: %d\n", len(benchQuery)) 61 } 62 63 func BenchmarkWithNormalizer(b *testing.B) { 64 createSandbox(KsTestUnsharded) 65 hcVTGateTest.Reset() 66 _ = hcVTGateTest.AddTestTablet("aa", "1.1.1.1", 1001, KsTestUnsharded, "0", topodatapb.TabletType_PRIMARY, true, 1, nil) 67 saved := rpcVTGate.executor.normalize 68 rpcVTGate.executor.normalize = true 69 defer func() { rpcVTGate.executor.normalize = saved }() 70 71 for i := 0; i < b.N; i++ { 72 _, _, err := rpcVTGate.Execute( 73 context.Background(), 74 &vtgatepb.Session{ 75 TargetString: "@primary", 76 Options: executeOptions, 77 }, 78 benchQuery, 79 nil, 80 ) 81 if err != nil { 82 panic(err) 83 } 84 } 85 } 86 87 func BenchmarkWithoutNormalizer(b *testing.B) { 88 createSandbox(KsTestUnsharded) 89 hcVTGateTest.Reset() 90 _ = hcVTGateTest.AddTestTablet("aa", "1.1.1.1", 1001, KsTestUnsharded, "0", topodatapb.TabletType_PRIMARY, true, 1, nil) 91 saved := rpcVTGate.executor.normalize 92 rpcVTGate.executor.normalize = false 93 defer func() { rpcVTGate.executor.normalize = saved }() 94 95 for i := 0; i < b.N; i++ { 96 _, _, err := rpcVTGate.Execute( 97 context.Background(), 98 &vtgatepb.Session{ 99 TargetString: "@primary", 100 Options: executeOptions, 101 }, 102 benchQuery, 103 nil, 104 ) 105 if err != nil { 106 panic(err) 107 } 108 } 109 }