github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_regexp_vec_const_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 memex
    15  
    16  import (
    17  	"testing"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    21  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    22  	"github.com/whtcorpsinc/milevadb/types"
    23  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    24  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    25  )
    26  
    27  func genVecBuiltinRegexpBenchCaseForConstants() (baseFunc builtinFunc, childrenFieldTypes []*types.FieldType, input *chunk.Chunk, output *chunk.DeferredCauset) {
    28  	const (
    29  		numArgs = 2
    30  		batchSz = 1024
    31  		rePat   = `\A[A-Za-z]{3,5}\d{1,5}[[:alpha:]]*\z`
    32  	)
    33  
    34  	childrenFieldTypes = make([]*types.FieldType, numArgs)
    35  	for i := 0; i < numArgs; i++ {
    36  		childrenFieldTypes[i] = eType2FieldType(types.ETString)
    37  	}
    38  
    39  	input = chunk.New(childrenFieldTypes, batchSz, batchSz)
    40  	// Fill the first arg with some random string
    41  	fillDeferredCausetWithGener(types.ETString, input, 0, newRandLenStrGener(10, 20))
    42  	// It seems like we still need to fill this defCausumn, otherwise event.GetCausetEvent() will crash
    43  	fillDeferredCausetWithGener(types.ETString, input, 1, &constStrGener{s: rePat})
    44  
    45  	args := make([]Expression, numArgs)
    46  	args[0] = &DeferredCauset{Index: 0, RetType: childrenFieldTypes[0]}
    47  	args[1] = CausetToConstant(types.NewStringCauset(rePat), allegrosql.TypeString)
    48  
    49  	var err error
    50  	baseFunc, err = funcs[ast.Regexp].getFunction(mock.NewContext(), args)
    51  	if err != nil {
    52  		panic(err)
    53  	}
    54  
    55  	output = chunk.NewDeferredCauset(eType2FieldType(types.ETInt), batchSz)
    56  	// Mess up the output to make sure vecEvalXXX to call ResizeXXX/ReserveXXX itself.
    57  	output.AppendNull()
    58  	return
    59  }
    60  
    61  func (s *testEvaluatorSuite) TestVectorizedBuiltinRegexpForConstants(c *C) {
    62  	bf, childrenFieldTypes, input, output := genVecBuiltinRegexpBenchCaseForConstants()
    63  	err := bf.vecEvalInt(input, output)
    64  	c.Assert(err, IsNil)
    65  	i64s := output.Int64s()
    66  
    67  	it := chunk.NewIterator4Chunk(input)
    68  	i := 0
    69  	commentf := func(event int) CommentInterface {
    70  		return Commentf("func: builtinRegexpUTF8Sig, event: %v, rowData: %v", event, input.GetEvent(event).GetCausetEvent(childrenFieldTypes))
    71  	}
    72  	for event := it.Begin(); event != it.End(); event = it.Next() {
    73  		val, isNull, err := bf.evalInt(event)
    74  		c.Assert(err, IsNil)
    75  		c.Assert(isNull, Equals, output.IsNull(i), commentf(i))
    76  		if !isNull {
    77  			c.Assert(val, Equals, i64s[i], commentf(i))
    78  		}
    79  		i++
    80  	}
    81  }
    82  
    83  func BenchmarkVectorizedBuiltinRegexpForConstants(b *testing.B) {
    84  	bf, _, input, output := genVecBuiltinRegexpBenchCaseForConstants()
    85  	b.Run("builtinRegexpUTF8Sig-Constants-VecBuiltinFunc", func(b *testing.B) {
    86  		b.ResetTimer()
    87  		for i := 0; i < b.N; i++ {
    88  			if err := bf.vecEvalInt(input, output); err != nil {
    89  				b.Fatal(err)
    90  			}
    91  		}
    92  	})
    93  	b.Run("builtinRegexpUTF8Sig-Constants-NonVecBuiltinFunc", func(b *testing.B) {
    94  		b.ResetTimer()
    95  		it := chunk.NewIterator4Chunk(input)
    96  		for i := 0; i < b.N; i++ {
    97  			output.Reset(types.ETInt)
    98  			for event := it.Begin(); event != it.End(); event = it.Next() {
    99  				v, isNull, err := bf.evalInt(event)
   100  				if err != nil {
   101  					b.Fatal(err)
   102  				}
   103  				if isNull {
   104  					output.AppendNull()
   105  				} else {
   106  					output.AppendInt64(v)
   107  				}
   108  			}
   109  		}
   110  	})
   111  }