Skip to content

Commit

Permalink
statistics: add historgram bench for MergePartitionHist2GlobalHist (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Sep 18, 2023
1 parent 8e47816 commit d7f8134
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions statistics/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ go_test(
"cmsketch_test.go",
"feedback_test.go",
"fmsketch_test.go",
"histogram_bench_test.go",
"histogram_test.go",
"integration_test.go",
"main_test.go",
Expand Down
107 changes: 107 additions & 0 deletions statistics/histogram_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright 2023 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package statistics

import (
"fmt"
"math/rand"
"testing"

"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/mock"
"github.com/stretchr/testify/require"
)

const (
histogramLen = 100
popedTopNLen = 100
expBucketNumber = 100
)

func genBucket4TestData(length int) []*bucket4Test {
result := make([]*bucket4Test, 0, length)
var lower, upper int64
for n := 0; n < length; n++ {
if n == 0 {
lower = 0
} else {
lower = upper + 1
}
if n == length-1 {
upper = 10000
} else {
upper = lower + (rand.Int63n(int64(100*(n+1)) - lower))
}
result = append(result, &bucket4Test{
lower: lower,
upper: upper,
count: rand.Int63n(10000),
repeat: rand.Int63n(100),
ndv: rand.Int63n(100),
})
}
return result
}

func genHist4Bench(t *testing.B, buckets []*bucket4Test, totColSize int64) *Histogram {
h := NewHistogram(0, 0, 0, 0, types.NewFieldType(mysql.TypeBlob), len(buckets), totColSize)
for _, bucket := range buckets {
lower, err := codec.EncodeKey(nil, nil, types.NewIntDatum(bucket.lower))
require.NoError(t, err)
upper, err := codec.EncodeKey(nil, nil, types.NewIntDatum(bucket.upper))
require.NoError(t, err)
di, du := types.NewBytesDatum(lower), types.NewBytesDatum(upper)
h.AppendBucketWithNDV(&di, &du, bucket.count, bucket.repeat, bucket.ndv)
}
return h
}

func benchmarkMergePartitionHist2GlobalHist(b *testing.B, partition int) {
ctx := mock.NewContext()
sc := ctx.GetSessionVars().StmtCtx
hists := make([]*Histogram, 0, partition)
for i := 0; i < partition; i++ {
buckets := genBucket4TestData(histogramLen)
hist := genHist4Bench(b, buckets, histogramLen)
hists = append(hists, hist)
}
poped := make([]TopNMeta, 0, popedTopNLen)
for n := 0; n < popedTopNLen; n++ {
b, _ := codec.EncodeKey(sc, nil, types.NewIntDatum(rand.Int63n(10000)))
tmp := TopNMeta{
Encoded: b,
Count: uint64(rand.Int63n(10000)),
}
poped = append(poped, tmp)
}
b.StartTimer()
MergePartitionHist2GlobalHist(sc, hists, poped, expBucketNumber, true)
b.StopTimer()
}

var benchmarkPartitionSize = []int{1000, 10000, 100000}

// cmd: go test -run=^$ -bench=BenchmarkMergePartitionHist2GlobalHist -benchmem github.com/pingcap/tidb/statistics
func BenchmarkMergePartitionHist2GlobalHist(b *testing.B) {
for _, size := range benchmarkPartitionSize {
b.Run(fmt.Sprintf("Size%d", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
benchmarkMergePartitionHist2GlobalHist(b, size)
}
})
}
}

0 comments on commit d7f8134

Please sign in to comment.