github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/internal/decoder/generic_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 decoder
    18  
    19  import (
    20      `fmt`
    21      `reflect`
    22      `testing`
    23  
    24      `github.com/bytedance/sonic/internal/native/types`
    25      `github.com/davecgh/go-spew/spew`
    26      `github.com/stretchr/testify/assert`
    27      `github.com/stretchr/testify/require`
    28  )
    29  
    30  //go:nosplit
    31  func decodeValueStub(st *_Stack, sp string, ic int, vp *interface{}, df uint64) (int, types.ParsingError)
    32  
    33  func decodeValue(k *_Stack, s string, i int, f uint64) (p int, v interface{}, e types.ParsingError) {
    34      p, e = decodeValueStub(k, s, i, &v, f)
    35      return
    36  }
    37  
    38  func decodeGeneric(s string, i int, f uint64) (p int, v interface{}, e types.ParsingError) {
    39      t := newStack()
    40      p, e = decodeValueStub(t, s, i, &v, f)
    41      freeStack(t)
    42      return
    43  }
    44  
    45  func TestGeneric_DecodeInterface(t *testing.T) {
    46      s := `[null, true, false, 1234, -1.25e-8, "hello\nworld", [], {"asdf": [1, 2.5, "qwer", null, true, false, [], {"zxcv": "fghj"}], "qwer": 7777}]`
    47      i, v, err := decodeGeneric(s, 0, 0)
    48      assert.Equal(t, len(s), i)
    49      if err != 0 {
    50          require.NoError(t, err)
    51      }
    52      fmt.Print("v: ")
    53      spew.Dump(v)
    54      fmt.Printf("type: %s\n", reflect.TypeOf(v))
    55  }
    56  
    57  func BenchmarkGeneric_DecodeGeneric(b *testing.B) {
    58      t := newStack()
    59      _, _, _ = decodeValue(t, TwitterJson, 0, 0)
    60      b.SetBytes(int64(len(TwitterJson)))
    61      b.ResetTimer()
    62      for i := 0; i < b.N; i++ {
    63          _, _, _ = decodeValue(t, TwitterJson, 0, 0)
    64      }
    65      freeStack(t)
    66  }
    67  
    68  func BenchmarkGeneric_Parallel_DecodeGeneric(b *testing.B) {
    69      _, _, _ = decodeGeneric(TwitterJson, 0, 0)
    70      b.SetBytes(int64(len(TwitterJson)))
    71      b.ResetTimer()
    72      b.RunParallel(func(pb *testing.PB) {
    73          for pb.Next() {
    74              _, _, _ = decodeGeneric(TwitterJson, 0, 0)
    75          }
    76      })
    77  }