github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/scheduler/schedulepb/table_schedule_manual_test.go (about)

     1  // Copyright 2022 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 schedulepb
    15  
    16  import (
    17  	"fmt"
    18  	"testing"
    19  
    20  	"github.com/gogo/protobuf/proto"
    21  	"github.com/pingcap/tiflow/cdc/model"
    22  	"github.com/pingcap/tiflow/cdc/processor/tablepb"
    23  )
    24  
    25  func benchmarkMessageHeartbeatResponse(
    26  	b *testing.B, bench func(b *testing.B, m *Message, total int),
    27  ) {
    28  	size := 16384
    29  	for total := 1; total <= size; total *= 2 {
    30  		msg := Message{
    31  			MsgType: MsgHeartbeatResponse,
    32  			HeartbeatResponse: &HeartbeatResponse{
    33  				Tables:   make([]tablepb.TableStatus, 0, total),
    34  				Liveness: model.LivenessCaptureAlive,
    35  			},
    36  		}
    37  
    38  		for i := 0; i < total; i++ {
    39  			msg.HeartbeatResponse.Tables = append(msg.HeartbeatResponse.Tables,
    40  				tablepb.TableStatus{
    41  					TableID: model.TableID(10000 + i),
    42  					State:   tablepb.TableStateReplicating,
    43  					Checkpoint: tablepb.Checkpoint{
    44  						CheckpointTs: 433331421532337260,
    45  						ResolvedTs:   433331421532337261,
    46  					},
    47  				})
    48  		}
    49  
    50  		b.ResetTimer()
    51  		bench(b, &msg, total)
    52  		b.StopTimer()
    53  	}
    54  }
    55  
    56  func BenchmarkMessageHeartbeatResponseProtoMarshal(b *testing.B) {
    57  	benchmarkMessageHeartbeatResponse(b, func(b *testing.B, m *Message, total int) {
    58  		b.Run(fmt.Sprintf("%d checkpoints(s) marshal", total), func(b *testing.B) {
    59  			totalLen := 0
    60  			for i := 0; i < b.N; i++ {
    61  				bytes, err := proto.Marshal(m)
    62  				if err != nil {
    63  					panic(err)
    64  				}
    65  				totalLen += len(bytes)
    66  			}
    67  			b.SetBytes(int64(totalLen / b.N))
    68  		})
    69  	})
    70  }
    71  
    72  func BenchmarkMessageHeartbeatResponseProtoUnmarshal(b *testing.B) {
    73  	benchmarkMessageHeartbeatResponse(b, func(b *testing.B, m *Message, total int) {
    74  		b.Run(fmt.Sprintf("%d checkpoints(s) marshal", total), func(b *testing.B) {
    75  			bytes, err := proto.Marshal(m)
    76  			if err != nil {
    77  				panic(err)
    78  			}
    79  
    80  			dst := Message{}
    81  			totalLen := 0
    82  			for i := 0; i < b.N; i++ {
    83  				if err := proto.Unmarshal(bytes, &dst); err != nil {
    84  					panic(err)
    85  				}
    86  				totalLen += len(bytes)
    87  			}
    88  			b.SetBytes(int64(totalLen / b.N))
    89  		})
    90  	})
    91  }
    92  
    93  func BenchmarkMessageHeartbeatResponseProtoSize(b *testing.B) {
    94  	benchmarkMessageHeartbeatResponse(b, func(b *testing.B, m *Message, total int) {
    95  		b.Run(fmt.Sprintf("%d checkpoint(s) size", total), func(b *testing.B) {
    96  			for i := 0; i < b.N; i++ {
    97  				m.Size()
    98  			}
    99  		})
   100  	})
   101  }