forked from gin-gonic/gin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmarks_test.go
122 lines (103 loc) · 2.86 KB
/
benchmarks_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
112
113
114
115
116
117
118
119
120
121
122
package gin
import (
"bytes"
"html/template"
"net/http"
"net/http/httptest"
"testing"
)
type FakeWriter struct{}
func (_ FakeWriter) Write(d []byte) (int, error) {
return 0, nil
}
func (_ FakeWriter) WriteString(d string) (int, error) {
return 0, nil
}
func runRequest(B *testing.B, r *Engine, method, path string) {
// create fake request
req, err := http.NewRequest(method, path, nil)
if err != nil {
panic(err)
}
w := httptest.NewRecorder()
B.ReportAllocs()
B.ResetTimer()
for i := 0; i < B.N; i++ {
r.ServeHTTP(w, req)
}
}
func BenchmarkOneRoute(B *testing.B) {
router := New()
router.GET("/ping", func(c *Context) {})
runRequest(B, router, "GET", "/ping")
}
func BenchmarkManyHandlers(B *testing.B) {
DefaultWriter = FakeWriter{}
//router := Default()
router := New()
router.Use(Recovery(), Logger())
router.Use(func(c *Context) {})
router.GET("/ping", func(c *Context) {})
runRequest(B, router, "GET", "/ping")
}
func Benchmark5Params(B *testing.B) {
DefaultWriter = new(bytes.Buffer)
router := New()
router.Use(func(c *Context) {})
router.GET("/param/:param1/:params2/:param3/:param4/:param5", func(c *Context) {})
runRequest(B, router, "GET", "/param/path/to/parameter/john/12345")
}
func BenchmarkOneRouteJSON(B *testing.B) {
router := New()
data := H{
"status": "ok",
}
router.GET("/json", func(c *Context) {
c.JSON(200, data)
})
runRequest(B, router, "GET", "/json")
}
var htmlContentType = []string{"text/html; charset=utf-8"}
func BenchmarkOneRouteHTML(B *testing.B) {
router := New()
t := template.Must(template.New("index").Parse(`
<html><body><h1>{{.}}</h1></body></html>`))
router.SetHTMLTemplate(t)
router.GET("/html", func(c *Context) {
//c.Writer.Header()["Content-Type"] = htmlContentType
//t.ExecuteTemplate(c.Writer, "index", "hola")
c.HTML(200, "index", "hola")
})
runRequest(B, router, "GET", "/html")
}
func BenchmarkOneRouteString(B *testing.B) {
router := New()
router.GET("/text", func(c *Context) {
c.String(200, "this is a plain text")
})
runRequest(B, router, "GET", "/text")
}
func BenchmarkManyRoutes(B *testing.B) {
router := New()
router.Any("/ping", func(c *Context) {})
runRequest(B, router, "PUT", "/ping")
}
func Benchmark404(B *testing.B) {
router := New()
router.Any("/something", func(c *Context) {})
router.NoRoute(func(c *Context) {})
runRequest(B, router, "GET", "/ping")
}
func Benchmark404Many(B *testing.B) {
router := New()
router.GET("/", func(c *Context) {})
router.GET("/path/to/something", func(c *Context) {})
router.GET("/post/:id", func(c *Context) {})
router.GET("/view/:id", func(c *Context) {})
router.GET("/favicon.ico", func(c *Context) {})
router.GET("/robots.txt", func(c *Context) {})
router.GET("/delete/:id", func(c *Context) {})
router.GET("/user/:id/:mode", func(c *Context) {})
router.NoRoute(func(c *Context) {})
runRequest(B, router, "GET", "/viewfake")
}