github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/thrift/binary_amd64_test.go (about)

     1  //go:build amd64 && go1.16
     2  // +build amd64,go1.16
     3  
     4  /**
     5   * Copyright 2023 ByteDance Inc.
     6   *
     7   * Licensed under the Apache License, Version 2.0 (the "License");
     8   * you may not use this file except in compliance with the License.
     9   * You may obtain a copy of the License at
    10   *
    11   *     http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   * Unless required by applicable law or agreed to in writing, software
    14   * distributed under the License is distributed on an "AS IS" BASIS,
    15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   * See the License for the specific language governing permissions and
    17   * limitations under the License.
    18   */
    19  
    20  package thrift
    21  
    22  import (
    23  	"strconv"
    24  	"testing"
    25  
    26  	"github.com/cloudwego/dynamicgo/testdata/kitex_gen/skip"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestSkip(t *testing.T) {
    31  	var MAX_STACKS = 1000
    32  	r := require.New(t)
    33  
    34  	obj := skip.NewTestListMap()
    35  	obj.ListMap = make([]map[string]string, 10)
    36  	for i := 0; i < 10; i++ {
    37  		obj.ListMap[i] = map[string]string{strconv.Itoa(i): strconv.Itoa(i)}
    38  	}
    39  	data := make([]byte, obj.BLength())
    40  	_ = obj.FastWriteNocopy(data, nil)
    41  
    42  	p := BinaryProtocol{
    43  		Buf: data,
    44  	}
    45  	println("Skip Go")
    46  	e1 := p.SkipGo(STRUCT, MAX_STACKS)
    47  	r.NoError(e1)
    48  	r.Equal(len(data), p.Read)
    49  
    50  	p.Read = 0
    51  	println("Skip Native")
    52  	e2 := p.SkipNative(STRUCT, MAX_STACKS)
    53  	r.NoError(e2)
    54  	r.Equal(len(data), p.Read)
    55  }
    56  
    57  
    58  func BenchmarkSkipNoCheck(b *testing.B) {
    59  	desc := getExampleDesc()
    60  	data := getExampleData()
    61  
    62  	b.Run("native", func(b *testing.B) {
    63  		p := NewBinaryProtocol(data)
    64  		err := p.SkipNative(desc.Type(), 512)
    65  		require.Nil(b, err)
    66  		require.Equal(b, len(data), p.Read)
    67  
    68  		b.SetBytes(int64(len(data)))
    69  		b.ResetTimer()
    70  		for i := 0; i < b.N; i++ {
    71  			p.Read = 0
    72  			_ = p.SkipNative(desc.Type(), 512)
    73  		}
    74  	})
    75  
    76  	b.Run("go", func(b *testing.B) {
    77  		p := NewBinaryProtocol(data)
    78  		err := p.SkipGo(desc.Type(), 512)
    79  		require.Nil(b, err)
    80  		require.Equal(b, len(data), p.Read)
    81  
    82  		b.SetBytes(int64(len(data)))
    83  		b.ResetTimer()
    84  		for i := 0; i < b.N; i++ {
    85  			p.Read = 0
    86  			_ = p.SkipGo(desc.Type(), 512)
    87  		}
    88  	})
    89  }
    90  
    91  func BenchmarkSkipAndCheck(b *testing.B) {
    92  	desc := getExampleDesc()
    93  	data := getExampleData()
    94  
    95  	b.Run("native", func(b *testing.B) {
    96  		p := NewBinaryProtocol(data)
    97  		err := p.SkipNative(desc.Type(), 512)
    98  		require.Nil(b, err)
    99  		require.Equal(b, len(data), p.Read)
   100  
   101  		b.SetBytes(int64(len(data)))
   102  		b.ResetTimer()
   103  		for i := 0; i < b.N; i++ {
   104  			p.Read = 0
   105  			err := p.SkipNative(desc.Type(), 512)
   106  			require.Nil(b, err)
   107  			require.Equal(b, len(data), p.Read)
   108  		}
   109  	})
   110  
   111  	b.Run("go", func(b *testing.B) {
   112  		p := NewBinaryProtocol(data)
   113  		err := p.SkipGo(desc.Type(), 512)
   114  		require.Nil(b, err)
   115  		require.Equal(b, len(data), p.Read)
   116  
   117  		b.SetBytes(int64(len(data)))
   118  		b.ResetTimer()
   119  		for i := 0; i < b.N; i++ {
   120  			p.Read = 0
   121  			err := p.SkipGo(desc.Type(), 512)
   122  			require.Nil(b, err)
   123  			require.Equal(b, len(data), p.Read)
   124  		}
   125  	})
   126  }