Repository navigation
Expand file tree
/
Copy pathbenchmarks_test.go
More file actions
270 lines (242 loc) · 7.24 KB
/
Copy pathbenchmarks_test.go
File metadata and controls
270 lines (242 loc) · 7.24 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package restapi
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func benchAPI(b *testing.B) *API {
b.Helper()
return Default()
}
// BenchmarkErrorRegistry_Lookup measures the hot-path cost of resolving a
// known code.
func BenchmarkErrorRegistry_Lookup(b *testing.B) {
b.ReportAllocs()
reg := Default().Registry()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = reg.Lookup(CodeNotFound)
}
}
// BenchmarkErrorRegistry_LookupParallel measures RWMutex read contention
// under many concurrent readers.
func BenchmarkErrorRegistry_LookupParallel(b *testing.B) {
b.ReportAllocs()
reg := Default().Registry()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _ = reg.Lookup(CodeNotFound)
}
})
}
// BenchmarkErrorRegistry_LookupMiss measures the fallback path.
func BenchmarkErrorRegistry_LookupMiss(b *testing.B) {
b.ReportAllocs()
reg := Default().Registry()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = reg.Lookup(99999)
}
}
// BenchmarkErrorRegistry_Register measures the cold-path cost of inserting
// a single entry. A fresh registry is used to avoid duplicate conflicts.
func BenchmarkErrorRegistry_Register(b *testing.B) {
b.ReportAllocs()
fb := ErrorRegistryEntry{StatusCode: 500, ErrorCode: CodeInternal, Message: "fb"}
reg, err := NewErrorRegistry(fb)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = reg.Register(ErrorRegistryEntry{StatusCode: 400, ErrorCode: 2000 + i, Message: "x"})
}
}
// BenchmarkAPI_NewError_Known benchmarks building an ErrorResponse from a
// registered code without options.
func BenchmarkAPI_NewError_Known(b *testing.B) {
b.ReportAllocs()
a := benchAPI(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = a.NewError(CodeNotFound)
}
}
// BenchmarkAPI_NewError_Unknown benchmarks the fallback path of NewError.
func BenchmarkAPI_NewError_Unknown(b *testing.B) {
b.ReportAllocs()
a := benchAPI(b)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = a.NewError(99999)
}
}
// BenchmarkAPI_NewError_WithOptions includes one WithMessage and one
// WithDetails option so the option-application loop is exercised.
func BenchmarkAPI_NewError_WithOptions(b *testing.B) {
b.ReportAllocs()
a := benchAPI(b)
details := map[string]any{"field": "email"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = a.NewError(CodeValidationFailed, WithMessage("m"), WithDetails(details))
}
}
// newBenchEngine builds a gin.Engine in ReleaseMode with a single GET /
// route bound to fn. It is the shared fixture for the engine-backed handler
// benchmarks below. Running in ReleaseMode silences gin's per-request debug
// logging, which otherwise dominates the benchmark.
func newBenchEngine(fn gin.HandlerFunc) *gin.Engine {
gin.SetMode(gin.ReleaseMode)
engine := gin.New()
engine.GET("/", fn)
return engine
}
// BenchmarkAPI_Success measures the per-request cost of a successful response
// dispatched through a real gin.Engine. Setup (engine, route, request) is
// done once; only the recorder — unavoidable per response — is allocated
// inside the loop.
func BenchmarkAPI_Success(b *testing.B) {
b.ReportAllocs()
payload := map[string]any{"id": 1, "name": "alice"}
engine := newBenchEngine(func(c *gin.Context) {
Success(c, http.StatusOK, payload)
})
req := httptest.NewRequest(http.MethodGet, "/", nil)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
engine.ServeHTTP(w, req)
}
}
// BenchmarkAPI_Error measures the per-request cost of an error response
// dispatched through a real gin.Engine.
func BenchmarkAPI_Error(b *testing.B) {
b.ReportAllocs()
engine := newBenchEngine(func(c *gin.Context) {
Error(c, CodeNotFound)
})
req := httptest.NewRequest(http.MethodGet, "/", nil)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
engine.ServeHTTP(w, req)
}
}
// BenchmarkAPI_Abort measures the per-request cost of an aborting error
// response dispatched through a real gin.Engine.
func BenchmarkAPI_Abort(b *testing.B) {
b.ReportAllocs()
engine := newBenchEngine(func(c *gin.Context) {
Abort(c, CodeForbidden)
})
req := httptest.NewRequest(http.MethodGet, "/", nil)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
engine.ServeHTTP(w, req)
}
}
// BenchmarkAPI_Success_Parallel measures aggregate in-process throughput of
// Success under GOMAXPROCS concurrent goroutines. The reported ns/op is
// wall_time / total_iterations, so `1e9 / ns_per_op` is the aggregate req/s.
func BenchmarkAPI_Success_Parallel(b *testing.B) {
b.ReportAllocs()
payload := map[string]any{"id": 1, "name": "alice"}
engine := newBenchEngine(func(c *gin.Context) {
Success(c, http.StatusOK, payload)
})
req := httptest.NewRequest(http.MethodGet, "/", nil)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
w := httptest.NewRecorder()
engine.ServeHTTP(w, req)
}
})
}
// BenchmarkAPI_Error_Parallel measures aggregate in-process throughput of
// Error under GOMAXPROCS concurrent goroutines.
func BenchmarkAPI_Error_Parallel(b *testing.B) {
b.ReportAllocs()
engine := newBenchEngine(func(c *gin.Context) {
Error(c, CodeNotFound)
})
req := httptest.NewRequest(http.MethodGet, "/", nil)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
w := httptest.NewRecorder()
engine.ServeHTTP(w, req)
}
})
}
// BenchmarkAPI_Abort_Parallel measures aggregate in-process throughput of
// Abort under GOMAXPROCS concurrent goroutines.
func BenchmarkAPI_Abort_Parallel(b *testing.B) {
b.ReportAllocs()
engine := newBenchEngine(func(c *gin.Context) {
Abort(c, CodeForbidden)
})
req := httptest.NewRequest(http.MethodGet, "/", nil)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
w := httptest.NewRecorder()
engine.ServeHTTP(w, req)
}
})
}
// BenchmarkResponseBody_BuilderChain measures the cost of the fluent
// builder API over a typical envelope.
func BenchmarkResponseBody_BuilderChain(b *testing.B) {
b.ReportAllocs()
pg := OffsetPaginationResponse{Limit: 10, Offset: 0, Total: 100}
errResp := ErrorResponse{Code: 1, Message: "e"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = NewResponseBody().
SetData("data").
SetPagination(pg).
AddError(errResp)
}
}
// BenchmarkPagination_OffsetValidate benchmarks Validate on the happy path.
func BenchmarkPagination_OffsetValidate(b *testing.B) {
b.ReportAllocs()
r := OffsetPaginationRequest{Limit: 10, Offset: 20}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = r.Validate()
}
}
// BenchmarkPagination_PageValidate benchmarks PagePaginationRequest.Validate.
func BenchmarkPagination_PageValidate(b *testing.B) {
b.ReportAllocs()
r := PagePaginationRequest{PageSize: 10, PageNumber: 0}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = r.Validate()
}
}
// BenchmarkPagination_CursorValidate benchmarks CursorPaginationRequest.Validate.
func BenchmarkPagination_CursorValidate(b *testing.B) {
b.ReportAllocs()
r := CursorPaginationRequest{Cursor: "abc", Limit: 10}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = r.Validate()
}
}
// BenchmarkDefault_Load benchmarks the atomic.Pointer read path that backs
// every top-level package wrapper.
func BenchmarkDefault_Load(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = Default()
}
})
}