github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/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      `time`
    22      `testing`
    23      `reflect`
    24  
    25      `github.com/stretchr/testify/require`
    26      `github.com/bytedance/sonic`
    27      `github.com/bytedance/sonic/option`
    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  func testPretouchTime(depth int) {
    63      start := time.Now()
    64      sonic.Pretouch(reflect.TypeOf(Issue138_DeepStruct{}), option.WithCompileRecursiveDepth(depth))
    65      elapsed := time.Since(start)
    66      fmt.Printf("Pretouch with recursive depth %d, time is %s\n", depth, elapsed)
    67  }
    68  
    69  func TestIssue138_PretouchTime(t *testing.T) {
    70      testPretouchTime(4)
    71      var obj Issue138_DeepStruct
    72      start := time.Now()
    73      data, err := sonic.Marshal(obj)
    74      err = sonic.Unmarshal([]byte(data), &obj)
    75      elapsed := time.Since(start)
    76      fmt.Printf("Marshal and unmarshal time is %s\n", elapsed)
    77      require.NoError(t, err)
    78  }
    79