github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/mergelimit/limit_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 mergelimit 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/testutil" 26 "github.com/matrixorigin/matrixone/pkg/vm" 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 = 1000000 // default rows for benchmark 34 ) 35 36 // add unit tests for cases 37 type limitTestCase struct { 38 arg *Argument 39 types []types.Type 40 proc *process.Process 41 cancel context.CancelFunc 42 } 43 44 var ( 45 tcs []limitTestCase 46 ) 47 48 func init() { 49 tcs = []limitTestCase{ 50 newTestCase(8), 51 newTestCase(10), 52 newTestCase(12), 53 } 54 } 55 56 func TestString(t *testing.T) { 57 buf := new(bytes.Buffer) 58 for _, tc := range tcs { 59 tc.arg.String(buf) 60 } 61 } 62 63 func TestPrepare(t *testing.T) { 64 for _, tc := range tcs { 65 err := tc.arg.Prepare(tc.proc) 66 require.NoError(t, err) 67 } 68 } 69 70 func TestLimit(t *testing.T) { 71 for _, tc := range tcs { 72 err := tc.arg.Prepare(tc.proc) 73 require.NoError(t, err) 74 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, Rows) 75 tc.proc.Reg.MergeReceivers[0].Ch <- batch.EmptyBatch 76 tc.proc.Reg.MergeReceivers[0].Ch <- nil 77 tc.proc.Reg.MergeReceivers[1].Ch <- newBatch(tc.types, tc.proc, Rows) 78 tc.proc.Reg.MergeReceivers[1].Ch <- batch.EmptyBatch 79 tc.proc.Reg.MergeReceivers[1].Ch <- nil 80 for { 81 ok, err := tc.arg.Call(tc.proc) 82 if ok.Status == vm.ExecStop || err != nil { 83 break 84 } 85 } 86 for i := 0; i < len(tc.proc.Reg.MergeReceivers); i++ { // simulating the end of a pipeline 87 for len(tc.proc.Reg.MergeReceivers[i].Ch) > 0 { 88 bat := <-tc.proc.Reg.MergeReceivers[i].Ch 89 if bat != nil { 90 bat.Clean(tc.proc.Mp()) 91 } 92 } 93 } 94 tc.proc.FreeVectors() 95 require.Equal(t, int64(0), tc.proc.Mp().CurrNB()) 96 } 97 } 98 99 func BenchmarkLimit(b *testing.B) { 100 for i := 0; i < b.N; i++ { 101 tcs = []limitTestCase{ 102 newTestCase(8), 103 newTestCase(10), 104 newTestCase(12), 105 } 106 107 t := new(testing.T) 108 for _, tc := range tcs { 109 err := tc.arg.Prepare(tc.proc) 110 require.NoError(t, err) 111 tc.proc.Reg.MergeReceivers[0].Ch <- newBatch(tc.types, tc.proc, BenchmarkRows) 112 tc.proc.Reg.MergeReceivers[0].Ch <- batch.EmptyBatch 113 tc.proc.Reg.MergeReceivers[0].Ch <- nil 114 tc.proc.Reg.MergeReceivers[1].Ch <- newBatch(tc.types, tc.proc, BenchmarkRows) 115 tc.proc.Reg.MergeReceivers[1].Ch <- batch.EmptyBatch 116 tc.proc.Reg.MergeReceivers[1].Ch <- nil 117 for { 118 ok, err := tc.arg.Call(tc.proc) 119 if ok.Status == vm.ExecStop || err != nil { 120 break 121 } 122 } 123 for i := 0; i < len(tc.proc.Reg.MergeReceivers); i++ { // simulating the end of a pipeline 124 for len(tc.proc.Reg.MergeReceivers[i].Ch) > 0 { 125 bat := <-tc.proc.Reg.MergeReceivers[i].Ch 126 if bat != nil { 127 bat.Clean(tc.proc.Mp()) 128 } 129 } 130 } 131 } 132 } 133 } 134 135 func newTestCase(limit uint64) limitTestCase { 136 proc := testutil.NewProcessWithMPool(mpool.MustNewZero()) 137 proc.Reg.MergeReceivers = make([]*process.WaitRegister, 2) 138 ctx, cancel := context.WithCancel(context.Background()) 139 proc.Reg.MergeReceivers[0] = &process.WaitRegister{ 140 Ctx: ctx, 141 Ch: make(chan *batch.Batch, 3), 142 } 143 proc.Reg.MergeReceivers[1] = &process.WaitRegister{ 144 Ctx: ctx, 145 Ch: make(chan *batch.Batch, 3), 146 } 147 return limitTestCase{ 148 proc: proc, 149 types: []types.Type{ 150 types.T_int8.ToType(), 151 }, 152 arg: &Argument{ 153 Limit: limit, 154 OperatorBase: vm.OperatorBase{ 155 OperatorInfo: vm.OperatorInfo{ 156 Idx: 0, 157 IsFirst: false, 158 IsLast: false, 159 }, 160 }, 161 }, 162 cancel: cancel, 163 } 164 } 165 166 // create a new block based on the type information 167 func newBatch(ts []types.Type, proc *process.Process, rows int64) *batch.Batch { 168 return testutil.NewBatch(ts, false, int(rows), proc.Mp()) 169 }