-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathperf_bench_test.go
More file actions
146 lines (131 loc) · 3.95 KB
/
Copy pathperf_bench_test.go
File metadata and controls
146 lines (131 loc) · 3.95 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
package echo
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// nopResponseWriter is a no-op http.ResponseWriter used to isolate framework
// overhead from the cost of httptest.ResponseRecorder buffering.
type nopResponseWriter struct{ h http.Header }
func (w *nopResponseWriter) Header() http.Header {
if w.h == nil {
w.h = make(http.Header)
}
return w.h
}
func (w *nopResponseWriter) Write(b []byte) (int, error) { return len(b), nil }
func (w *nopResponseWriter) WriteHeader(int) {}
func benchServe(b *testing.B, e *Echo, req *http.Request) {
b.Helper()
w := &nopResponseWriter{}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.h = nil
e.ServeHTTP(w, req)
}
}
func BenchmarkServeHTTP_Static(b *testing.B) {
e := New()
e.GET("/users/profile", func(c *Context) error { return c.NoContent(http.StatusOK) })
req := httptest.NewRequest(http.MethodGet, "/users/profile", nil)
benchServe(b, e, req)
}
func BenchmarkServeHTTP_Param(b *testing.B) {
e := New()
e.GET("/users/:id/books/:bid", func(c *Context) error { return c.NoContent(http.StatusOK) })
req := httptest.NewRequest(http.MethodGet, "/users/42/books/7", nil)
benchServe(b, e, req)
}
// Exercises the global middleware chain (finding #1). Five pass-through middlewares.
func BenchmarkServeHTTP_Middleware(b *testing.B) {
e := New()
for i := 0; i < 5; i++ {
e.Use(func(next HandlerFunc) HandlerFunc {
return func(c *Context) error { return next(c) }
})
}
e.GET("/users/:id", func(c *Context) error { return c.NoContent(http.StatusOK) })
req := httptest.NewRequest(http.MethodGet, "/users/42", nil)
benchServe(b, e, req)
}
func BenchmarkServeHTTP_String(b *testing.B) {
e := New()
e.GET("/", func(c *Context) error { return c.String(http.StatusOK, "Hello, World!") })
req := httptest.NewRequest(http.MethodGet, "/", nil)
benchServe(b, e, req)
}
func BenchmarkServeHTTP_JSON(b *testing.B) {
e := New()
type payload struct {
ID int `json:"id"`
Name string `json:"name"`
Tags []string
}
p := payload{ID: 1, Name: "Jon Snow", Tags: []string{"a", "b", "c"}}
e.GET("/", func(c *Context) error { return c.JSON(http.StatusOK, p) })
req := httptest.NewRequest(http.MethodGet, "/", nil)
benchServe(b, e, req)
}
// Exercises a per-request Set (as request_id/auth middleware do), measuring the store-map reuse.
func BenchmarkServeHTTP_Store(b *testing.B) {
e := New()
e.GET("/", func(c *Context) error {
c.Set("user", "alice")
return c.NoContent(http.StatusOK)
})
req := httptest.NewRequest(http.MethodGet, "/", nil)
benchServe(b, e, req)
}
func BenchmarkContext_GetSet(b *testing.B) {
e := New()
c := e.NewContext(httptest.NewRequest(http.MethodGet, "/", nil), &nopResponseWriter{})
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Set("key", i)
_ = c.Get("key")
}
}
type bindTarget struct {
ID int `json:"id" query:"id"`
Name string `json:"name" query:"name"`
Email string `json:"email" query:"email"`
Age int `json:"age" query:"age"`
Active bool `json:"active" query:"active"`
}
func BenchmarkBind_JSON(b *testing.B) {
e := New()
body := `{"id":1,"name":"Jon Snow","email":"jon@winterfell.north","age":24,"active":true}`
e.POST("/", func(c *Context) error {
var t bindTarget
return c.Bind(&t)
})
b.ReportAllocs()
b.ResetTimer()
w := &nopResponseWriter{}
for i := 0; i < b.N; i++ {
w.h = nil
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(body))
req.Header.Set(HeaderContentType, MIMEApplicationJSON)
e.ServeHTTP(w, req)
}
}
func BenchmarkBind_Query(b *testing.B) {
e := New()
e.GET("/", func(c *Context) error {
var t bindTarget
return c.Bind(&t)
})
b.ReportAllocs()
b.ResetTimer()
w := &nopResponseWriter{}
req := httptest.NewRequest(http.MethodGet, "/?id=1&name=Jon&email=jon@x.io&age=24&active=true", nil)
for i := 0; i < b.N; i++ {
w.h = nil
e.ServeHTTP(w, req)
}
}