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