-
Notifications
You must be signed in to change notification settings - Fork 3
/
reflector_test.go
74 lines (60 loc) · 1.46 KB
/
reflector_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package slurp
import (
"reflect"
"testing"
"github.com/spy16/slurp/core"
)
var (
int64Type = reflect.TypeOf(int64(0))
int8Type = reflect.TypeOf(int8(0))
int8Vals []reflect.Value
)
func init() {
int8Vals = make([]reflect.Value, 1000)
for i := 0; i < len(int8Vals); i++ {
int8Vals[i] = reflect.ValueOf(int8(0))
}
}
func Benchmark_funcWrapper_Invoke(b *testing.B) {
fw := Func("foo", func(a, b int) int { return a + b })
var res int
for i := 0; i < b.N; i++ {
ret, _ := fw.Invoke(1, 2)
res = ret.(int)
}
b.Logf("final result: %d", res)
}
func Benchmark_funcWrapper_Invoke_Variadic(b *testing.B) {
fw := Func("foo", func(args ...int32) int32 { return args[0] + args[len(args)-1] })
args := make([]core.Any, 1000)
for i := 0; i < len(args); i++ {
args[i] = 1
}
var res int32
for i := 0; i < b.N; i++ {
ret, _ := fw.Invoke(args...)
res = ret.(int32)
}
b.Logf("final result: %d", res)
}
func Benchmark_convertArgsTo_Convertible(b *testing.B) {
var retVals []reflect.Value
var ret []reflect.Value
for i := 0; i < b.N; i++ {
ret, _ = convertArgsTo(int64Type, int8Vals...)
retVals = ret
}
dummyPrint(b, retVals)
}
func Benchmark_convertArgsTo_Assign(b *testing.B) {
var retVals []reflect.Value
var ret []reflect.Value
for i := 0; i < b.N; i++ {
ret, _ = convertArgsTo(int8Type, int8Vals...)
retVals = ret
}
dummyPrint(b, retVals)
}
func dummyPrint(b *testing.B, retVals []reflect.Value) {
b.Logf("return values of size=%d", len(retVals))
}