From d4ac98334788f94eefc18818141b29731ce5b4c7 Mon Sep 17 00:00:00 2001 From: qupeng Date: Tue, 12 Sep 2023 16:02:17 +0800 Subject: [PATCH 1/4] init Signed-off-by: qupeng --- cdc/puller/frontier/frontier_bench_test.go | 54 ++++++++++++---------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/cdc/puller/frontier/frontier_bench_test.go b/cdc/puller/frontier/frontier_bench_test.go index 50e6c30f60b..b96d787dd1f 100644 --- a/cdc/puller/frontier/frontier_bench_test.go +++ b/cdc/puller/frontier/frontier_bench_test.go @@ -15,46 +15,52 @@ package frontier import ( "fmt" + "math/rand" "testing" + "time" "github.com/pingcap/tiflow/cdc/processor/tablepb" + "github.com/pingcap/tiflow/pkg/spanz" ) func toCMPBytes(i int) []byte { - s := fmt.Sprintf("%09d", i) + s := fmt.Sprintf("t_XXXXXXXX_r_%09d", i) return []byte(s) } -func BenchmarkSpanFrontier(b *testing.B) { +func BenchmarkSpanForwardAndFrontier(b *testing.B) { tests := []struct { - name string - n int + regions int + rounds int }{ - {name: "5k", n: 5000}, - {name: "10k", n: 10_000}, - {name: "50k", n: 50_000}, - {name: "100k", n: 100_000}, + {900_000, 10}, + {400_000, 10}, } for _, test := range tests { - n := test.n + regions := test.regions + rounds := test.rounds - b.Run(test.name, func(b *testing.B) { - spans := make([]tablepb.Span, 0, n) - for i := 0; i < n; i++ { - span := tablepb.Span{ - StartKey: toCMPBytes(i), - EndKey: toCMPBytes(i + 1), + spans := make([]tablepb.Span, 0, regions) + for i := 0; i < regions; i++ { + span := tablepb.Span{StartKey: toCMPBytes(i), EndKey: toCMPBytes(i + 1)} + spans = append(spans, span) + } + totalSpan := tablepb.Span{StartKey: spans[0].StartKey, EndKey: spans[len(spans)-1].EndKey} + f := NewFrontier(0, totalSpan) + + r := rand.New(rand.NewSource(time.Now().Unix())) + b.ResetTimer() + b.Run(fmt.Sprintf("%d(region)-%d(round)", regions, rounds), func(b *testing.B) { + for i := 0; i < regions*rounds; i++ { + offset := r.Uint64() % uint64(regions) + span := spans[offset] + if spanz.IsSubSpan(span, totalSpan) { + f.Forward(offset, span, offset) + if i%regions == 0 { + f.Frontier() + } } - spans = append(spans, span) - } - - f := NewFrontier(0, spans...) - - b.ResetTimer() - - for i := 0; i < b.N; i++ { - f.Forward(0, spans[i%n], uint64(i)) } }) } From 3d227623310a95d621f76e565a9710e69406c0e4 Mon Sep 17 00:00:00 2001 From: qupeng Date: Tue, 12 Sep 2023 16:27:39 +0800 Subject: [PATCH 2/4] fix Signed-off-by: qupeng --- cdc/puller/frontier/frontier_bench_test.go | 25 +++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/cdc/puller/frontier/frontier_bench_test.go b/cdc/puller/frontier/frontier_bench_test.go index b96d787dd1f..e798ae6930f 100644 --- a/cdc/puller/frontier/frontier_bench_test.go +++ b/cdc/puller/frontier/frontier_bench_test.go @@ -30,14 +30,18 @@ func toCMPBytes(i int) []byte { func BenchmarkSpanForwardAndFrontier(b *testing.B) { tests := []struct { + order string regions int rounds int }{ - {900_000, 10}, - {400_000, 10}, + {"random", 900_000, 10}, + {"random", 400_000, 10}, + {"ordered", 900_000, 10}, + {"ordered", 400_000, 10}, } for _, test := range tests { + order := test.order regions := test.regions rounds := test.rounds @@ -49,11 +53,22 @@ func BenchmarkSpanForwardAndFrontier(b *testing.B) { totalSpan := tablepb.Span{StartKey: spans[0].StartKey, EndKey: spans[len(spans)-1].EndKey} f := NewFrontier(0, totalSpan) - r := rand.New(rand.NewSource(time.Now().Unix())) + offsets := make([]uint64, 0, regions*rounds) + if order == "random" { + r := rand.New(rand.NewSource(time.Now().Unix())) + for i := 0; i < regions*rounds; i++ { + offsets = append(offsets, r.Uint64()%uint64(regions)) + } + } else { + for i := 0; i < regions*rounds; i++ { + offsets = append(offsets, uint64(i)%uint64(regions)) + } + } + b.ResetTimer() - b.Run(fmt.Sprintf("%d(region)-%d(round)", regions, rounds), func(b *testing.B) { + b.Run(fmt.Sprintf("%s-%d(region)-%d(round)", order, regions, rounds), func(b *testing.B) { for i := 0; i < regions*rounds; i++ { - offset := r.Uint64() % uint64(regions) + offset := offsets[i] span := spans[offset] if spanz.IsSubSpan(span, totalSpan) { f.Forward(offset, span, offset) From 519a4cc57f9836b3f763b813806fec6d07706fe8 Mon Sep 17 00:00:00 2001 From: qupeng Date: Tue, 12 Sep 2023 17:51:08 +0800 Subject: [PATCH 3/4] fix Signed-off-by: qupeng --- cdc/puller/frontier/frontier_bench_test.go | 54 +++++++++++----------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/cdc/puller/frontier/frontier_bench_test.go b/cdc/puller/frontier/frontier_bench_test.go index e798ae6930f..260fde09976 100644 --- a/cdc/puller/frontier/frontier_bench_test.go +++ b/cdc/puller/frontier/frontier_bench_test.go @@ -28,23 +28,25 @@ func toCMPBytes(i int) []byte { return []byte(s) } -func BenchmarkSpanForwardAndFrontier(b *testing.B) { - tests := []struct { - order string - regions int - rounds int - }{ - {"random", 900_000, 10}, - {"random", 400_000, 10}, - {"ordered", 900_000, 10}, - {"ordered", 400_000, 10}, - } +func BenchmarkSpanForwardAndFrontier_random_900k_10(b *testing.B) { + benchmarkSpanForwardAndFrontier(b, "random", 900_000, 10) +} - for _, test := range tests { - order := test.order - regions := test.regions - rounds := test.rounds +func BenchmarkSpanForwardAndFrontier_ordered_900k_10(b *testing.B) { + benchmarkSpanForwardAndFrontier(b, "ordered", 900_000, 10) +} +func BenchmarkSpanForwardAndFrontier_random_400k_10(b *testing.B) { + benchmarkSpanForwardAndFrontier(b, "random", 400_000, 10) +} + +func BenchmarkSpanForwardAndFrontier_ordered_400k_10(b *testing.B) { + benchmarkSpanForwardAndFrontier(b, "ordered", 400_000, 10) +} + +func benchmarkSpanForwardAndFrontier(b *testing.B, order string, regions, rounds int) { + for i := 0; i < b.N; i++ { + b.StopTimer() spans := make([]tablepb.Span, 0, regions) for i := 0; i < regions; i++ { span := tablepb.Span{StartKey: toCMPBytes(i), EndKey: toCMPBytes(i + 1)} @@ -65,19 +67,19 @@ func BenchmarkSpanForwardAndFrontier(b *testing.B) { } } - b.ResetTimer() - b.Run(fmt.Sprintf("%s-%d(region)-%d(round)", order, regions, rounds), func(b *testing.B) { - for i := 0; i < regions*rounds; i++ { - offset := offsets[i] - span := spans[offset] - if spanz.IsSubSpan(span, totalSpan) { - f.Forward(offset, span, offset) - if i%regions == 0 { - f.Frontier() - } + b.StartTimer() + start := time.Now() + for i := 0; i < regions*rounds; i++ { + offset := offsets[i] + span := spans[offset] + if spanz.IsSubSpan(span, totalSpan) { + f.Forward(offset, span, offset) + if i%regions == 0 { + f.Frontier() } } - }) + } + b.Logf("finishes a round, time in ms: %d", time.Since(start).Milliseconds()) } } From 719751edefec2197d7d6535acdd578d26059f508 Mon Sep 17 00:00:00 2001 From: qupeng Date: Wed, 13 Sep 2023 13:17:08 +0800 Subject: [PATCH 4/4] fix Signed-off-by: qupeng --- cdc/puller/frontier/frontier_bench_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdc/puller/frontier/frontier_bench_test.go b/cdc/puller/frontier/frontier_bench_test.go index 260fde09976..d2a8b3422cc 100644 --- a/cdc/puller/frontier/frontier_bench_test.go +++ b/cdc/puller/frontier/frontier_bench_test.go @@ -73,7 +73,7 @@ func benchmarkSpanForwardAndFrontier(b *testing.B, order string, regions, rounds offset := offsets[i] span := spans[offset] if spanz.IsSubSpan(span, totalSpan) { - f.Forward(offset, span, offset) + f.Forward(offset, span, uint64(1)) if i%regions == 0 { f.Frontier() }