support for parameters, middleware, and route groups.
This commit is contained in:
212
benchmark_test.go
Normal file
212
benchmark_test.go
Normal file
@@ -0,0 +1,212 @@
|
||||
package sux
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// BenchmarkStaticRoute benchmarks basic static route performance
|
||||
func BenchmarkStaticRoute(b *testing.B) {
|
||||
router := New()
|
||||
|
||||
router.GET("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("home"))
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkParameterRoute benchmarks parameter route performance
|
||||
func BenchmarkParameterRoute(b *testing.B) {
|
||||
router := New()
|
||||
|
||||
router.GET("/users/:id", func(w http.ResponseWriter, r *http.Request) {
|
||||
params := ParamsFromContext(r)
|
||||
_ = params["id"] // Use the parameter
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("GET", "/users/123", nil)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkWildcardRoute benchmarks wildcard route performance
|
||||
func BenchmarkWildcardRoute(b *testing.B) {
|
||||
router := New()
|
||||
|
||||
router.GET("/files/*path", func(w http.ResponseWriter, r *http.Request) {
|
||||
params := ParamsFromContext(r)
|
||||
_ = params["path"] // Use the parameter
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("GET", "/files/docs/readme.txt", nil)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkMultipleParameters benchmarks multiple parameter route performance
|
||||
func BenchmarkMultipleParameters(b *testing.B) {
|
||||
router := New()
|
||||
|
||||
router.GET("/users/:userId/posts/:postId/comments/:commentId", func(w http.ResponseWriter, r *http.Request) {
|
||||
params := ParamsFromContext(r)
|
||||
_ = params["userId"] + params["postId"] + params["commentId"] // Use parameters
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("GET", "/users/123/posts/456/comments/789", nil)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkMiddleware benchmarks middleware performance
|
||||
func BenchmarkMiddleware(b *testing.B) {
|
||||
router := New()
|
||||
|
||||
// Add multiple middleware layers
|
||||
router.Use(func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Middleware-1", "1")
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
})
|
||||
|
||||
router.Use(func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Middleware-2", "2")
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
})
|
||||
|
||||
router.GET("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("GET", "/", nil)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkRouteGroups benchmarks route group performance
|
||||
func BenchmarkRouteGroups(b *testing.B) {
|
||||
router := New()
|
||||
|
||||
api := router.Group("/api", func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-API-Version", "v1")
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
})
|
||||
|
||||
api.GET("/users", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("GET", "/api/users", nil)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkNotFound benchmarks 404 performance
|
||||
func BenchmarkNotFound(b *testing.B) {
|
||||
router := New()
|
||||
|
||||
router.GET("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("GET", "/nonexistent", nil)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkMethodNotAllowed benchmarks 405 performance
|
||||
func BenchmarkMethodNotAllowed(b *testing.B) {
|
||||
router := New()
|
||||
|
||||
router.GET("/resource", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
|
||||
req := httptest.NewRequest("POST", "/resource", nil)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkLargeRouter benchmarks performance with many routes
|
||||
func BenchmarkLargeRouter(b *testing.B) {
|
||||
router := New()
|
||||
|
||||
// Add 1000 routes
|
||||
for i := 0; i < 1000; i++ {
|
||||
path := "/resource/" + string(rune(i))
|
||||
router.GET(path, func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
})
|
||||
}
|
||||
|
||||
// Test the last route
|
||||
req := httptest.NewRequest("GET", "/resource/999", nil)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user