-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench_test.go
99 lines (86 loc) · 1.96 KB
/
bench_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
package gbind
import (
"context"
"net/http"
"testing"
"github.com/gin-gonic/gin"
)
type BindTestQuery struct {
Ak string `form:"ak,default=ak-default" gbind:"http.query.ak,default=ak-default"`
Tk string `form:"tk" gbind:"http.query.tk"`
Ts int64 `form:"ts" gbind:"http.query.ts"`
}
type BindTestForm struct {
Page int `form:"page" gbind:"http.post.page"`
Size int `form:"size" gbind:"http.post.size"`
Appkey string `form:"appkey" gbind:"http.post.appkey"`
}
type BindTestHeader struct {
Host string `header:"host" gbind:"http.header.host"`
}
type BindTestCookie struct {
Bduss string `gbind:"http.cookie.BDUSS"`
}
func BenchmarkBind(b *testing.B) {
req := newReq().
addQueryParam("ak", "ak1").
addQueryParam("tk", "tk1").
addQueryParam("ts", "123456789"). //
addFormParam("page", "1").
addFormParam("size", "2").
addFormParam("appkey", "3").
addHeader("host", "www.baidu.com").r()
req.AddCookie(&http.Cookie{
Name: "BDUSS",
Value: "bduss-value",
})
{
type BindTest struct {
BindTestQuery
BindTestForm
}
b.ResetTimer()
b.Run("gin-query-form", func(b *testing.B) {
for n := 0; n < b.N; n++ {
ginCtx := gin.Context{
Request: req,
}
value := &BindTest{}
ginCtx.ShouldBind(&value)
}
})
b.ResetTimer()
b.Run("gbind-query-form", func(b *testing.B) {
for n := 0; n < b.N; n++ {
value := &BindTest{}
Bind(context.Background(), &value, req)
}
})
}
{
type BindTest struct {
BindTestQuery
BindTestForm
BindTestHeader
BindTestCookie
}
b.ResetTimer()
b.Run("gin-query-form-header", func(b *testing.B) {
for n := 0; n < b.N; n++ {
ginCtx := gin.Context{
Request: req,
}
value := &BindTest{}
ginCtx.ShouldBind(&value)
ginCtx.ShouldBindHeader(&value)
}
})
b.ResetTimer()
b.Run("gbind-query-form-header", func(b *testing.B) {
for n := 0; n < b.N; n++ {
value := &BindTest{}
Bind(context.Background(), &value, req)
}
})
}
}