From a047a98bc32bf1073b11c8add0e68fc740e1f964 Mon Sep 17 00:00:00 2001 From: Harshit Gangal Date: Sat, 23 Sep 2023 13:24:36 +0530 Subject: [PATCH] benchmark: impact of the change with existing implementation Signed-off-by: Harshit Gangal --- go/test/endtoend/cluster/cluster_util.go | 2 +- go/test/endtoend/utils/utils.go | 2 +- .../queries/benchmark/benchmark_test.go | 80 +++++++++++ .../vtgate/queries/benchmark/main_test.go | 124 ++++++++++++++++++ .../queries/benchmark/sharded_schema.sql | 16 +++ .../vtgate/queries/benchmark/vschema.json | 18 +++ 6 files changed, 240 insertions(+), 2 deletions(-) create mode 100644 go/test/endtoend/vtgate/queries/benchmark/benchmark_test.go create mode 100644 go/test/endtoend/vtgate/queries/benchmark/main_test.go create mode 100644 go/test/endtoend/vtgate/queries/benchmark/sharded_schema.sql create mode 100644 go/test/endtoend/vtgate/queries/benchmark/vschema.json diff --git a/go/test/endtoend/cluster/cluster_util.go b/go/test/endtoend/cluster/cluster_util.go index 1af9504389b..3d442bbb576 100644 --- a/go/test/endtoend/cluster/cluster_util.go +++ b/go/test/endtoend/cluster/cluster_util.go @@ -127,7 +127,7 @@ func VerifyRowsInTablet(t *testing.T, vttablet *Vttablet, ksName string, expecte } // PanicHandler handles the panic in the testcase. -func PanicHandler(t *testing.T) { +func PanicHandler(t testing.TB) { err := recover() if t == nil { return diff --git a/go/test/endtoend/utils/utils.go b/go/test/endtoend/utils/utils.go index c0137b27066..4fc7d7cfecf 100644 --- a/go/test/endtoend/utils/utils.go +++ b/go/test/endtoend/utils/utils.go @@ -169,7 +169,7 @@ func ExecCompareMySQL(t *testing.T, vtConn, mysqlConn *mysql.Conn, query string) // ExecAllowError executes the given query without failing the test if it produces // an error. The error is returned to the client, along with the result set. -func ExecAllowError(t *testing.T, conn *mysql.Conn, query string) (*sqltypes.Result, error) { +func ExecAllowError(t testing.TB, conn *mysql.Conn, query string) (*sqltypes.Result, error) { t.Helper() return conn.ExecuteFetch(query, 1000, true) } diff --git a/go/test/endtoend/vtgate/queries/benchmark/benchmark_test.go b/go/test/endtoend/vtgate/queries/benchmark/benchmark_test.go new file mode 100644 index 00000000000..1fa1232f2a4 --- /dev/null +++ b/go/test/endtoend/vtgate/queries/benchmark/benchmark_test.go @@ -0,0 +1,80 @@ +/* +Copyright 2022 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package dml + +import ( + "fmt" + "math/rand" + "strconv" + "strings" + "testing" + + "vitess.io/vitess/go/test/endtoend/utils" +) + +type testQuery struct { + tableName string + cols []string + intTyp []bool +} + +func (tq *testQuery) getInsertQuery(rows int) string { + var allRows []string + for i := 0; i < rows; i++ { + var row []string + for _, isInt := range tq.intTyp { + if isInt { + row = append(row, strconv.Itoa(i)) + continue + } + row = append(row, "'"+getRandomString(50)+"'") + } + allRows = append(allRows, "("+strings.Join(row, ",")+")") + } + return fmt.Sprintf("insert into %s(%s) values %s", tq.tableName, strings.Join(tq.cols, ","), strings.Join(allRows, ",")) +} + +func getRandomString(size int) string { + var str strings.Builder + + for i := 0; i < size; i++ { + str.WriteByte(byte((rand.Int() % 26) + 97)) + } + return str.String() +} + +func BenchmarkShardedTblNoLookup(b *testing.B) { + conn, closer := start(b) + defer closer() + + cols := []string{"id", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10", "c11", "c12"} + intType := make([]bool, len(cols)) + intType[0] = true + tq := &testQuery{ + tableName: "tbl_no_lkp_vdx", + cols: cols, + intTyp: intType, + } + for _, rows := range []int{1, 10, 100, 500, 1000, 5000, 10000} { + insStmt := tq.getInsertQuery(rows) + b.Run(fmt.Sprintf("16-shards-%d-rows", rows), func(b *testing.B) { + for i := 0; i < b.N; i++ { + _ = utils.Exec(b, conn, insStmt) + } + }) + } +} diff --git a/go/test/endtoend/vtgate/queries/benchmark/main_test.go b/go/test/endtoend/vtgate/queries/benchmark/main_test.go new file mode 100644 index 00000000000..6cb716491ab --- /dev/null +++ b/go/test/endtoend/vtgate/queries/benchmark/main_test.go @@ -0,0 +1,124 @@ +/* +Copyright 2022 The Vitess Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package dml + +import ( + "context" + _ "embed" + "flag" + "os" + "testing" + + "github.com/stretchr/testify/require" + + "vitess.io/vitess/go/mysql" + "vitess.io/vitess/go/test/endtoend/cluster" + "vitess.io/vitess/go/test/endtoend/utils" +) + +var ( + clusterInstance *cluster.LocalProcessCluster + vtParams mysql.ConnParams + mysqlParams mysql.ConnParams + sKs = "sks" + uKs = "uks" + cell = "test" + + //go:embed sharded_schema.sql + sSchemaSQL string + + //go:embed vschema.json + sVSchema string +) + +var ( + shards4 = []string{ + "-40", "40-80", "80-c0", "c0-", + } + + shards8 = []string{ + "-20", "20-40", "40-60", "60-80", "80-a0", "a0-c0", "c0-e0", "e0-", + } + + shards16 = []string{ + "-10", "10-20", "20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", "90-a0", "a0-b0", "b0-c0", "c0-d0", "d0-e0", "e0-f0", "f0-", + } + + shards32 = []string{ + "-05", "05-10", "10-15", "15-20", "20-25", "25-30", "30-35", "35-40", "40-45", "45-50", "50-55", "55-60", "60-65", "65-70", "70-75", "75-80", + "80-85", "85-90", "90-95", "95-a0", "a0-a5", "a5-b0", "b0-b5", "b5-c0", "c0-c5", "c5-d0", "d0-d5", "d5-e0", "e0-e5", "e5-f0", "f0-f5", "f5-", + } +) + +func TestMain(m *testing.M) { + defer cluster.PanicHandler(nil) + flag.Parse() + + exitCode := func() int { + clusterInstance = cluster.NewCluster(cell, "localhost") + defer clusterInstance.Teardown() + + // Start topo server + err := clusterInstance.StartTopo() + if err != nil { + return 1 + } + + // Start sharded keyspace + sKeyspace := &cluster.Keyspace{ + Name: sKs, + SchemaSQL: sSchemaSQL, + VSchema: sVSchema, + } + + err = clusterInstance.StartKeyspace(*sKeyspace, shards4, 0, false) + if err != nil { + return 1 + } + + // Start vtgate + err = clusterInstance.StartVtgate() + if err != nil { + return 1 + } + + vtParams = clusterInstance.GetVTParams(sKs) + + return m.Run() + }() + os.Exit(exitCode) +} + +func start(b *testing.B) (*mysql.Conn, func()) { + conn, err := mysql.Connect(context.Background(), &vtParams) + require.NoError(b, err) + + deleteAll := func() { + tables := []string{"tbl_no_lkp_vdx"} + for _, table := range tables { + _, _ = utils.ExecAllowError(b, conn, "delete from "+table) + } + } + + deleteAll() + + return conn, func() { + deleteAll() + conn.Close() + cluster.PanicHandler(b) + } +} diff --git a/go/test/endtoend/vtgate/queries/benchmark/sharded_schema.sql b/go/test/endtoend/vtgate/queries/benchmark/sharded_schema.sql new file mode 100644 index 00000000000..850b6ffc15a --- /dev/null +++ b/go/test/endtoend/vtgate/queries/benchmark/sharded_schema.sql @@ -0,0 +1,16 @@ +create table tbl_no_lkp_vdx +( + id bigint, + c1 varchar(50), + c2 varchar(50), + c3 varchar(50), + c4 varchar(50), + c5 varchar(50), + c6 varchar(50), + c7 varchar(50), + c8 varchar(50), + c9 varchar(50), + c10 varchar(50), + c11 varchar(50), + c12 varchar(50) +) Engine = InnoDB; \ No newline at end of file diff --git a/go/test/endtoend/vtgate/queries/benchmark/vschema.json b/go/test/endtoend/vtgate/queries/benchmark/vschema.json new file mode 100644 index 00000000000..4970e8b7437 --- /dev/null +++ b/go/test/endtoend/vtgate/queries/benchmark/vschema.json @@ -0,0 +1,18 @@ +{ + "sharded": true, + "vindexes": { + "xxhash": { + "type": "xxhash" + } + }, + "tables": { + "tbl_no_lkp_vdx": { + "column_vindexes": [ + { + "column": "id", + "name": "xxhash" + } + ] + } + } +} \ No newline at end of file