forked from glennliao/apijson-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz_test.go
executable file
·111 lines (94 loc) · 2.45 KB
/
z_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package _func
import (
"context"
"fmt"
"github.com/glennliao/apijson-go/model"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"reflect"
"testing"
)
// 1
type Func1 struct {
ParamList []string
ParamTypeList []string
Handler func(ctx context.Context, param model.Map) (res interface{}, err error)
}
type Func2 struct {
Handler any
}
func basic(ctx context.Context, user string) (string, error) {
return user + " hi", nil
}
var f1 Func1
var f2 Func2
var ctx = gctx.New()
var funcValue reflect.Value
func init() {
f1 = Func1{
ParamList: []string{"req", "xx"},
ParamTypeList: []string{"string", "string"}, // or any
Handler: func(ctx context.Context, param model.Map) (res interface{}, err error) {
return param["user"].(string) + " :hi", nil
},
}
f2 = Func2{Handler: func(ctx context.Context, user string) (string, error) {
return user + " hi", nil
}}
// 将函数包装为反射值对象
funcValue = reflect.ValueOf(f2.Handler)
}
func TestName(t *testing.T) {
ret, err := f1.Handler(ctx, model.Map{"user": 1})
if err != nil {
panic(err)
}
g.Dump(ret)
}
func TestName2(t *testing.T) {
// 将函数包装为反射值对象
funcValue := reflect.ValueOf(f2.Handler)
// 构造函数参数, 传入两个整型值
paramList := []reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf("luc")}
// 反射调用函数
retList := funcValue.Call(paramList)
// 获取第一个返回值, 取整数值
fmt.Println(retList[0].String())
}
func BenchmarkBasic(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := basic(ctx, "user1")
if err != nil {
panic(err)
}
}
}
func BenchmarkName(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := f1.Handler(ctx, model.Map{"user": "user1"})
if err != nil {
panic(err)
}
}
}
func BenchmarkName23(b *testing.B) {
for i := 0; i < b.N; i++ {
// 构造函数参数, 传入两个整型值
paramList := []reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf("luc")}
// 反射调用函数
_ = funcValue.Call(paramList)
// 获取第一个返回值, 取整数值
//fmt.Println(retList[0].String())
}
}
func BenchmarkName2(b *testing.B) {
for i := 0; i < b.N; i++ {
funcValue := reflect.ValueOf(f2.Handler)
// 构造函数参数, 传入两个整型值
paramList := []reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf("luc")}
// 反射调用函数
_ = funcValue.Call(paramList)
// 获取第一个返回值, 取整数值
//fmt.Println(retList[0].String())
}
}