github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/sqlmodel/multirow_bench_test.go (about)

     1  // Copyright 2023 PingCAP, 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 sqlmodel
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  	"time"
    20  
    21  	cdcmodel "github.com/pingcap/tiflow/cdc/model"
    22  )
    23  
    24  func prepareDataOneColoumnPK(t *testing.T, batch int) []*RowChange {
    25  	source := &cdcmodel.TableName{Schema: "db", Table: "tb"}
    26  	target := &cdcmodel.TableName{Schema: "db", Table: "tb"}
    27  
    28  	sourceTI := mockTableInfo(t, `CREATE TABLE tb (c INT, c2 INT, c3 INT,
    29  	c4 VARCHAR(10), c5 VARCHAR(100), c6 VARCHAR(1000), PRIMARY KEY (c))`)
    30  	targetTI := mockTableInfo(t, `CREATE TABLE tb (c INT, c2 INT, c3 INT,
    31  	c4 VARCHAR(10), c5 VARCHAR(100), c6 VARCHAR(1000), PRIMARY KEY (c))`)
    32  
    33  	changes := make([]*RowChange, 0, batch)
    34  	for i := 0; i < batch; i++ {
    35  		change := NewRowChange(source, target,
    36  			[]interface{}{i + 1, i + 2, i + 3, "c4", "c5", "c6"},
    37  			[]interface{}{i + 10, i + 20, i + 30, "c4", "c5", "c6"},
    38  			sourceTI, targetTI, nil)
    39  		changes = append(changes, change)
    40  	}
    41  	return changes
    42  }
    43  
    44  func prepareDataMultiColumnsPK(t *testing.T, batch int) []*RowChange {
    45  	source := &cdcmodel.TableName{Schema: "db", Table: "tb"}
    46  	target := &cdcmodel.TableName{Schema: "db", Table: "tb"}
    47  
    48  	sourceTI := mockTableInfo(t, `CREATE TABLE tb (c1 INT, c2 INT, c3 INT, c4 INT,
    49  	c5 VARCHAR(10), c6 VARCHAR(100), c7 VARCHAR(1000), c8 timestamp, c9 timestamp,
    50  	PRIMARY KEY (c1, c2, c3, c4))`)
    51  	targetTI := mockTableInfo(t, `CREATE TABLE tb (c1 INT, c2 INT, c3 INT, c4 INT,
    52  	c5 VARCHAR(10), c6 VARCHAR(100), c7 VARCHAR(1000), c8 timestamp, c9 timestamp,
    53  	PRIMARY KEY (c1, c2, c3, c4))`)
    54  
    55  	changes := make([]*RowChange, 0, batch)
    56  	for i := 0; i < batch; i++ {
    57  		change := NewRowChange(source, target,
    58  			[]interface{}{i + 1, i + 2, i + 3, i + 4, "c4", "c5", "c6", "c7", time.Time{}, time.Time{}},
    59  			[]interface{}{i + 10, i + 20, i + 30, i + 40, "c4", "c5", "c6", "c7", time.Time{}, time.Time{}},
    60  			sourceTI, targetTI, nil)
    61  		changes = append(changes, change)
    62  	}
    63  	return changes
    64  }
    65  
    66  // bench cmd: go test -run='^$' -benchmem -bench '^(BenchmarkGenUpdate)$' github.com/pingcap/tiflow/pkg/sqlmodel
    67  func BenchmarkGenUpdate(b *testing.B) {
    68  	t := &testing.T{}
    69  	type genCase struct {
    70  		name    string
    71  		fn      genSQLFunc
    72  		prepare func(t *testing.T, batch int) []*RowChange
    73  	}
    74  	batchSizes := []int{
    75  		1, 2, 4, 8, 16, 32, 64, 128,
    76  	}
    77  	benchCases := []genCase{
    78  		{
    79  			name:    "OneColumnPK-GenUpdateSQL",
    80  			fn:      GenUpdateSQL,
    81  			prepare: prepareDataOneColoumnPK,
    82  		},
    83  		{
    84  			name:    "OneColumnPK-GenUpdateSQL",
    85  			fn:      GenUpdateSQL,
    86  			prepare: prepareDataOneColoumnPK,
    87  		},
    88  		{
    89  			name:    "MultiColumnsPK-GenUpdateSQL",
    90  			fn:      GenUpdateSQL,
    91  			prepare: prepareDataMultiColumnsPK,
    92  		},
    93  		{
    94  			name:    "MultiColumnsPK-GenUpdateSQL",
    95  			fn:      GenUpdateSQL,
    96  			prepare: prepareDataMultiColumnsPK,
    97  		},
    98  	}
    99  	for _, bc := range benchCases {
   100  		for _, batch := range batchSizes {
   101  			name := fmt.Sprintf("%s-Batch%d", bc.name, batch)
   102  			b.Run(name, func(b *testing.B) {
   103  				changes := prepareDataOneColoumnPK(t, batch)
   104  				for i := 0; i < b.N; i++ {
   105  					bc.fn(changes...)
   106  				}
   107  			})
   108  		}
   109  	}
   110  }