This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark_test.go
66 lines (60 loc) · 1.66 KB
/
benchmark_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
package gonion
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/julienschmidt/httprouter"
)
func BenchmarkSimple(b *testing.B) {
g := New()
g.Get("/simple", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "hello")
}))
routes := g.BuildRoutes()
router := httprouter.New()
for _, route := range routes {
router.Handle(route.Method, route.Pattern, func(rw http.ResponseWriter, r *http.Request, params httprouter.Params) {
route.Handler.ServeHTTP(rw, r)
})
}
b.ReportAllocs()
b.ResetTimer()
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/simple", nil)
for i := 0; i < b.N; i++ {
router.ServeHTTP(recorder, request)
}
}
func BenchmarkMiddleware(b *testing.B) {
g := New()
g.Sub("/a", func(a *Composer) {
a.Use().Func(func(rw http.ResponseWriter, r *http.Request) {
})
a.Use().Func(func(rw http.ResponseWriter, r *http.Request) {
})
a.Sub("/c", func(c *Composer) {
c.Use().Func(func(rw http.ResponseWriter, r *http.Request) {
})
c.Use().Func(func(rw http.ResponseWriter, r *http.Request) {
})
c.Get("/action", http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "hello")
}))
})
})
routes := g.BuildRoutes()
router := httprouter.New()
for _, route := range routes {
router.Handle(route.Method, route.Pattern, func(rw http.ResponseWriter, r *http.Request, params httprouter.Params) {
route.Handler.ServeHTTP(rw, r)
})
}
b.ReportAllocs()
b.ResetTimer()
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/a/c/action", nil)
for i := 0; i < b.N; i++ {
router.ServeHTTP(recorder, request)
}
}