github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/puller/frontier/frontier_bench_test.go (about)

     1  // Copyright 2020 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 frontier
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  
    20  	"github.com/pingcap/tiflow/cdc/processor/tablepb"
    21  )
    22  
    23  func toCMPBytes(i int) []byte {
    24  	s := fmt.Sprintf("%09d", i)
    25  	return []byte(s)
    26  }
    27  
    28  func BenchmarkSpanFrontier(b *testing.B) {
    29  	tests := []struct {
    30  		name string
    31  		n    int
    32  	}{
    33  		{name: "5k", n: 5000},
    34  		{name: "10k", n: 10_000},
    35  		{name: "50k", n: 50_000},
    36  		{name: "100k", n: 100_000},
    37  	}
    38  
    39  	for _, test := range tests {
    40  		n := test.n
    41  
    42  		b.Run(test.name, func(b *testing.B) {
    43  			spans := make([]tablepb.Span, 0, n)
    44  			for i := 0; i < n; i++ {
    45  				span := tablepb.Span{
    46  					StartKey: toCMPBytes(i),
    47  					EndKey:   toCMPBytes(i + 1),
    48  				}
    49  				spans = append(spans, span)
    50  			}
    51  
    52  			f := NewFrontier(0, spans...)
    53  
    54  			b.ResetTimer()
    55  
    56  			for i := 0; i < b.N; i++ {
    57  				f.Forward(0, spans[i%n], uint64(i))
    58  			}
    59  		})
    60  	}
    61  }
    62  
    63  func BenchmarkSpanFrontierOverlap(b *testing.B) {
    64  	tests := []struct {
    65  		name string
    66  		n    int
    67  	}{
    68  		{name: "5k", n: 5000},
    69  		{name: "10k", n: 10_000},
    70  		{name: "50k", n: 50_000},
    71  		{name: "100k", n: 100_000},
    72  	}
    73  
    74  	steps := []int{5, 10, 100, 500}
    75  
    76  	for _, test := range tests {
    77  		n := test.n
    78  
    79  		for _, step := range steps {
    80  			b.Run(fmt.Sprintf("%s_%d", test.name, step), func(b *testing.B) {
    81  				spans := make([]tablepb.Span, 0, n)
    82  				forward := make([]tablepb.Span, 0, n)
    83  				for i := 0; i < n; i++ {
    84  					spans = append(spans, tablepb.Span{
    85  						StartKey: toCMPBytes(i),
    86  						EndKey:   toCMPBytes(i + 1),
    87  					})
    88  					forward = append(forward, tablepb.Span{
    89  						StartKey: toCMPBytes(i),
    90  						EndKey:   toCMPBytes(i + step),
    91  					})
    92  				}
    93  
    94  				f := NewFrontier(0, spans...)
    95  
    96  				b.ResetTimer()
    97  
    98  				for i := 0; i < b.N; i++ {
    99  					f.Forward(0, forward[i%n], uint64(i))
   100  				}
   101  			})
   102  		}
   103  	}
   104  }