|
| 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 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package statistics_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "testing" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/pingcap/tidb/parser/mysql" |
| 23 | + "github.com/pingcap/tidb/sessionctx/stmtctx" |
| 24 | + "github.com/pingcap/tidb/statistics" |
| 25 | + "github.com/pingcap/tidb/statistics/handle" |
| 26 | + "github.com/pingcap/tidb/types" |
| 27 | + "github.com/pingcap/tidb/util/chunk" |
| 28 | + "github.com/pingcap/tidb/util/codec" |
| 29 | + "github.com/stretchr/testify/require" |
| 30 | +) |
| 31 | + |
| 32 | +// cmd: go test -run=^$ -bench=BenchmarkMergePartTopN2GlobalTopNWithHists -benchmem github.com/pingcap/tidb/statistics |
| 33 | +func benchmarkMergePartTopN2GlobalTopNWithHists(partitions int, b *testing.B) { |
| 34 | + loc := time.UTC |
| 35 | + sc := &stmtctx.StatementContext{TimeZone: loc} |
| 36 | + version := 1 |
| 37 | + isKilled := uint32(0) |
| 38 | + |
| 39 | + // Prepare TopNs. |
| 40 | + topNs := make([]*statistics.TopN, 0, partitions) |
| 41 | + for i := 0; i < partitions; i++ { |
| 42 | + // Construct TopN, should be key1 -> 2, key2 -> 2, key3 -> 3. |
| 43 | + topN := statistics.NewTopN(3) |
| 44 | + { |
| 45 | + key1, err := codec.EncodeKey(sc, nil, types.NewIntDatum(1)) |
| 46 | + require.NoError(b, err) |
| 47 | + topN.AppendTopN(key1, 2) |
| 48 | + key2, err := codec.EncodeKey(sc, nil, types.NewIntDatum(2)) |
| 49 | + require.NoError(b, err) |
| 50 | + topN.AppendTopN(key2, 2) |
| 51 | + if i%2 == 0 { |
| 52 | + key3, err := codec.EncodeKey(sc, nil, types.NewIntDatum(3)) |
| 53 | + require.NoError(b, err) |
| 54 | + topN.AppendTopN(key3, 3) |
| 55 | + } |
| 56 | + } |
| 57 | + topNs = append(topNs, topN) |
| 58 | + } |
| 59 | + |
| 60 | + // Prepare Hists. |
| 61 | + hists := make([]*statistics.Histogram, 0, partitions) |
| 62 | + for i := 0; i < partitions; i++ { |
| 63 | + // Construct Hist |
| 64 | + h := statistics.NewHistogram(1, 10, 0, 0, types.NewFieldType(mysql.TypeTiny), chunk.InitialCapacity, 0) |
| 65 | + h.Bounds.AppendInt64(0, 1) |
| 66 | + h.Buckets = append(h.Buckets, statistics.Bucket{Repeat: 10, Count: 20}) |
| 67 | + h.Bounds.AppendInt64(0, 2) |
| 68 | + h.Buckets = append(h.Buckets, statistics.Bucket{Repeat: 10, Count: 30}) |
| 69 | + h.Bounds.AppendInt64(0, 3) |
| 70 | + h.Buckets = append(h.Buckets, statistics.Bucket{Repeat: 10, Count: 30}) |
| 71 | + h.Bounds.AppendInt64(0, 4) |
| 72 | + h.Buckets = append(h.Buckets, statistics.Bucket{Repeat: 10, Count: 40}) |
| 73 | + hists = append(hists, h) |
| 74 | + } |
| 75 | + |
| 76 | + b.ResetTimer() |
| 77 | + for i := 0; i < b.N; i++ { |
| 78 | + // Benchmark merge 10 topN. |
| 79 | + _, _, _, _ = statistics.MergePartTopN2GlobalTopN(loc, version, topNs, 10, hists, false, &isKilled) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +// cmd: go test -run=^$ -bench=BenchmarkMergeGlobalStatsTopNByConcurrencyWithHists -benchmem github.com/pingcap/tidb/statistics |
| 84 | +func benchmarkMergeGlobalStatsTopNByConcurrencyWithHists(partitions int, b *testing.B) { |
| 85 | + loc := time.UTC |
| 86 | + sc := &stmtctx.StatementContext{TimeZone: loc} |
| 87 | + version := 1 |
| 88 | + isKilled := uint32(0) |
| 89 | + |
| 90 | + // Prepare TopNs. |
| 91 | + topNs := make([]*statistics.TopN, 0, partitions) |
| 92 | + for i := 0; i < partitions; i++ { |
| 93 | + // Construct TopN, should be key1 -> 2, key2 -> 2, key3 -> 3. |
| 94 | + topN := statistics.NewTopN(3) |
| 95 | + { |
| 96 | + key1, err := codec.EncodeKey(sc, nil, types.NewIntDatum(1)) |
| 97 | + require.NoError(b, err) |
| 98 | + topN.AppendTopN(key1, 2) |
| 99 | + key2, err := codec.EncodeKey(sc, nil, types.NewIntDatum(2)) |
| 100 | + require.NoError(b, err) |
| 101 | + topN.AppendTopN(key2, 2) |
| 102 | + if i%2 == 0 { |
| 103 | + key3, err := codec.EncodeKey(sc, nil, types.NewIntDatum(3)) |
| 104 | + require.NoError(b, err) |
| 105 | + topN.AppendTopN(key3, 3) |
| 106 | + } |
| 107 | + } |
| 108 | + topNs = append(topNs, topN) |
| 109 | + } |
| 110 | + |
| 111 | + // Prepare Hists. |
| 112 | + hists := make([]*statistics.Histogram, 0, partitions) |
| 113 | + for i := 0; i < partitions; i++ { |
| 114 | + // Construct Hist |
| 115 | + h := statistics.NewHistogram(1, 10, 0, 0, types.NewFieldType(mysql.TypeTiny), chunk.InitialCapacity, 0) |
| 116 | + h.Bounds.AppendInt64(0, 1) |
| 117 | + h.Buckets = append(h.Buckets, statistics.Bucket{Repeat: 10, Count: 20}) |
| 118 | + h.Bounds.AppendInt64(0, 2) |
| 119 | + h.Buckets = append(h.Buckets, statistics.Bucket{Repeat: 10, Count: 30}) |
| 120 | + h.Bounds.AppendInt64(0, 3) |
| 121 | + h.Buckets = append(h.Buckets, statistics.Bucket{Repeat: 10, Count: 30}) |
| 122 | + h.Bounds.AppendInt64(0, 4) |
| 123 | + h.Buckets = append(h.Buckets, statistics.Bucket{Repeat: 10, Count: 40}) |
| 124 | + hists = append(hists, h) |
| 125 | + } |
| 126 | + wrapper := &statistics.StatsWrapper{ |
| 127 | + AllTopN: topNs, |
| 128 | + AllHg: hists, |
| 129 | + } |
| 130 | + const mergeConcurrency = 4 |
| 131 | + batchSize := len(wrapper.AllTopN) / mergeConcurrency |
| 132 | + if batchSize < 1 { |
| 133 | + batchSize = 1 |
| 134 | + } else if batchSize > handle.MaxPartitionMergeBatchSize { |
| 135 | + batchSize = handle.MaxPartitionMergeBatchSize |
| 136 | + } |
| 137 | + b.ResetTimer() |
| 138 | + for i := 0; i < b.N; i++ { |
| 139 | + // Benchmark merge 10 topN. |
| 140 | + _, _, _, _ = handle.MergeGlobalStatsTopNByConcurrency(mergeConcurrency, batchSize, wrapper, loc, version, 10, false, &isKilled) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +var benchmarkSizes = []int{100, 1000, 10000, 100000, 1000000, 10000000} |
| 145 | +var benchmarkConcurrencySizes = []int{100, 1000, 10000, 100000, 1000000, 10000000, 100000000} |
| 146 | + |
| 147 | +func BenchmarkMergePartTopN2GlobalTopNWithHists(b *testing.B) { |
| 148 | + for _, size := range benchmarkSizes { |
| 149 | + b.Run(fmt.Sprintf("Size%d", size), func(b *testing.B) { |
| 150 | + benchmarkMergePartTopN2GlobalTopNWithHists(size, b) |
| 151 | + }) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func BenchmarkMergeGlobalStatsTopNByConcurrencyWithHists(b *testing.B) { |
| 156 | + for _, size := range benchmarkConcurrencySizes { |
| 157 | + b.Run(fmt.Sprintf("Size%d", size), func(b *testing.B) { |
| 158 | + benchmarkMergeGlobalStatsTopNByConcurrencyWithHists(size, b) |
| 159 | + }) |
| 160 | + } |
| 161 | +} |
0 commit comments