-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenmarks_test.go
81 lines (70 loc) · 1.76 KB
/
benmarks_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
package woocoo
import (
"github.com/gin-gonic/gin"
"github.com/tsingsun/woocoo/pkg/conf"
"github.com/tsingsun/woocoo/web"
"github.com/tsingsun/woocoo/web/handler"
"net/http"
"testing"
)
func BenchmarkGinDefault(b *testing.B) {
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.Use(gin.Recovery())
router.Use(gin.Logger())
router.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
runRequest(b, router, "GET", "/ping")
}
func BenchmarkWooCooWebDefault(b *testing.B) {
gin.SetMode(gin.ReleaseMode)
router := web.New().Router().Engine
router.Use(handler.NewAccessLog().ApplyFunc(conf.New()))
router.Use(handler.Recovery().ApplyFunc(nil))
router.GET("ping/", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
runRequest(b, router, "GET", "/ping")
}
func BenchmarkGinDefaultMockLogger(b *testing.B) {
gin.SetMode(gin.ReleaseMode)
router := gin.New()
router.Use(gin.Recovery())
router.Use(gin.LoggerWithWriter(newMockWriter()))
router.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong")
})
runRequest(b, router, "GET", "/ping")
}
type mockWriter struct {
headers http.Header
}
func newMockWriter() *mockWriter {
return &mockWriter{
http.Header{},
}
}
func (m *mockWriter) Header() (h http.Header) {
return m.headers
}
func (m *mockWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}
func (m *mockWriter) WriteString(s string) (n int, err error) {
return len(s), nil
}
func (m *mockWriter) WriteHeader(int) {}
func runRequest(b *testing.B, r *gin.Engine, method, path string) {
// create fake request
req, err := http.NewRequest(method, path, nil)
if err != nil {
panic(err)
}
w := newMockWriter()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.ServeHTTP(w, req)
}
}