Skip to content

Commit 1b4383a

Browse files
authored
statistics: refactor test package (#47608)
ref #44940
1 parent 9504d83 commit 1b4383a

File tree

7 files changed

+57
-30
lines changed

7 files changed

+57
-30
lines changed

pkg/statistics/handle/globalstats/BUILD.bazel

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,28 @@ go_library(
3434
go_test(
3535
name = "globalstats_test",
3636
timeout = "short",
37-
srcs = ["topn_bench_test.go"],
38-
embed = [":globalstats"],
37+
srcs = [
38+
"globalstats_test.go",
39+
"main_test.go",
40+
"topn_bench_test.go",
41+
],
3942
flaky = True,
43+
shard_count = 14,
4044
deps = [
45+
":globalstats",
46+
"//pkg/config",
47+
"//pkg/parser/model",
4148
"//pkg/parser/mysql",
4249
"//pkg/sessionctx/stmtctx",
4350
"//pkg/statistics",
51+
"//pkg/testkit",
52+
"//pkg/testkit/testsetup",
4453
"//pkg/types",
4554
"//pkg/util/chunk",
4655
"//pkg/util/codec",
56+
"@com_github_pingcap_failpoint//:failpoint",
4757
"@com_github_stretchr_testify//require",
4858
"@com_github_tiancaiamao_gp//:gp",
59+
"@org_uber_go_goleak//:goleak",
4960
],
5061
)

pkg/statistics/handle/handletest/globalstats/globalstats_test.go renamed to pkg/statistics/handle/globalstats/globalstats_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package globalstats
15+
package globalstats_test
1616

1717
import (
1818
"fmt"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 globalstats_test
16+
17+
import (
18+
"testing"
19+
20+
"github.com/pingcap/tidb/pkg/testkit/testsetup"
21+
"go.uber.org/goleak"
22+
)
23+
24+
func TestMain(m *testing.M) {
25+
opts := []goleak.Option{
26+
goleak.IgnoreTopFunction("github.com/golang/glog.(*fileSink).flushDaemon"),
27+
goleak.IgnoreTopFunction("github.com/lestrrat-go/httprc.runFetchWorker"),
28+
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"),
29+
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
30+
}
31+
testsetup.SetupForCommonTest()
32+
goleak.VerifyTestMain(m, opts...)
33+
}

pkg/statistics/handle/globalstats/topn_bench_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package globalstats
15+
package globalstats_test
1616

1717
import (
1818
"fmt"
@@ -22,6 +22,7 @@ import (
2222
"github.com/pingcap/tidb/pkg/parser/mysql"
2323
"github.com/pingcap/tidb/pkg/sessionctx/stmtctx"
2424
"github.com/pingcap/tidb/pkg/statistics"
25+
"github.com/pingcap/tidb/pkg/statistics/handle/globalstats"
2526
"github.com/pingcap/tidb/pkg/types"
2627
"github.com/pingcap/tidb/pkg/util/chunk"
2728
"github.com/pingcap/tidb/pkg/util/codec"
@@ -123,20 +124,20 @@ func benchmarkMergeGlobalStatsTopNByConcurrencyWithHists(partitions int, b *test
123124
h.Buckets = append(h.Buckets, statistics.Bucket{Repeat: 10, Count: 40})
124125
hists = append(hists, h)
125126
}
126-
wrapper := NewStatsWrapper(hists, topNs)
127+
wrapper := globalstats.NewStatsWrapper(hists, topNs)
127128
const mergeConcurrency = 4
128129
batchSize := len(wrapper.AllTopN) / mergeConcurrency
129130
if batchSize < 1 {
130131
batchSize = 1
131-
} else if batchSize > MaxPartitionMergeBatchSize {
132-
batchSize = MaxPartitionMergeBatchSize
132+
} else if batchSize > globalstats.MaxPartitionMergeBatchSize {
133+
batchSize = globalstats.MaxPartitionMergeBatchSize
133134
}
134135
gpool := gp.New(mergeConcurrency, 5*time.Minute)
135136
defer gpool.Close()
136137
b.ResetTimer()
137138
for i := 0; i < b.N; i++ {
138139
// Benchmark merge 10 topN.
139-
_, _, _, _ = MergeGlobalStatsTopNByConcurrency(gpool, mergeConcurrency, batchSize, wrapper, loc, version, 10, false, &isKilled)
140+
_, _, _, _ = globalstats.MergeGlobalStatsTopNByConcurrency(gpool, mergeConcurrency, batchSize, wrapper, loc, version, 10, false, &isKilled)
140141
}
141142
}
142143

pkg/statistics/handle/handletest/globalstats/BUILD.bazel

Lines changed: 0 additions & 21 deletions
This file was deleted.

pkg/statistics/handle/lockstats/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ go_test(
2525
timeout = "short",
2626
srcs = [
2727
"lock_stats_test.go",
28+
"main_test.go",
2829
"query_lock_test.go",
2930
"unlock_stats_test.go",
3031
],
@@ -36,13 +37,15 @@ go_test(
3637
"//pkg/parser/mysql",
3738
"//pkg/sessionctx",
3839
"//pkg/statistics/handle/util",
40+
"//pkg/testkit/testsetup",
3941
"//pkg/types",
4042
"//pkg/util/chunk",
4143
"//pkg/util/mock",
4244
"//pkg/util/sqlexec/mock",
4345
"@com_github_pingcap_errors//:errors",
4446
"@com_github_stretchr_testify//require",
4547
"@com_github_tikv_client_go_v2//util",
48+
"@org_uber_go_goleak//:goleak",
4649
"@org_uber_go_mock//gomock",
4750
],
4851
)

pkg/statistics/handle/handletest/globalstats/main_test.go renamed to pkg/statistics/handle/lockstats/main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package globalstats
15+
package lockstats
1616

1717
import (
1818
"testing"

0 commit comments

Comments
 (0)