Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistics: migrate test-infra to testify for cmsketch_test.go #28552

Merged
merged 9 commits into from
Oct 19, 2021
113 changes: 60 additions & 53 deletions statistics/cmsketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ import (
"fmt"
"math"
"math/rand"
"testing"
"time"

. "github.com/pingcap/check"
"github.com/pingcap/errors"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/codec"
"github.com/stretchr/testify/require"
)

func (c *CMSketch) insert(val *types.Datum) error {
Expand Down Expand Up @@ -105,7 +106,8 @@ func averageAbsoluteError(cms *CMSketch, topN *TopN, mp map[int64]uint32) (uint6
return total / uint64(len(mp)), nil
}

func (s *testStatisticsSuite) TestCMSketch(c *C) {
func TestCMSketch(t *testing.T) {
t.Parallel()
tests := []struct {
zipfFactor float64
avgError uint64
Expand All @@ -125,31 +127,32 @@ func (s *testStatisticsSuite) TestCMSketch(c *C) {
}
d, w := int32(5), int32(2048)
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)
for _, tt := range tests {
lSketch, lMap, err := buildCMSketchAndMap(d, w, 0, total, imax, tt.zipfFactor)
require.NoError(t, err)
avg, err := averageAbsoluteError(lSketch, nil, lMap)
c.Assert(err, IsNil)
c.Check(avg, LessEqual, t.avgError)
require.NoError(t, err)
require.LessOrEqual(t, avg, tt.avgError)

rSketch, rMap, err := buildCMSketchAndMap(d, w, 1, total, imax, t.zipfFactor)
c.Check(err, IsNil)
rSketch, rMap, err := buildCMSketchAndMap(d, w, 1, total, imax, tt.zipfFactor)
require.NoError(t, err)
avg, err = averageAbsoluteError(rSketch, nil, rMap)
c.Assert(err, IsNil)
c.Check(avg, LessEqual, t.avgError)
require.NoError(t, err)
require.LessOrEqual(t, avg, tt.avgError)

err = lSketch.MergeCMSketch(rSketch)
c.Assert(err, IsNil)
require.NoError(t, err)
for val, count := range rMap {
lMap[val] += count
}
avg, err = averageAbsoluteError(lSketch, nil, lMap)
c.Assert(err, IsNil)
c.Check(avg, Less, t.avgError*2)
require.NoError(t, err)
require.Less(t, avg, tt.avgError*2)
}
}

func (s *testStatisticsSuite) TestCMSketchCoding(c *C) {
func TestCMSketchCoding(t *testing.T) {
t.Parallel()
lSketch := NewCMSketch(5, 2048)
lSketch.count = 2048 * math.MaxUint32
for i := range lSketch.table {
Expand All @@ -158,14 +161,15 @@ func (s *testStatisticsSuite) TestCMSketchCoding(c *C) {
}
}
bytes, err := EncodeCMSketchWithoutTopN(lSketch)
c.Assert(err, IsNil)
c.Assert(len(bytes), Equals, 61457)
require.NoError(t, err)
require.Len(t, bytes, 61457)
rSketch, _, err := DecodeCMSketchAndTopN(bytes, nil)
c.Assert(err, IsNil)
c.Assert(lSketch.Equal(rSketch), IsTrue)
require.NoError(t, err)
require.True(t, lSketch.Equal(rSketch))
}

func (s *testStatisticsSuite) TestCMSketchTopN(c *C) {
func TestCMSketchTopN(t *testing.T) {
t.Parallel()
tests := []struct {
zipfFactor float64
avgError uint64
Expand Down Expand Up @@ -193,17 +197,18 @@ func (s *testStatisticsSuite) TestCMSketchTopN(c *C) {
}
d, w := int32(5), int32(2048)
total, imax := uint64(1000000), uint64(1000000)
for _, t := range tests {
lSketch, topN, lMap, err := buildCMSketchTopNAndMap(d, w, 20, 1000, 0, total, imax, t.zipfFactor)
c.Check(err, IsNil)
c.Assert(len(topN.TopN), LessEqual, 40)
for _, tt := range tests {
lSketch, topN, lMap, err := buildCMSketchTopNAndMap(d, w, 20, 1000, 0, total, imax, tt.zipfFactor)
require.NoError(t, err)
require.LessOrEqual(t, len(topN.TopN), 40)
avg, err := averageAbsoluteError(lSketch, topN, lMap)
c.Assert(err, IsNil)
c.Check(avg, LessEqual, t.avgError)
require.NoError(t, err)
require.LessOrEqual(t, avg, tt.avgError)
}
}

func (s *testStatisticsSuite) TestMergeCMSketch4IncrementalAnalyze(c *C) {
func TestMergeCMSketch4IncrementalAnalyze(t *testing.T) {
t.Parallel()
tests := []struct {
zipfFactor float64
avgError uint64
Expand All @@ -227,33 +232,34 @@ func (s *testStatisticsSuite) TestMergeCMSketch4IncrementalAnalyze(c *C) {
}
d, w := int32(5), int32(2048)
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)
for _, tt := range tests {
lSketch, lMap, err := buildCMSketchAndMap(d, w, 0, total, imax, tt.zipfFactor)
require.NoError(t, err)
avg, err := averageAbsoluteError(lSketch, nil, lMap)
c.Assert(err, IsNil)
c.Check(avg, LessEqual, t.avgError)
require.NoError(t, err)
require.LessOrEqual(t, avg, tt.avgError)

rSketch, rMap, err := buildCMSketchAndMapWithOffset(d, w, 1, total, imax, t.zipfFactor, int64(imax))
c.Check(err, IsNil)
rSketch, rMap, err := buildCMSketchAndMapWithOffset(d, w, 1, total, imax, tt.zipfFactor, int64(imax))
require.NoError(t, err)
avg, err = averageAbsoluteError(rSketch, nil, rMap)
c.Assert(err, IsNil)
c.Check(avg, LessEqual, t.avgError)
require.NoError(t, err)
require.LessOrEqual(t, avg, tt.avgError)

for key, val := range rMap {
lMap[key] += val
}
c.Assert(lSketch.MergeCMSketch4IncrementalAnalyze(rSketch, 0), IsNil)
require.NoError(t, lSketch.MergeCMSketch4IncrementalAnalyze(rSketch, 0))
avg, err = averageAbsoluteError(lSketch, nil, lMap)
c.Assert(err, IsNil)
c.Check(avg, LessEqual, t.avgError)
require.NoError(t, err)
require.LessOrEqual(t, avg, tt.avgError)
width, depth := lSketch.GetWidthAndDepth()
c.Assert(width, Equals, int32(2048))
c.Assert(depth, Equals, int32(5))
require.Equal(t, int32(2048), width)
require.Equal(t, int32(5), depth)
}
}

func (s *testStatisticsSuite) TestCMSketchTopNUniqueData(c *C) {
func TestCMSketchTopNUniqueData(t *testing.T) {
t.Parallel()
d, w := int32(5), int32(2048)
total := uint64(1000000)
mp := make(map[int64]uint32)
Expand All @@ -266,15 +272,16 @@ func (s *testStatisticsSuite) TestCMSketchTopNUniqueData(c *C) {
}
}
cms, topN, err := prepareCMSAndTopN(d, w, vals, uint32(20), total)
c.Assert(err, IsNil)
require.NoError(t, err)
avg, err := averageAbsoluteError(cms, topN, mp)
c.Assert(err, IsNil)
c.Check(cms.defaultValue, Equals, uint64(1))
c.Check(avg, Equals, uint64(0))
c.Check(topN, IsNil)
require.NoError(t, err)
require.Equal(t, uint64(1), cms.defaultValue)
require.Equal(t, uint64(0), avg)
require.Nil(t, topN)
}

func (s *testStatisticsSuite) TestCMSketchCodingTopN(c *C) {
func TestCMSketchCodingTopN(t *testing.T) {
t.Parallel()
lSketch := NewCMSketch(5, 2048)
lSketch.count = 2048 * (math.MaxUint32)
for i := range lSketch.table {
Expand All @@ -296,12 +303,12 @@ func (s *testStatisticsSuite) TestCMSketchCodingTopN(c *C) {
}

bytes, err := EncodeCMSketchWithoutTopN(lSketch)
c.Assert(err, IsNil)
c.Assert(len(bytes), Equals, 61457)
require.NoError(t, err)
require.Len(t, bytes, 61457)
rSketch, _, err := DecodeCMSketchAndTopN(bytes, rows)
c.Assert(err, IsNil)
c.Assert(lSketch.Equal(rSketch), IsTrue)
require.NoError(t, err)
require.True(t, lSketch.Equal(rSketch))
// do not panic
_, _, err = DecodeCMSketchAndTopN([]byte{}, rows)
c.Assert(err, IsNil)
require.NoError(t, err)
}