github.com/wangyougui/gf/v2@v2.6.5/encoding/gjson/gjson_z_bench_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gjson_test
     8  
     9  import (
    10  	json2 "encoding/json"
    11  	"testing"
    12  
    13  	"github.com/wangyougui/gf/v2/encoding/gjson"
    14  )
    15  
    16  var (
    17  	jsonStr1 = `{"name":"john","slice":[1,2,3]}`
    18  	jsonStr2 = `{"CallbackCommand":"Group.CallbackAfterSendMsg","From_Account":"61934946","GroupId":"@TGS#2FLGX67FD","MsgBody":[{"MsgContent":{"Text":"是的"},"MsgType":"TIMTextElem"}],"MsgSeq":23,"MsgTime":1567032819,"Operator_Account":"61934946","Random":2804799576,"Type":"Public"}`
    19  	jsonObj1 = gjson.New(jsonStr1)
    20  	jsonObj2 = gjson.New(jsonStr2)
    21  )
    22  
    23  func Benchmark_Validate_Simple_Json(b *testing.B) {
    24  	for i := 0; i < b.N; i++ {
    25  		gjson.Valid(jsonStr1)
    26  	}
    27  }
    28  
    29  func Benchmark_Validate_Complicated_Json(b *testing.B) {
    30  	for i := 0; i < b.N; i++ {
    31  		gjson.Valid(jsonStr2)
    32  	}
    33  }
    34  
    35  func Benchmark_Get_Simple_Json(b *testing.B) {
    36  	for i := 0; i < b.N; i++ {
    37  		jsonObj1.Get("name")
    38  	}
    39  }
    40  
    41  func Benchmark_Get_Complicated_Json(b *testing.B) {
    42  	for i := 0; i < b.N; i++ {
    43  		jsonObj2.Get("GroupId")
    44  	}
    45  }
    46  
    47  func Benchmark_Stdlib_Json_Unmarshal_Simple_Json(b *testing.B) {
    48  	for i := 0; i < b.N; i++ {
    49  		var m map[string]interface{}
    50  		json2.Unmarshal([]byte(jsonStr1), &m)
    51  	}
    52  }
    53  
    54  func Benchmark_Stdlib_Json_Unmarshal_Complicated_Json(b *testing.B) {
    55  	for i := 0; i < b.N; i++ {
    56  		var m map[string]interface{}
    57  		json2.Unmarshal([]byte(jsonStr2), &m)
    58  	}
    59  }
    60  
    61  func Benchmark_New_Simple_Json(b *testing.B) {
    62  	for i := 0; i < b.N; i++ {
    63  		gjson.New(jsonStr1)
    64  	}
    65  }
    66  
    67  func Benchmark_New_Complicated_Json(b *testing.B) {
    68  	for i := 0; i < b.N; i++ {
    69  		gjson.New(jsonStr2)
    70  	}
    71  }
    72  
    73  func Benchmark_Remove_Simple_Json(b *testing.B) {
    74  	for i := 0; i < b.N; i++ {
    75  		jsonObj1.Remove("name")
    76  	}
    77  }
    78  
    79  func Benchmark_Remove_Complicated_Json(b *testing.B) {
    80  	for i := 0; i < b.N; i++ {
    81  		jsonObj2.Remove("GroupId")
    82  	}
    83  }
    84  
    85  func Benchmark_New_Nil_And_Set_Simple(b *testing.B) {
    86  	for i := 0; i < b.N; i++ {
    87  		p := gjson.New(nil)
    88  		p.Set("k", "v")
    89  	}
    90  }
    91  
    92  func Benchmark_New_Nil_And_Set_Multiple_Level(b *testing.B) {
    93  	for i := 0; i < b.N; i++ {
    94  		p := gjson.New(nil)
    95  		p.Set("0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0", []int{1, 2, 3})
    96  	}
    97  }