github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/issue_test/issue3_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      . `github.com/bytedance/sonic`
    21      `encoding/json`
    22      `reflect`
    23      `testing`
    24  
    25      `github.com/stretchr/testify/assert`
    26  )
    27  
    28  func TestIssue3_Encode(t *testing.T) {
    29      var v HugeStruct6
    30      ret, err := Marshal(v)
    31      assert.Nil(t, err)
    32      assert.Equal(t, []byte(`{}`), ret)
    33  }
    34  
    35  func TestIssue3_Decode(t *testing.T) {
    36      var v HugeStruct6
    37      err := Unmarshal([]byte(`{}`), &v)
    38      assert.Nil(t, err)
    39      assert.Equal(t, HugeStruct6{}, v)
    40  }
    41  
    42  func BenchmarkIssue3_Encode_Sonic(b *testing.B) {
    43      var v HugeStruct6
    44      err := Pretouch(reflect.TypeOf(v))
    45      assert.Nil(b, err)
    46      b.ResetTimer()
    47      for i := 0; i < b.N; i++ {
    48          _, _ = Marshal(v)
    49      }
    50  }
    51  
    52  func BenchmarkIssue3_Encode_StdLib(b *testing.B) {
    53      var v HugeStruct6
    54      ret, err := json.Marshal(v)
    55      assert.Nil(b, err)
    56      assert.Equal(b, []byte(`{}`), ret)
    57      b.ResetTimer()
    58      for i := 0; i < b.N; i++ {
    59          _, _ = json.Marshal(v)
    60      }
    61  }
    62  
    63  func BenchmarkIssue3_Decode_Sonic(b *testing.B) {
    64      var v HugeStruct6
    65      buf := []byte(`{}`)
    66      err := Pretouch(reflect.TypeOf(v))
    67      assert.Nil(b, err)
    68      b.ResetTimer()
    69      for i := 0; i < b.N; i++ {
    70          _ = Unmarshal(buf, &v)
    71      }
    72  }
    73  
    74  func BenchmarkIssue3_Decode_StdLib(b *testing.B) {
    75      var v HugeStruct6
    76      buf := []byte(`{}`)
    77      err := json.Unmarshal(buf, &v)
    78      assert.Nil(b, err)
    79      assert.Equal(b, HugeStruct6{}, v)
    80      b.ResetTimer()
    81      for i := 0; i < b.N; i++ {
    82          _ = json.Unmarshal(buf, &v)
    83      }
    84  }