github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/mergetop/top_test.go (about) 1 // Copyright 2021 Matrix Origin 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 mergetop 16 17 import ( 18 "bytes" 19 "context" 20 "testing" 21 22 "github.com/matrixorigin/matrixone/pkg/common/mpool" 23 "github.com/matrixorigin/matrixone/pkg/container/batch" 24 "github.com/matrixorigin/matrixone/pkg/container/types" 25 "github.com/matrixorigin/matrixone/pkg/pb/plan" 26 "github.com/matrixorigin/matrixone/pkg/testutil" 27 "github.com/matrixorigin/matrixone/pkg/vm/process" 28 "github.com/stretchr/testify/require" 29 ) 30 31 const ( 32 Rows = 10 // default rows 33 BenchmarkRows = 100000 // default rows for benchmark 34 ) 35 36 // add unit tests for cases 37 type topTestCase struct { 38 ds []bool // Directions, ds[i] == true: the attrs[i] are in descending order 39 arg *Argument 40 types []types.Type 41 proc *process.Process 42 cancel context.CancelFunc 43 } 44 45 var ( 46 tcs []topTestCase 47 ) 48 49 func init() { 50 tcs = []topTestCase{ 51 newTestCase([]bool{false}, []types.Type{{Oid: types.T_int8}}, 3, []*plan.OrderBySpec{{Expr: newExpression(0), Flag: 0}}), 52 newTestCase([]bool{true}, []types.Type{{Oid: types.T_int8}}, 3, []*plan.OrderBySpec{{Expr: newExpression(0), Flag: 2}}), 53 newTestCase([]bool{false, false}, []types.Type{{Oid: types.T_int8}, {Oid: types.T_int64}}, 3, []*plan.OrderBySpec{{Expr: newExpression(0), Flag: 0}}), 54 newTestCase([]bool{true, false}, []types.Type{{Oid: types.T_int8}, {Oid: types.T_int64}}, 3, []*plan.OrderBySpec{{Expr: newExpression(0), Flag: 2}}), 55 newTestCase([]bool{true, false}, []types.Type{{Oid: types.T_int8}, {Oid: types.T_int64}}, 3, []*plan.OrderBySpec{{Expr: newExpression(0), Flag: 2}, {Expr: newExpression(1), Flag: 0}}), 56 } 57 58 } 59 60 func TestString(t *testing.T) { 61 buf := new(bytes.Buffer) 62 for _, tc := range tcs { 63 String(tc.arg, buf) 64 } 65 } 66 67 func TestPrepare(t *testing.T) { 68 for _, tc := range tcs { 69 err := Prepare(tc.proc, tc.arg) 70 require.NoError(t, err) 71 } 72 } 73 74 func TestTop(t *testing.T) { 75 for _, tc := range tcs { 76 err := Prepare(tc.proc, tc.arg) 77 require.NoError(t, err) 78 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(t, tc.ds, tc.types, tc.proc, Rows) 79 tc.proc.Reg.MergeReceivers[0].Ch <- &batch.Batch{} 80 tc.proc.Reg.MergeReceivers[0].Ch <- nil 81 tc.proc.Reg.MergeReceivers[1].Ch <- newBatch(t, tc.ds, tc.types, tc.proc, Rows) 82 tc.proc.Reg.MergeReceivers[1].Ch <- &batch.Batch{} 83 tc.proc.Reg.MergeReceivers[1].Ch <- nil 84 for { 85 if ok, err := Call(0, tc.proc, tc.arg, false, false); ok || err != nil { 86 if tc.proc.Reg.InputBatch != nil { 87 tc.proc.Reg.InputBatch.Clean(tc.proc.Mp()) 88 } 89 break 90 } 91 } 92 for i := 0; i < len(tc.proc.Reg.MergeReceivers); i++ { // simulating the end of a pipeline 93 for len(tc.proc.Reg.MergeReceivers[i].Ch) > 0 { 94 bat := <-tc.proc.Reg.MergeReceivers[i].Ch 95 if bat != nil { 96 bat.Clean(tc.proc.Mp()) 97 } 98 } 99 } 100 require.Equal(t, tc.proc.Mp().CurrNB(), int64(0)) 101 } 102 } 103 104 func BenchmarkTop(b *testing.B) { 105 for i := 0; i < b.N; i++ { 106 tcs = []topTestCase{ 107 newTestCase([]bool{false}, []types.Type{{Oid: types.T_int8}}, 3, []*plan.OrderBySpec{{Expr: newExpression(0), Flag: 0}}), 108 newTestCase([]bool{true}, []types.Type{{Oid: types.T_int8}}, 3, []*plan.OrderBySpec{{Expr: newExpression(0), Flag: 2}}), 109 } 110 t := new(testing.T) 111 for _, tc := range tcs { 112 err := Prepare(tc.proc, tc.arg) 113 require.NoError(t, err) 114 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(t, tc.ds, tc.types, tc.proc, BenchmarkRows) 115 tc.proc.Reg.MergeReceivers[0].Ch <- &batch.Batch{} 116 tc.proc.Reg.MergeReceivers[0].Ch <- nil 117 tc.proc.Reg.MergeReceivers[1].Ch <- newBatch(t, tc.ds, tc.types, tc.proc, BenchmarkRows) 118 tc.proc.Reg.MergeReceivers[1].Ch <- &batch.Batch{} 119 tc.proc.Reg.MergeReceivers[1].Ch <- nil 120 for { 121 if ok, err := Call(0, tc.proc, tc.arg, false, false); ok || err != nil { 122 if tc.proc.Reg.InputBatch != nil { 123 tc.proc.Reg.InputBatch.Clean(tc.proc.Mp()) 124 } 125 break 126 } 127 } 128 for i := 0; i < len(tc.proc.Reg.MergeReceivers); i++ { // simulating the end of a pipeline 129 for len(tc.proc.Reg.MergeReceivers[i].Ch) > 0 { 130 bat := <-tc.proc.Reg.MergeReceivers[i].Ch 131 if bat != nil { 132 bat.Clean(tc.proc.Mp()) 133 } 134 } 135 } 136 } 137 } 138 } 139 140 func newTestCase(ds []bool, ts []types.Type, limit int64, fs []*plan.OrderBySpec) topTestCase { 141 proc := testutil.NewProcessWithMPool(mpool.MustNewZero()) 142 proc.Reg.MergeReceivers = make([]*process.WaitRegister, 2) 143 ctx, cancel := context.WithCancel(context.Background()) 144 proc.Reg.MergeReceivers[0] = &process.WaitRegister{ 145 Ctx: ctx, 146 Ch: make(chan *batch.Batch, 3), 147 } 148 proc.Reg.MergeReceivers[1] = &process.WaitRegister{ 149 Ctx: ctx, 150 Ch: make(chan *batch.Batch, 3), 151 } 152 return topTestCase{ 153 ds: ds, 154 types: ts, 155 proc: proc, 156 arg: &Argument{ 157 Fs: fs, 158 Limit: limit, 159 }, 160 cancel: cancel, 161 } 162 } 163 164 func newExpression(pos int32) *plan.Expr { 165 return &plan.Expr{ 166 Expr: &plan.Expr_Col{ 167 Col: &plan.ColRef{ 168 ColPos: pos, 169 }, 170 }, 171 } 172 } 173 174 // create a new block based on the type information, ds[i] == true: in descending order 175 func newBatch(t *testing.T, ds []bool, ts []types.Type, proc *process.Process, rows int64) *batch.Batch { 176 return testutil.NewBatch(ts, false, int(rows), proc.Mp()) 177 }