github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/demo/handler_call_test.go (about)

     1  package test
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  type myint int64
     9  
    10  type Inccer interface {
    11  	Inc()
    12  }
    13  
    14  func (i myint) Inc2() {
    15  	i = 123 + 231 + 23
    16  }
    17  func (i *myint) Inc() {
    18  	*i = *i + 1
    19  }
    20  
    21  var (
    22  	func_v = reflect.ValueOf(myint.Inc2)
    23  
    24  	//incnReflectCall2(func_v, b.N)
    25  
    26  	rec = func_v.Type().In(0)
    27  	v   = reflect.Zero(rec)
    28  	f   = v.MethodByName("Inc2")
    29  )
    30  
    31  func BenchmarkReflectMethodCall2(b *testing.B) {
    32  	//func_type := reflect.TypeOf(myint.Inc2)
    33  	func_v := reflect.ValueOf(myint.Inc2)
    34  
    35  	incnReflectCall2(func_v, b.N)
    36  	/*
    37  		rec := func_v.Type().In(0)
    38  		v := reflect.Zero(rec)
    39  		f := v.MethodByName("Inc2")
    40  		incnReflectCall3(f, b.N)*/
    41  }
    42  
    43  func incnReflectCall2(fn reflect.Value, n int) {
    44  	rec := fn.Type().In(0)
    45  	v := reflect.Zero(rec)
    46  
    47  	for k := 0; k < n; k++ {
    48  		fn.Call([]reflect.Value{v})
    49  	}
    50  }
    51  
    52  func incnReflectCall3(v reflect.Value, n int) {
    53  	//	fn := v.Interface().(func())
    54  
    55  	for k := 0; k < n; k++ {
    56  		//fn()
    57  		func() int { return 1 + 1231231 }()
    58  		//v.Call(nil)
    59  	}
    60  }
    61  func BenchmarkReflectMethodCall(b *testing.B) {
    62  	i := new(myint)
    63  	incnReflectCall(i.Inc, b.N)
    64  }
    65  
    66  func _BenchmarkReflectOnceMethodCall(b *testing.B) {
    67  	i := new(myint)
    68  	incnReflectOnceCall(i.Inc, b.N)
    69  }
    70  
    71  func BenchmarkStructMethodCall(b *testing.B) {
    72  	i := new(myint)
    73  	incnIntmethod(i, b.N)
    74  }
    75  
    76  func _BenchmarkInterfaceMethodCall(b *testing.B) {
    77  	i := new(myint)
    78  	incnInterface(i, b.N)
    79  }
    80  
    81  func _BenchmarkTypeSwitchMethodCall(b *testing.B) {
    82  	i := new(myint)
    83  	incnSwitch(i, b.N)
    84  }
    85  
    86  func _BenchmarkTypeAssertionMethodCall(b *testing.B) {
    87  	i := new(myint)
    88  	incnAssertion(i, b.N)
    89  }
    90  
    91  func incnReflectCall(v interface{}, n int) {
    92  	for k := 0; k < n; k++ {
    93  		reflect.ValueOf(v).Call(nil)
    94  	}
    95  }
    96  
    97  func incnReflectOnceCall(v interface{}, n int) {
    98  	fn := reflect.ValueOf(v)
    99  	for k := 0; k < n; k++ {
   100  		fn.Call(nil)
   101  	}
   102  }
   103  
   104  func incnIntmethod(i *myint, n int) {
   105  	for k := 0; k < n; k++ {
   106  		i.Inc2()
   107  	}
   108  }
   109  
   110  func incnInterface(any Inccer, n int) {
   111  	for k := 0; k < n; k++ {
   112  		any.Inc()
   113  	}
   114  }
   115  
   116  func incnSwitch(any Inccer, n int) {
   117  	for k := 0; k < n; k++ {
   118  		switch v := any.(type) {
   119  		case *myint:
   120  			v.Inc()
   121  		}
   122  	}
   123  }
   124  
   125  func incnAssertion(any Inccer, n int) {
   126  	for k := 0; k < n; k++ {
   127  		if newint, ok := any.(*myint); ok {
   128  			newint.Inc()
   129  		}
   130  	}
   131  }