github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_z_bench_reflect_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_test
    10  
    11  import (
    12  	"reflect"
    13  	"testing"
    14  )
    15  
    16  type testStruct struct {
    17  	Id   int
    18  	Name string
    19  }
    20  
    21  var ptr = []*testStruct{
    22  	{
    23  		Id:   1,
    24  		Name: "test1",
    25  	},
    26  	{
    27  		Id:   2,
    28  		Name: "test2",
    29  	},
    30  }
    31  
    32  func init() {
    33  	for i := 1; i <= 1000; i++ {
    34  		ptr = append(ptr, &testStruct{
    35  			Id:   1,
    36  			Name: "test1",
    37  		})
    38  	}
    39  }
    40  
    41  func Benchmark_Reflect_ValueOf(b *testing.B) {
    42  	for i := 0; i < b.N; i++ {
    43  		reflect.ValueOf(ptr)
    44  	}
    45  }
    46  
    47  func Benchmark_Reflect_ValueOf_Kind(b *testing.B) {
    48  	for i := 0; i < b.N; i++ {
    49  		reflect.ValueOf(ptr).Kind()
    50  	}
    51  }
    52  
    53  func Benchmark_Reflect_ValueOf_Interface(b *testing.B) {
    54  	for i := 0; i < b.N; i++ {
    55  		reflect.ValueOf(ptr).Interface()
    56  	}
    57  }
    58  
    59  func Benchmark_Reflect_ValueOf_Len(b *testing.B) {
    60  	for i := 0; i < b.N; i++ {
    61  		reflect.ValueOf(ptr).Len()
    62  	}
    63  }