github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/mergeoffset/offset_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 mergeoffset
    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 offsetTestCase struct {
    38  	arg    *Argument
    39  	types  []types.Type
    40  	proc   *process.Process
    41  	cancel context.CancelFunc
    42  }
    43  
    44  var (
    45  	tcs []offsetTestCase
    46  )
    47  
    48  func init() {
    49  	tcs = []offsetTestCase{
    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 TestOffset(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 BenchmarkOffset(b *testing.B) {
   100  	for i := 0; i < b.N; i++ {
   101  		tcs = []offsetTestCase{
   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  
   136  func newTestCase(offset uint64) offsetTestCase {
   137  	proc := testutil.NewProcessWithMPool(mpool.MustNewZero())
   138  	proc.Reg.MergeReceivers = make([]*process.WaitRegister, 2)
   139  	ctx, cancel := context.WithCancel(context.Background())
   140  	proc.Reg.MergeReceivers[0] = &process.WaitRegister{
   141  		Ctx: ctx,
   142  		Ch:  make(chan *batch.Batch, 3),
   143  	}
   144  	proc.Reg.MergeReceivers[1] = &process.WaitRegister{
   145  		Ctx: ctx,
   146  		Ch:  make(chan *batch.Batch, 3),
   147  	}
   148  	return offsetTestCase{
   149  		proc: proc,
   150  		types: []types.Type{
   151  			types.T_int8.ToType(),
   152  		},
   153  		arg: &Argument{
   154  			Offset: offset,
   155  			OperatorBase: vm.OperatorBase{
   156  				OperatorInfo: vm.OperatorInfo{
   157  					Idx:     0,
   158  					IsFirst: false,
   159  					IsLast:  false,
   160  				},
   161  			},
   162  		},
   163  		cancel: cancel,
   164  	}
   165  }
   166  
   167  // create a new block based on the type information
   168  func newBatch(ts []types.Type, proc *process.Process, rows int64) *batch.Batch {
   169  	return testutil.NewBatch(ts, false, int(rows), proc.Mp())
   170  }