github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/issue_test/issue138_test.go (about)

     1  /*
     2   * Copyright 2021 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package issue_test
    18  
    19  import (
    20  	"fmt"
    21  	"reflect"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/goshafaq/sonic"
    26  	"github.com/goshafaq/sonic/option"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  type Issue138_DeepStruct struct {
    31  	L0 struct {
    32  		L1 struct {
    33  			L2 struct {
    34  				L3 struct {
    35  					L4 struct {
    36  						L5 struct {
    37  							L6 struct {
    38  								L7 struct {
    39  									L8 struct {
    40  										L9 struct {
    41  											L10 struct {
    42  												L11 struct {
    43  													L12 struct {
    44  														L13 struct {
    45  															L14 struct {
    46  																L15 struct {
    47  																	L16 struct {
    48  																		L17 struct {
    49  																			L18 struct {
    50  																				L19 struct {
    51  																					L20 struct {
    52  																						L21 struct {
    53  																							L22 struct {
    54  																								A int
    55  																								B string
    56  																								C []float64
    57  																								E map[string]bool
    58  																								F *Issue138_DeepStruct
    59  																							}
    60  																						}
    61  																					}
    62  																				}
    63  																			}
    64  																		}
    65  																	}
    66  																}
    67  															}
    68  														}
    69  													}
    70  												}
    71  											}
    72  										}
    73  									}
    74  								}
    75  							}
    76  						}
    77  					}
    78  				}
    79  			}
    80  		}
    81  	}
    82  }
    83  
    84  func testPretouchTime(depth int) {
    85  	start := time.Now()
    86  	sonic.Pretouch(reflect.TypeOf(Issue138_DeepStruct{}), option.WithCompileRecursiveDepth(depth))
    87  	elapsed := time.Since(start)
    88  	fmt.Printf("Pretouch with recursive depth %d, time is %s\n", depth, elapsed)
    89  }
    90  
    91  func TestIssue138_PretouchTime(t *testing.T) {
    92  	testPretouchTime(4)
    93  	var obj Issue138_DeepStruct
    94  	start := time.Now()
    95  	data, err := sonic.Marshal(obj)
    96  	err = sonic.Unmarshal([]byte(data), &obj)
    97  	elapsed := time.Since(start)
    98  	fmt.Printf("Marshal and unmarshal time is %s\n", elapsed)
    99  	require.NoError(t, err)
   100  }