Fix pooled params correctness and add context copy benchmark

This commit is contained in:
2025-11-25 15:59:31 +01:00
parent 3b94964068
commit 788a736e06
2 changed files with 145 additions and 89 deletions

View File

@@ -29,7 +29,7 @@ func BenchmarkParameterOperations(b *testing.B) {
// BenchmarkPathParsing compares old vs new path parsing
func BenchmarkPathParsingOld(b *testing.B) {
path := "/users/123/posts/456/comments/789"
b.ResetTimer()
b.ReportAllocs()
@@ -40,7 +40,7 @@ func BenchmarkPathParsingOld(b *testing.B) {
func BenchmarkPathParsingNew(b *testing.B) {
path := "/users/123/posts/456/comments/789"
b.ResetTimer()
b.ReportAllocs()
@@ -197,4 +197,24 @@ func BenchmarkPoolEfficiency(b *testing.B) {
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
}
}
}
// BenchmarkContextParamCopy measures overhead of copying params into context
func BenchmarkContextParamCopy(b *testing.B) {
router := New()
router.GET("/users/:id/posts/:postId", func(w http.ResponseWriter, r *http.Request) {
params := ParamsFromContext(r)
_ = params.Get("id") + params.Get("postId")
w.WriteHeader(http.StatusOK)
})
req := httptest.NewRequest("GET", "/users/123/posts/456", nil)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
}
}