diff --git a/internal/server/server.go b/internal/server/server.go index 4aa0c77..8df1cc5 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -7,6 +7,7 @@ import ( "mal/internal/config" "mal/internal/observability" "net/http" + _ "net/http/pprof" // register pprof handlers on http.DefaultServeMux "time" "github.com/gin-gonic/gin" @@ -31,6 +32,8 @@ func ProvideRouter(cfg config.Config, htmlRender render.HTMLRender, metrics *obs r.Static("/static", "./static") r.Static("/dist", "./dist") r.GET("/metrics", gin.WrapH(metrics.Handler())) + r.GET("/debug/pprof", gin.WrapH(http.DefaultServeMux)) + r.GET("/debug/pprof/*action", gin.WrapH(http.DefaultServeMux)) r.HTMLRender = htmlRender return r } diff --git a/internal/server/server_test.go b/internal/server/server_test.go index d908928..6380029 100644 --- a/internal/server/server_test.go +++ b/internal/server/server_test.go @@ -5,6 +5,7 @@ import ( "context" "io" "log" + "mal/internal/config" "mal/internal/observability" "net/http" "net/http/httptest" @@ -35,6 +36,23 @@ func TestNewHTTPServer_TimeoutsAndAddr(t *testing.T) { } } +func TestProvideRouterRegistersPprof(t *testing.T) { + gin.SetMode(gin.TestMode) + + router := ProvideRouter(config.Config{GinMode: gin.TestMode}, nil, observability.NewMetrics()) + req := httptest.NewRequestWithContext(context.Background(), http.MethodGet, "/debug/pprof/", nil) + rec := httptest.NewRecorder() + + router.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("pprof status = %d, want %d", rec.Code, http.StatusOK) + } + if !strings.Contains(rec.Body.String(), "Types of profiles available") { + t.Fatalf("pprof index missing profile list: %s", rec.Body.String()) + } +} + func TestRequestLoggerUsesMatchedRoute(t *testing.T) { gin.SetMode(gin.TestMode) diff --git a/justfile b/justfile index f383c3e..e3ffd9a 100644 --- a/justfile +++ b/justfile @@ -16,6 +16,12 @@ lint-go: test: go test ./... +bench: + go test -bench=. -benchmem -count=5 ./internal/anime/... ./integrations/jikan/... ./internal/playback/... + +bench-all: + go test -bench=. -benchmem ./... + build-go: @go build -o server ./cmd/server