-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
bench_test.go
91 lines (76 loc) · 2.59 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
// Borrowed code from go-router-benchmark
// See: https://github.com/bmf-san/go-router-benchmark
package goblin
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
// routeSet is a struct for routeSet.
type routeSet struct {
path string
reqPath string
}
var (
staticRoutesRoot = routeSet{"/", "/"}
staticRoutes1 = routeSet{"/foo", "/foo"}
staticRoutes5 = routeSet{"/foo/bar/baz/qux/quux", "/foo/bar/baz/qux/quux"}
staticRoutes10 = routeSet{"/foo/bar/baz/qux/quux/corge/grault/garply/waldo/fred", "/foo/bar/baz/qux/quux/corge/grault/garply/waldo/fred"}
pathParamRoutes1Colon = routeSet{"/foo/:bar", "/foo/bar"}
pathParamRoutes5Colon = routeSet{"/foo/:bar/:baz/:qux/:quux/:corge", "/foo/bar/baz/qux/quux/corge"}
pathParamRoutes10Colon = routeSet{"/foo/:bar/:baz/:qux/:quux/:corge/:grault/:garply/:waldo/:fred/:plugh", "/foo/bar/baz/qux/quux/corge/grault/garply/waldo/fred/plugh"}
)
func loadGoblin(r routeSet) http.Handler {
router := NewRouter()
handler := http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {})
router.Methods(http.MethodGet).Handler(r.path, handler)
return router
}
func testServeHTTP(b *testing.B, r routeSet, router http.Handler) {
rec := httptest.NewRecorder()
req, err := http.NewRequest(http.MethodGet, r.reqPath, nil)
if err != nil {
b.Fatal(err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
router.ServeHTTP(rec, req)
if rec.Code != 200 {
panic(fmt.Sprintf("Request failed. path: %v request path:%v", r.path, r.reqPath))
}
}
}
func benchmark(b *testing.B, r routeSet, router http.Handler) {
testServeHTTP(b, r, router)
}
// Benchmark tests
func BenchmarkStaticRoutesRootGoblin(b *testing.B) {
router := loadGoblin(staticRoutesRoot)
benchmark(b, staticRoutesRoot, router)
}
func BenchmarkStaticRoutes1Goblin(b *testing.B) {
router := loadGoblin(staticRoutes1)
benchmark(b, staticRoutes1, router)
}
func BenchmarkStaticRoutes5Goblin(b *testing.B) {
router := loadGoblin(staticRoutes5)
benchmark(b, staticRoutes5, router)
}
func BenchmarkStaticRoutes10Goblin(b *testing.B) {
router := loadGoblin(staticRoutes10)
benchmark(b, staticRoutes10, router)
}
func BenchmarkPathParamRoutes1ColonGoblin(b *testing.B) {
router := loadGoblin(pathParamRoutes1Colon)
benchmark(b, pathParamRoutes1Colon, router)
}
func BenchmarkPathParamRoutes5ColonGoblin(b *testing.B) {
router := loadGoblin(pathParamRoutes5Colon)
benchmark(b, pathParamRoutes5Colon, router)
}
func BenchmarkPathParamRoutes10ColonGoblin(b *testing.B) {
router := loadGoblin(pathParamRoutes10Colon)
benchmark(b, pathParamRoutes10Colon, router)
}