Skip to content

Commit

Permalink
statistics: improve test to make test more fast (pingcap#41403)
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkingrei authored and ghazalfamilyusa committed Feb 15, 2023
1 parent ad3c1b9 commit 2067b5d
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 80 deletions.
3 changes: 2 additions & 1 deletion ddl/failtest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
name = "failtest_test",
timeout = "moderate",
timeout = "short",
srcs = [
"fail_db_test.go",
"main_test.go",
],
flaky = True,
shard_count = 14,
deps = [
"//config",
"//ddl",
Expand Down
1 change: 1 addition & 0 deletions ddl/failtest/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestMain(m *testing.M) {
goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"),
goleak.IgnoreTopFunction("github.com/lestrrat-go/httprc.runFetchWorker"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"),
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
}

goleak.VerifyTestMain(m, opts...)
Expand Down
4 changes: 2 additions & 2 deletions ddl/indexmergetest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
name = "indexmergetest_test",
timeout = "moderate",
timeout = "short",
srcs = [
"main_test.go",
"merge_test.go",
],
flaky = True,
race = "on",
shard_count = 4,
shard_count = 18,
deps = [
"//config",
"//ddl",
Expand Down
3 changes: 2 additions & 1 deletion sessiontxn/isolation/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ go_library(

go_test(
name = "isolation_test",
timeout = "moderate",
timeout = "short",
srcs = [
"main_test.go",
"optimistic_test.go",
Expand All @@ -47,6 +47,7 @@ go_test(
"serializable_test.go",
],
flaky = True,
shard_count = 28,
deps = [
":isolation",
"//config",
Expand Down
13 changes: 2 additions & 11 deletions statistics/handle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,15 @@ go_library(

go_test(
name = "handle_test",
timeout = "moderate",
timeout = "short",
srcs = [
"ddl_test.go",
"dump_test.go",
"gc_test.go",
"handle_hist_test.go",
"handle_test.go",
"lru_cache_test.go",
"main_test.go",
"update_list_test.go",
"update_test.go",
],
embed = [":handle"],
flaky = True,
Expand All @@ -78,28 +76,21 @@ go_test(
deps = [
"//config",
"//domain",
"//metrics",
"//parser/model",
"//parser/mysql",
"//session",
"//sessionctx/stmtctx",
"//sessionctx/variable",
"//statistics",
"//statistics/handle/internal",
"//testkit",
"//testkit/testsetup",
"//types",
"//util",
"//util/codec",
"//util/collate",
"//util/israce",
"//util/mathutil",
"//util/mock",
"//util/ranger",
"//util/sqlexec",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_prometheus_client_model//go",
"@com_github_stretchr_testify//require",
"@com_github_tikv_client_go_v2//oracle",
"@org_uber_go_goleak//:goleak",
],
)
3 changes: 2 additions & 1 deletion statistics/handle/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/statistics"
"github.com/pingcap/tidb/statistics/handle"
"github.com/pingcap/tidb/statistics/handle/internal"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/util"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -53,7 +54,7 @@ func requireTableEqual(t *testing.T, a *statistics.Table, b *statistics.Table) {
}
require.True(t, a.Indices[i].TopN.Equal(b.Indices[i].TopN))
}
require.True(t, isSameExtendedStats(a.ExtendedStats, b.ExtendedStats))
require.True(t, internal.IsSameExtendedStats(a.ExtendedStats, b.ExtendedStats))
}

func cleanStats(tk *testkit.TestKit, do *domain.Domain) {
Expand Down
33 changes: 33 additions & 0 deletions statistics/handle/handletest/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
name = "handletest_test",
timeout = "short",
srcs = [
"handle_test.go",
"main_test.go",
],
flaky = True,
shard_count = 50,
deps = [
"//config",
"//domain",
"//parser/model",
"//session",
"//sessionctx/variable",
"//statistics",
"//statistics/handle",
"//statistics/handle/internal",
"//testkit",
"//testkit/testsetup",
"//types",
"//util/collate",
"//util/israce",
"//util/mock",
"//util/ranger",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_stretchr_testify//require",
"@com_github_tikv_client_go_v2//oracle",
"@org_uber_go_goleak//:goleak",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package handle_test
package handletest

import (
"bytes"
Expand All @@ -32,6 +32,7 @@ import (
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/statistics"
"github.com/pingcap/tidb/statistics/handle"
"github.com/pingcap/tidb/statistics/handle/internal"
"github.com/pingcap/tidb/testkit"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/collate"
Expand Down Expand Up @@ -144,63 +145,6 @@ func TestStatsCacheMemTracker(t *testing.T) {
require.False(t, statsTbl.Pseudo)
}

func assertTableEqual(t *testing.T, a *statistics.Table, b *statistics.Table) {
require.Equal(t, b.Count, a.Count)
require.Equal(t, b.ModifyCount, a.ModifyCount)
require.Len(t, a.Columns, len(b.Columns))
for i := range a.Columns {
require.Equal(t, b.Columns[i].Count, a.Columns[i].Count)
require.True(t, statistics.HistogramEqual(&a.Columns[i].Histogram, &b.Columns[i].Histogram, false))
if a.Columns[i].CMSketch == nil {
require.Nil(t, b.Columns[i].CMSketch)
} else {
require.True(t, a.Columns[i].CMSketch.Equal(b.Columns[i].CMSketch))
}
// The nil case has been considered in (*TopN).Equal() so we don't need to consider it here.
require.Truef(t, a.Columns[i].TopN.Equal(b.Columns[i].TopN), "%v, %v", a.Columns[i].TopN, b.Columns[i].TopN)
}
require.Len(t, a.Indices, len(b.Indices))
for i := range a.Indices {
require.True(t, statistics.HistogramEqual(&a.Indices[i].Histogram, &b.Indices[i].Histogram, false))
if a.Indices[i].CMSketch == nil {
require.Nil(t, b.Indices[i].CMSketch)
} else {
require.True(t, a.Indices[i].CMSketch.Equal(b.Indices[i].CMSketch))
}
require.True(t, a.Indices[i].TopN.Equal(b.Indices[i].TopN))
}
require.True(t, isSameExtendedStats(a.ExtendedStats, b.ExtendedStats))
}

func isSameExtendedStats(a, b *statistics.ExtendedStatsColl) bool {
aEmpty := (a == nil) || len(a.Stats) == 0
bEmpty := (b == nil) || len(b.Stats) == 0
if (aEmpty && !bEmpty) || (!aEmpty && bEmpty) {
return false
}
if aEmpty && bEmpty {
return true
}
if len(a.Stats) != len(b.Stats) {
return false
}
for aKey, aItem := range a.Stats {
bItem, ok := b.Stats[aKey]
if !ok {
return false
}
for i, id := range aItem.ColIDs {
if id != bItem.ColIDs[i] {
return false
}
}
if (aItem.Tp != bItem.Tp) || (aItem.ScalarVals != bItem.ScalarVals) || (aItem.StringVals != bItem.StringVals) {
return false
}
}
return true
}

func TestStatsStoreAndLoad(t *testing.T) {
store, dom := testkit.CreateMockStoreAndDomain(t)
testKit := testkit.NewTestKit(t, store)
Expand All @@ -226,7 +170,7 @@ func TestStatsStoreAndLoad(t *testing.T) {
statsTbl2 := do.StatsHandle().GetTableStats(tableInfo)
require.False(t, statsTbl2.Pseudo)
require.Equal(t, int64(recordCount), statsTbl2.Count)
assertTableEqual(t, statsTbl1, statsTbl2)
internal.AssertTableEqual(t, statsTbl1, statsTbl2)
}

func TestEmptyTable(t *testing.T) {
Expand Down Expand Up @@ -510,7 +454,7 @@ func TestInitStats(t *testing.T) {
h.Clear()
require.NoError(t, h.Update(is))
table1 := h.GetTableStats(tbl.Meta())
assertTableEqual(t, table0, table1)
internal.AssertTableEqual(t, table0, table1)
h.SetLease(0)
}

Expand Down Expand Up @@ -540,7 +484,7 @@ func TestInitStatsVer2(t *testing.T) {
h.Clear()
require.NoError(t, h.Update(is))
table1 := h.GetTableStats(tbl.Meta())
assertTableEqual(t, table0, table1)
internal.AssertTableEqual(t, table0, table1)
h.SetLease(0)
}

Expand Down Expand Up @@ -2484,7 +2428,7 @@ func TestFeedbackWithGlobalStats(t *testing.T) {
statsTblAfter := h.GetTableStats(tblInfo)
// assert that statistics not changed
// the feedback can not work for the partition table in both static and dynamic mode
assertTableEqual(t, statsBefore, statsTblAfter)
internal.AssertTableEqual(t, statsBefore, statsTblAfter)
}

// Case 2: Feedback wouldn't be applied on version 2 and global-level statistics.
Expand Down
33 changes: 33 additions & 0 deletions statistics/handle/handletest/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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 handletest

import (
"testing"

"github.com/pingcap/tidb/testkit/testsetup"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
opts := []goleak.Option{
goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"),
goleak.IgnoreTopFunction("github.com/lestrrat-go/httprc.runFetchWorker"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"),
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
}
testsetup.SetupForCommonTest()
goleak.VerifyTestMain(m, opts...)
}
12 changes: 12 additions & 0 deletions statistics/handle/internal/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "internal",
srcs = ["testutil.go"],
importpath = "github.com/pingcap/tidb/statistics/handle/internal",
visibility = ["//statistics/handle:__subpackages__"],
deps = [
"//statistics",
"@com_github_stretchr_testify//require",
],
)
Loading

0 comments on commit 2067b5d

Please sign in to comment.