github.com/wangyougui/gf/v2@v2.6.5/util/gconv/gconv_z_bench_struct_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  // go test *.go -bench=".*" -benchmem
     8  
     9  package gconv
    10  
    11  import (
    12  	"reflect"
    13  	"testing"
    14  )
    15  
    16  type structType struct {
    17  	Name  string
    18  	Score int
    19  }
    20  
    21  var (
    22  	structMap = map[string]interface{}{
    23  		"name":  "gf",
    24  		"score": 100,
    25  	}
    26  	structObj = structType{
    27  		Name:  "john",
    28  		Score: 60,
    29  	}
    30  	structPointer = &structType{
    31  		Name:  "john",
    32  		Score: 60,
    33  	}
    34  	structPointerNil *structType
    35  	// struct slice
    36  	structSliceNil []structType
    37  	structSlice    = []structType{
    38  		{Name: "john", Score: 60},
    39  		{Name: "smith", Score: 100},
    40  	}
    41  	// struct pointer slice
    42  	structPointerSliceNil []*structType
    43  	structPointerSlice    = []*structType{
    44  		{Name: "john", Score: 60},
    45  		{Name: "smith", Score: 100},
    46  	}
    47  )
    48  
    49  func Benchmark_Struct_Basic(b *testing.B) {
    50  	for i := 0; i < b.N; i++ {
    51  		Struct(structMap, structPointer)
    52  	}
    53  }
    54  
    55  // *struct -> **struct
    56  func Benchmark_Reflect_PPStruct_PStruct(b *testing.B) {
    57  	for i := 0; i < b.N; i++ {
    58  		v1 := reflect.ValueOf(&structPointerNil)
    59  		v2 := reflect.ValueOf(structPointer)
    60  		//if v1.Kind() == reflect.Ptr {
    61  		//	if elem := v1.Elem(); elem.Type() == v2.Type() {
    62  		//		elem.Set(v2)
    63  		//	}
    64  		//}
    65  		v1.Elem().Set(v2)
    66  	}
    67  }
    68  
    69  func Benchmark_Struct_PPStruct_PStruct(b *testing.B) {
    70  	for i := 0; i < b.N; i++ {
    71  		Struct(structPointer, &structPointerNil)
    72  	}
    73  }
    74  
    75  // struct -> *struct
    76  func Benchmark_Reflect_PStruct_Struct(b *testing.B) {
    77  	for i := 0; i < b.N; i++ {
    78  		v1 := reflect.ValueOf(structPointer)
    79  		v2 := reflect.ValueOf(structObj)
    80  		//if v1.Kind() == reflect.Ptr {
    81  		//	if elem := v1.Elem(); elem.Type() == v2.Type() {
    82  		//		elem.Set(v2)
    83  		//	}
    84  		//}
    85  		v1.Elem().Set(v2)
    86  	}
    87  }
    88  
    89  func Benchmark_Struct_PStruct_Struct(b *testing.B) {
    90  	for i := 0; i < b.N; i++ {
    91  		Struct(structObj, structPointer)
    92  	}
    93  }
    94  
    95  // []struct -> *[]struct
    96  func Benchmark_Reflect_PStructs_Structs(b *testing.B) {
    97  	for i := 0; i < b.N; i++ {
    98  		v1 := reflect.ValueOf(&structSliceNil)
    99  		v2 := reflect.ValueOf(structSlice)
   100  		//if v1.Kind() == reflect.Ptr {
   101  		//	if elem := v1.Elem(); elem.Type() == v2.Type() {
   102  		//		elem.Set(v2)
   103  		//	}
   104  		//}
   105  		v1.Elem().Set(v2)
   106  	}
   107  }
   108  
   109  func Benchmark_Structs_PStructs_Structs(b *testing.B) {
   110  	for i := 0; i < b.N; i++ {
   111  		Structs(structSlice, &structSliceNil)
   112  	}
   113  }
   114  
   115  // []*struct -> *[]*struct
   116  func Benchmark_Reflect_PPStructs_PStructs(b *testing.B) {
   117  	for i := 0; i < b.N; i++ {
   118  		v1 := reflect.ValueOf(&structPointerSliceNil)
   119  		v2 := reflect.ValueOf(structPointerSlice)
   120  		//if v1.Kind() == reflect.Ptr {
   121  		//	if elem := v1.Elem(); elem.Type() == v2.Type() {
   122  		//		elem.Set(v2)
   123  		//	}
   124  		//}
   125  		v1.Elem().Set(v2)
   126  	}
   127  }
   128  
   129  func Benchmark_Structs_PPStructs_PStructs(b *testing.B) {
   130  	for i := 0; i < b.N; i++ {
   131  		Structs(structPointerSlice, &structPointerSliceNil)
   132  	}
   133  }