github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/interlock/join_pkg_test.go (about) 1 // Copyright 2020 WHTCORPS INC, 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package interlock 15 16 import ( 17 "context" 18 "time" 19 20 . "github.com/whtcorpsinc/check" 21 "github.com/whtcorpsinc/failpoint" 22 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 23 "github.com/whtcorpsinc/milevadb/memex" 24 "github.com/whtcorpsinc/milevadb/types" 25 ) 26 27 func (s *pkgTestSerialSuite) TestJoinInterDirc(c *C) { 28 c.Assert(failpoint.Enable("github.com/whtcorpsinc/milevadb/interlock/testEventContainerSpill", "return(true)"), IsNil) 29 defer func() { c.Assert(failpoint.Disable("github.com/whtcorpsinc/milevadb/interlock/testEventContainerSpill"), IsNil) }() 30 defCausTypes := []*types.FieldType{ 31 types.NewFieldType(allegrosql.TypeLonglong), 32 types.NewFieldType(allegrosql.TypeDouble), 33 } 34 casTest := defaultHashJoinTestCase(defCausTypes, 0, false) 35 36 runTest := func() { 37 opt1 := mockDataSourceParameters{ 38 rows: casTest.rows, 39 ctx: casTest.ctx, 40 genDataFunc: func(event int, typ *types.FieldType) interface{} { 41 switch typ.Tp { 42 case allegrosql.TypeLong, allegrosql.TypeLonglong: 43 return int64(event) 44 case allegrosql.TypeDouble: 45 return float64(event) 46 default: 47 panic("not implement") 48 } 49 }, 50 } 51 opt2 := opt1 52 opt1.schemaReplicant = memex.NewSchema(casTest.defCausumns()...) 53 opt2.schemaReplicant = memex.NewSchema(casTest.defCausumns()...) 54 dataSource1 := buildMockDataSource(opt1) 55 dataSource2 := buildMockDataSource(opt2) 56 dataSource1.prepareChunks() 57 dataSource2.prepareChunks() 58 59 exec := prepare4HashJoin(casTest, dataSource1, dataSource2) 60 result := newFirstChunk(exec) 61 { 62 ctx := context.Background() 63 chk := newFirstChunk(exec) 64 err := exec.Open(ctx) 65 c.Assert(err, IsNil) 66 for { 67 err = exec.Next(ctx, chk) 68 c.Assert(err, IsNil) 69 if chk.NumEvents() == 0 { 70 break 71 } 72 result.Append(chk, 0, chk.NumEvents()) 73 } 74 c.Assert(exec.rowContainer.alreadySpilledSafeForTest(), Equals, casTest.disk) 75 err = exec.Close() 76 c.Assert(err, IsNil) 77 } 78 79 c.Assert(result.NumDefCauss(), Equals, 4) 80 c.Assert(result.NumEvents(), Equals, casTest.rows) 81 visit := make(map[int64]bool, casTest.rows) 82 for i := 0; i < casTest.rows; i++ { 83 val := result.DeferredCauset(0).Int64s()[i] 84 c.Assert(result.DeferredCauset(1).Float64s()[i], Equals, float64(val)) 85 c.Assert(result.DeferredCauset(2).Int64s()[i], Equals, val) 86 c.Assert(result.DeferredCauset(3).Float64s()[i], Equals, float64(val)) 87 visit[val] = true 88 } 89 for i := 0; i < casTest.rows; i++ { 90 c.Assert(visit[int64(i)], IsTrue) 91 } 92 } 93 94 concurrency := []int{1, 4} 95 rows := []int{3, 1024, 4096} 96 disk := []bool{false, true} 97 for _, concurrency := range concurrency { 98 for _, rows := range rows { 99 for _, disk := range disk { 100 casTest.concurrency = concurrency 101 casTest.rows = rows 102 casTest.disk = disk 103 runTest() 104 } 105 } 106 } 107 } 108 109 func (s *pkgTestSuite) TestHashJoinRuntimeStats(c *C) { 110 stats := &hashJoinRuntimeStats{ 111 fetchAndBuildHashBlock: 2 * time.Second, 112 hashStat: hashStatistic{ 113 probeDefCauslision: 1, 114 buildBlockElapse: time.Millisecond * 100, 115 }, 116 fetchAndProbe: int64(5 * time.Second), 117 probe: int64(4 * time.Second), 118 concurrent: 4, 119 maxFetchAndProbe: int64(2 * time.Second), 120 } 121 c.Assert(stats.String(), Equals, "build_hash_block:{total:2s, fetch:1.9s, build:100ms}, probe:{concurrency:4, total:5s, max:2s, probe:4s, fetch:1s, probe_defCauslision:1}") 122 c.Assert(stats.String(), Equals, stats.Clone().String()) 123 stats.Merge(stats.Clone()) 124 c.Assert(stats.String(), Equals, "build_hash_block:{total:4s, fetch:3.8s, build:200ms}, probe:{concurrency:4, total:10s, max:2s, probe:8s, fetch:2s, probe_defCauslision:2}") 125 } 126 127 func (s *pkgTestSuite) TestIndexJoinRuntimeStats(c *C) { 128 stats := indexLookUpJoinRuntimeStats{ 129 concurrency: 5, 130 probe: int64(time.Second), 131 innerWorker: innerWorkerRuntimeStats{ 132 totalTime: int64(time.Second * 5), 133 task: 16, 134 construct: int64(100 * time.Millisecond), 135 fetch: int64(300 * time.Millisecond), 136 build: int64(250 * time.Millisecond), 137 join: int64(150 * time.Millisecond), 138 }, 139 } 140 c.Assert(stats.String(), Equals, "inner:{total:5s, concurrency:5, task:16, construct:100ms, fetch:300ms, build:250ms, join:150ms}, probe:1s") 141 c.Assert(stats.String(), Equals, stats.Clone().String()) 142 stats.Merge(stats.Clone()) 143 c.Assert(stats.String(), Equals, "inner:{total:10s, concurrency:5, task:32, construct:200ms, fetch:600ms, build:500ms, join:300ms}, probe:2s") 144 }