github.com/gogf/gf/v2@v2.7.4/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/gogf/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  	Age   int
    20  	ID    int
    21  }
    22  
    23  type structType8 struct {
    24  	Name        string  `json:"name"   `
    25  	CategoryId  string  `json:"category-Id" `
    26  	Price       float64 `json:"price"    `
    27  	Code        string  `json:"code"       `
    28  	Image       string  `json:"image"   `
    29  	Description string  `json:"description" `
    30  	Status      int     `json:"status"   `
    31  	IdType      int     `json:"id-type"`
    32  	Score       int
    33  	Age         int
    34  	ID          int
    35  }
    36  
    37  var (
    38  	structMap = map[string]interface{}{
    39  		"name":  "gf",
    40  		"score": 100,
    41  		"Age":   98,
    42  		"ID":    199,
    43  	}
    44  
    45  	structMapFields8 = map[string]interface{}{
    46  		"name":  "gf",
    47  		"score": 100,
    48  		"Age":   98,
    49  		"ID":    199,
    50  
    51  		"category-Id": "1",
    52  		"price":       198.09,
    53  		"code":        "1",
    54  		"image":       "https://goframe.org",
    55  		"description": "This is the data for testing eight fields",
    56  		"status":      1,
    57  		"id-type":     2,
    58  	}
    59  
    60  	structObj = structType{
    61  		Name:  "john",
    62  		Score: 60,
    63  		Age:   98,
    64  		ID:    199,
    65  	}
    66  	structPointer = &structType{
    67  		Name:  "john",
    68  		Score: 60,
    69  	}
    70  	structPointer8   = &structType8{}
    71  	structPointerNil *structType
    72  
    73  	// struct slice
    74  	structSliceNil []structType
    75  	structSlice    = []structType{
    76  		{Name: "john", Score: 60},
    77  		{Name: "smith", Score: 100},
    78  	}
    79  	// struct pointer slice
    80  	structPointerSliceNil []*structType
    81  	structPointerSlice    = []*structType{
    82  		{Name: "john", Score: 60},
    83  		{Name: "smith", Score: 100},
    84  	}
    85  )
    86  
    87  func Benchmark_Struct_Basic(b *testing.B) {
    88  	for i := 0; i < b.N; i++ {
    89  		Struct(structMap, structPointer)
    90  	}
    91  }
    92  
    93  func Benchmark_doStruct_Fields8_Basic_MapToStruct(b *testing.B) {
    94  	for i := 0; i < b.N; i++ {
    95  		doStruct(structMapFields8, structPointer8, map[string]string{}, "")
    96  	}
    97  }
    98  
    99  // *struct -> **struct
   100  func Benchmark_Reflect_PPStruct_PStruct(b *testing.B) {
   101  	for i := 0; i < b.N; i++ {
   102  		v1 := reflect.ValueOf(&structPointerNil)
   103  		v2 := reflect.ValueOf(structPointer)
   104  		//if v1.Kind() == reflect.Ptr {
   105  		//	if elem := v1.Elem(); elem.Type() == v2.Type() {
   106  		//		elem.Set(v2)
   107  		//	}
   108  		//}
   109  		v1.Elem().Set(v2)
   110  	}
   111  }
   112  
   113  func Benchmark_Struct_PPStruct_PStruct(b *testing.B) {
   114  	for i := 0; i < b.N; i++ {
   115  		Struct(structPointer, &structPointerNil)
   116  	}
   117  }
   118  
   119  // struct -> *struct
   120  func Benchmark_Reflect_PStruct_Struct(b *testing.B) {
   121  	for i := 0; i < b.N; i++ {
   122  		v1 := reflect.ValueOf(structPointer)
   123  		v2 := reflect.ValueOf(structObj)
   124  		//if v1.Kind() == reflect.Ptr {
   125  		//	if elem := v1.Elem(); elem.Type() == v2.Type() {
   126  		//		elem.Set(v2)
   127  		//	}
   128  		//}
   129  		v1.Elem().Set(v2)
   130  	}
   131  }
   132  
   133  func Benchmark_Struct_PStruct_Struct(b *testing.B) {
   134  	for i := 0; i < b.N; i++ {
   135  		Struct(structObj, structPointer)
   136  	}
   137  }
   138  
   139  // []struct -> *[]struct
   140  func Benchmark_Reflect_PStructs_Structs(b *testing.B) {
   141  	for i := 0; i < b.N; i++ {
   142  		v1 := reflect.ValueOf(&structSliceNil)
   143  		v2 := reflect.ValueOf(structSlice)
   144  		//if v1.Kind() == reflect.Ptr {
   145  		//	if elem := v1.Elem(); elem.Type() == v2.Type() {
   146  		//		elem.Set(v2)
   147  		//	}
   148  		//}
   149  		v1.Elem().Set(v2)
   150  	}
   151  }
   152  
   153  func Benchmark_Structs_PStructs_Structs(b *testing.B) {
   154  	for i := 0; i < b.N; i++ {
   155  		Structs(structSlice, &structSliceNil)
   156  	}
   157  }
   158  
   159  // []*struct -> *[]*struct
   160  func Benchmark_Reflect_PPStructs_PStructs(b *testing.B) {
   161  	for i := 0; i < b.N; i++ {
   162  		v1 := reflect.ValueOf(&structPointerSliceNil)
   163  		v2 := reflect.ValueOf(structPointerSlice)
   164  		//if v1.Kind() == reflect.Ptr {
   165  		//	if elem := v1.Elem(); elem.Type() == v2.Type() {
   166  		//		elem.Set(v2)
   167  		//	}
   168  		//}
   169  		v1.Elem().Set(v2)
   170  	}
   171  }
   172  
   173  func Benchmark_Structs_PPStructs_PStructs(b *testing.B) {
   174  	for i := 0; i < b.N; i++ {
   175  		Structs(structPointerSlice, &structPointerSliceNil)
   176  	}
   177  }