Skip to content

Commit

Permalink
stats: optimize unit test execution time (pingcap#8597)
Browse files Browse the repository at this point in the history
  • Loading branch information
alivxxx authored Dec 6, 2018
1 parent 126e44b commit 98c72c6
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 141 deletions.
2 changes: 1 addition & 1 deletion statistics/cmsketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *testStatisticsSuite) TestCMSketch(c *C) {
},
}
d, w := int32(5), int32(2048)
total, imax := uint64(1000000), uint64(10000000)
total, imax := uint64(100000), uint64(1000000)
for _, t := range tests {
lSketch, lMap, err := buildCMSketchAndMap(d, w, 0, total, imax, t.zipfFactor)
c.Check(err, IsNil)
Expand Down
8 changes: 4 additions & 4 deletions statistics/ddl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/pingcap/tidb/util/testkit"
)

func (s *testStatsCacheSuite) TestDDLAfterLoad(c *C) {
func (s *testStatsSuite) TestDDLAfterLoad(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
Expand Down Expand Up @@ -55,7 +55,7 @@ func (s *testStatsCacheSuite) TestDDLAfterLoad(c *C) {
c.Assert(int(count), Equals, 333)
}

func (s *testStatsCacheSuite) TestDDLTable(c *C) {
func (s *testStatsSuite) TestDDLTable(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
Expand Down Expand Up @@ -95,7 +95,7 @@ func (s *testStatsCacheSuite) TestDDLTable(c *C) {
c.Assert(statsTbl.Pseudo, IsFalse)
}

func (s *testStatsCacheSuite) TestDDLHistogram(c *C) {
func (s *testStatsSuite) TestDDLHistogram(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
do := s.do
Expand Down Expand Up @@ -172,7 +172,7 @@ func (s *testStatsCacheSuite) TestDDLHistogram(c *C) {
rs.Check(testkit.Rows("2"))
}

func (s *testStatsCacheSuite) TestDDLPartition(c *C) {
func (s *testStatsSuite) TestDDLPartition(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
Expand Down
35 changes: 6 additions & 29 deletions statistics/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,11 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/statistics"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testleak"
)

var _ = Suite(&testDumpStatsSuite{})

type testDumpStatsSuite struct {
store kv.Storage
do *domain.Domain
}

func (s *testDumpStatsSuite) SetUpSuite(c *C) {
testleak.BeforeTest()
var err error
s.store, s.do, err = newStoreWithBootstrap(0)
c.Assert(err, IsNil)
}

func (s *testDumpStatsSuite) TearDownSuite(c *C) {
s.do.Close()
s.store.Close()
testleak.AfterTest(c)()
}

func (s *testDumpStatsSuite) TestConversion(c *C) {
func (s *testStatsSuite) TestConversion(c *C) {
defer cleanEnv(c, s.store, s.do)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down Expand Up @@ -76,7 +53,7 @@ func (s *testDumpStatsSuite) TestConversion(c *C) {
assertTableEqual(c, loadTblInStorage, tbl)
}

func (s *testDumpStatsSuite) TestDumpPartitions(c *C) {
func (s *testStatsSuite) TestDumpPartitions(c *C) {
defer cleanEnv(c, s.store, s.do)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down Expand Up @@ -108,9 +85,9 @@ PARTITION BY RANGE ( a ) (
originTables = append(originTables, h.GetPartitionStats(tableInfo, def.ID))
}

tk.MustExec("truncate table mysql.stats_meta")
tk.MustExec("truncate table mysql.stats_histograms")
tk.MustExec("truncate table mysql.stats_buckets")
tk.MustExec("delete from mysql.stats_meta")
tk.MustExec("delete from mysql.stats_histograms")
tk.MustExec("delete from mysql.stats_buckets")
h.Clear()

err = h.LoadStatsFromJSON(s.do.InfoSchema(), jsonTbl)
Expand All @@ -121,7 +98,7 @@ PARTITION BY RANGE ( a ) (
}
}

func (s *testDumpStatsSuite) TestDumpAlteredTable(c *C) {
func (s *testStatsSuite) TestDumpAlteredTable(c *C) {
defer cleanEnv(c, s.store, s.do)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
4 changes: 2 additions & 2 deletions statistics/gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/pingcap/tidb/util/testkit"
)

func (s *testStatsUpdateSuite) TestGCStats(c *C) {
func (s *testStatsSuite) TestGCStats(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
Expand Down Expand Up @@ -53,7 +53,7 @@ func (s *testStatsUpdateSuite) TestGCStats(c *C) {
testKit.MustQuery("select count(*) from mysql.stats_meta").Check(testkit.Rows("0"))
}

func (s *testStatsUpdateSuite) TestGCPartition(c *C) {
func (s *testStatsSuite) TestGCPartition(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
Expand Down
66 changes: 23 additions & 43 deletions statistics/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,8 @@ import (
"github.com/pingcap/tidb/store/tikv/oracle"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testleak"
)

var _ = Suite(&testStatsCacheSuite{})

type testStatsCacheSuite struct {
store kv.Storage
do *domain.Domain
}

func (s *testStatsCacheSuite) SetUpSuite(c *C) {
testleak.BeforeTest()
var err error
s.store, s.do, err = newStoreWithBootstrap(0)
c.Assert(err, IsNil)
}

func (s *testStatsCacheSuite) TearDownSuite(c *C) {
s.do.Close()
s.store.Close()
testleak.AfterTest(c)()
}

func cleanEnv(c *C, store kv.Storage, do *domain.Domain) {
tk := testkit.NewTestKit(c, store)
tk.MustExec("use test")
Expand All @@ -60,13 +39,13 @@ func cleanEnv(c *C, store kv.Storage, do *domain.Domain) {
tableName := tb[0]
tk.MustExec(fmt.Sprintf("drop table %v", tableName))
}
tk.MustExec("truncate table mysql.stats_meta")
tk.MustExec("truncate table mysql.stats_histograms")
tk.MustExec("truncate table mysql.stats_buckets")
tk.MustExec("delete from mysql.stats_meta")
tk.MustExec("delete from mysql.stats_histograms")
tk.MustExec("delete from mysql.stats_buckets")
do.StatsHandle().Clear()
}

func (s *testStatsCacheSuite) TestStatsCache(c *C) {
func (s *testStatsSuite) TestStatsCache(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
Expand Down Expand Up @@ -135,7 +114,7 @@ func assertTableEqual(c *C, a *statistics.Table, b *statistics.Table) {
}
}

func (s *testStatsCacheSuite) TestStatsStoreAndLoad(c *C) {
func (s *testStatsSuite) TestStatsStoreAndLoad(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
Expand All @@ -162,7 +141,7 @@ func (s *testStatsCacheSuite) TestStatsStoreAndLoad(c *C) {
assertTableEqual(c, statsTbl1, statsTbl2)
}

func (s *testStatsCacheSuite) TestEmptyTable(c *C) {
func (s *testStatsSuite) TestEmptyTable(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
Expand All @@ -179,7 +158,7 @@ func (s *testStatsCacheSuite) TestEmptyTable(c *C) {
c.Assert(count, Equals, 0.0)
}

func (s *testStatsCacheSuite) TestColumnIDs(c *C) {
func (s *testStatsSuite) TestColumnIDs(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
Expand Down Expand Up @@ -210,7 +189,7 @@ func (s *testStatsCacheSuite) TestColumnIDs(c *C) {
c.Assert(count, Equals, 0.0)
}

func (s *testStatsCacheSuite) TestAvgColLen(c *C) {
func (s *testStatsSuite) TestAvgColLen(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
Expand Down Expand Up @@ -238,15 +217,15 @@ func (s *testStatsCacheSuite) TestAvgColLen(c *C) {
c.Assert(statsTbl.Columns[tableInfo.Columns[3].ID].AvgColSize(statsTbl.Count), Equals, 16.0)
}

func (s *testStatsCacheSuite) TestDurationToTS(c *C) {
func (s *testStatsSuite) TestDurationToTS(c *C) {
tests := []time.Duration{time.Millisecond, time.Second, time.Minute, time.Hour}
for _, t := range tests {
ts := statistics.DurationToTS(t)
c.Assert(oracle.ExtractPhysical(ts)*int64(time.Millisecond), Equals, int64(t))
}
}

func (s *testStatsCacheSuite) TestVersion(c *C) {
func (s *testStatsSuite) TestVersion(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
Expand Down Expand Up @@ -323,7 +302,7 @@ func (s *testStatsCacheSuite) TestVersion(c *C) {
c.Assert(statsTbl2.Columns[int64(3)], NotNil)
}

func (s *testStatsCacheSuite) TestLoadHist(c *C) {
func (s *testStatsSuite) TestLoadHist(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
Expand Down Expand Up @@ -381,7 +360,7 @@ func (s *testStatsCacheSuite) TestLoadHist(c *C) {
c.Assert(newStatsTbl2.Columns[int64(3)].LastUpdateVersion, Greater, newStatsTbl2.Columns[int64(1)].LastUpdateVersion)
}

func (s *testStatsCacheSuite) TestInitStats(c *C) {
func (s *testStatsSuite) TestInitStats(c *C) {
defer cleanEnv(c, s.store, s.do)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
Expand All @@ -406,24 +385,25 @@ func (s *testStatsCacheSuite) TestInitStats(c *C) {
h.Lease = 0
}

func (s *testStatsUpdateSuite) TestLoadStats(c *C) {
func (s *testStatsSuite) TestLoadStats(c *C) {
defer cleanEnv(c, s.store, s.do)
store, do, err := newStoreWithBootstrap(10 * time.Millisecond)
c.Assert(err, IsNil)
defer store.Close()
defer do.Close()
testKit := testkit.NewTestKit(c, store)
testKit := testkit.NewTestKit(c, s.store)
testKit.MustExec("use test")
testKit.MustExec("create table t(a int, b int, c int, primary key(a), key idx(b))")
testKit.MustExec("insert into t values (1,1,1),(2,2,2),(3,3,3)")

oriLease := s.do.StatsHandle().Lease
s.do.StatsHandle().Lease = 1
defer func() {
s.do.StatsHandle().Lease = oriLease
}()
testKit.MustExec("analyze table t")

is := do.InfoSchema()
is := s.do.InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
c.Assert(err, IsNil)
tableInfo := tbl.Meta()
h := do.StatsHandle()
time.Sleep(1 * time.Second)
h := s.do.StatsHandle()
stat := h.GetTableStats(tableInfo)
hg := stat.Columns[tableInfo.Columns[0].ID].Histogram
c.Assert(hg.Len(), Greater, 0)
Expand All @@ -439,7 +419,7 @@ func (s *testStatsUpdateSuite) TestLoadStats(c *C) {
c.Assert(cms, IsNil)
_, err = stat.ColumnEqualRowCount(testKit.Se.GetSessionVars().StmtCtx, types.NewIntDatum(1), tableInfo.Columns[2].ID)
c.Assert(err, IsNil)
time.Sleep(1 * time.Second)
c.Assert(h.LoadNeededHistograms(), IsNil)
stat = h.GetTableStats(tableInfo)
hg = stat.Columns[tableInfo.Columns[2].ID].Histogram
c.Assert(hg.Len(), Greater, 0)
Expand Down
Loading

0 comments on commit 98c72c6

Please sign in to comment.