-
Notifications
You must be signed in to change notification settings - Fork 5
/
binding_test.go
57 lines (47 loc) · 1.22 KB
/
binding_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
package benchmarks_test
import (
"context"
stdhttptest "net/http/httptest"
"testing"
"github.com/abemedia/go-don"
_ "github.com/abemedia/go-don/encoding/text"
"github.com/abemedia/go-don/pkg/httptest"
"github.com/gin-gonic/gin"
)
func BenchmarkDon_BindRequest(b *testing.B) {
type request struct {
Path string `path:"path"`
Header string `header:"Header"`
Query string `query:"query"`
}
api := don.New(nil)
api.Post("/:path", don.H(func(ctx context.Context, req request) (any, error) {
return nil, nil
}))
h := api.RequestHandler()
ctx := httptest.NewRequest("POST", "/path?query=query", "", map[string]string{"header": "header"})
for i := 0; i < b.N; i++ {
h(ctx)
}
}
func BenchmarkGin_BindRequest(b *testing.B) {
type request struct {
Path string `uri:"path"`
Header string `header:"Header"`
Query string `form:"query"`
}
gin.SetMode("release")
router := gin.New()
router.POST("/:path", func(c *gin.Context) {
req := &request{}
c.ShouldBindHeader(req)
c.ShouldBindQuery(req)
c.ShouldBindUri(req)
})
w := stdhttptest.NewRecorder()
r := stdhttptest.NewRequest("POST", "/path?query=query", nil)
r.Header.Add("header", "header")
for i := 0; i < b.N; i++ {
router.ServeHTTP(w, r)
}
}