github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/spanz/map_bench_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 spanz
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/pingcap/tiflow/cdc/processor/tablepb"
    20  )
    21  
    22  func BenchmarkMap(b *testing.B) {
    23  	bm := NewBtreeMap[int]()
    24  	hm := NewHashMap[int]()
    25  	sm := SyncMap{}
    26  
    27  	spans := [100]tablepb.Span{}
    28  	for i := 0; i < len(spans); i++ {
    29  		spans[i] = TableIDToComparableSpan(int64(i))
    30  	}
    31  	for i, span := range spans {
    32  		bm.ReplaceOrInsert(span, i)
    33  		hm.ReplaceOrInsert(span, i)
    34  		sm.Store(span, i)
    35  	}
    36  
    37  	bench := func(name string, benchBm, benchHm, benchSm func(i int)) {
    38  		b.Run(name, func(b *testing.B) {
    39  			if benchBm != nil {
    40  				b.ResetTimer()
    41  				b.Run("BtreeMap", func(b *testing.B) {
    42  					for i := 0; i < b.N; i++ {
    43  						benchBm(i)
    44  					}
    45  				})
    46  				b.StopTimer()
    47  			}
    48  			if benchHm != nil {
    49  				b.ResetTimer()
    50  				b.Run("HashMap", func(b *testing.B) {
    51  					for i := 0; i < b.N; i++ {
    52  						benchHm(i)
    53  					}
    54  				})
    55  				b.StopTimer()
    56  			}
    57  			if benchSm != nil {
    58  				b.ResetTimer()
    59  				b.Run("SyncMap", func(b *testing.B) {
    60  					for i := 0; i < b.N; i++ {
    61  						benchSm(i)
    62  					}
    63  				})
    64  				b.StopTimer()
    65  			}
    66  		})
    67  	}
    68  
    69  	bench("Get", func(i int) {
    70  		bm.Get(spans[i%100])
    71  	}, func(i int) {
    72  		hm.Get(spans[i%100])
    73  	}, func(i int) {
    74  		sm.Load(spans[i%100])
    75  	})
    76  
    77  	bench("Store", func(i int) {
    78  		bm.ReplaceOrInsert(spans[i%100], i)
    79  	}, func(i int) {
    80  		hm.ReplaceOrInsert(spans[i%100], i)
    81  	}, func(i int) {
    82  		sm.Store(spans[i%100], i)
    83  	})
    84  
    85  	bench("Delete+Store", func(i int) {
    86  		bm.Delete(spans[i%100])
    87  		bm.ReplaceOrInsert(spans[i%100], i)
    88  	}, func(i int) {
    89  		hm.Delete(spans[i%100])
    90  		hm.ReplaceOrInsert(spans[i%100], i)
    91  	}, func(i int) {
    92  		sm.Delete(spans[i%100])
    93  		sm.Store(spans[i%100], i)
    94  	})
    95  
    96  	bench("Range", func(i int) {
    97  		bm.Ascend(func(span tablepb.Span, value int) bool {
    98  			return span.TableID > -1
    99  		})
   100  	}, func(i int) {
   101  		hm.Range(func(span tablepb.Span, value int) bool {
   102  			return span.TableID > -1
   103  		})
   104  	}, func(i int) {
   105  		sm.Range(func(span tablepb.Span, value any) bool {
   106  			return span.TableID > -1
   107  		})
   108  	})
   109  
   110  	start, end := spans[0], TableIDToComparableSpan(int64(len(spans)))
   111  	bench("AscendRange", func(i int) {
   112  		bm.AscendRange(start, end, func(span tablepb.Span, value int) bool {
   113  			return span.TableID > -1
   114  		})
   115  	}, nil, nil)
   116  }