From 75178a44cc78d9ff7751172d1a1472ab6cacbfd6 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 4 Jun 2025 14:27:16 -0300 Subject: [PATCH] feat: profiling --- Taskfile.yaml | 22 ++++++++++++++++++++++ main.go | 14 ++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/Taskfile.yaml b/Taskfile.yaml index 285b6bf3ebda1044ffdd2dbe8925e03b73b39d19..827db449c673fd545cbb042b3bb407273d8b2872 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -17,3 +17,25 @@ tasks: desc: Run gofumpt cmds: - gofumpt -w . + + dev: + desc: Run with profiling enabled + env: + OPENCODE_PROFILE: true + cmds: + - go run . + + profile:cpu: + desc: 10s CPU profile + cmds: + - go tool pprof -http :6061 'http://localhost:6060/debug/pprof/profile?seconds=10' + + profile:heap: + desc: Heap profile + cmds: + - go tool pprof -http :6061 'http://localhost:6060/debug/pprof/heap' + + profile:allocs: + desc: Allocations profile + cmds: + - go tool pprof -http :6061 'http://localhost:6060/debug/pprof/allocs' diff --git a/main.go b/main.go index 857344ef52f4b4928e2ea0f794b8d9a1753c0616..0e95a17f366793f747e161942594710a4f4cdc48 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,11 @@ package main import ( + "net/http" + "os" + + _ "net/http/pprof" // profiling + "github.com/opencode-ai/opencode/cmd" "github.com/opencode-ai/opencode/internal/logging" ) @@ -10,5 +15,14 @@ func main() { logging.ErrorPersist("Application terminated due to unhandled panic") }) + if os.Getenv("OPENCODE_PROFILE") != "" { + go func() { + logging.Info("Serving pprof at localhost:6060") + if httpErr := http.ListenAndServe("localhost:6060", nil); httpErr != nil { + logging.Error("Failed to pprof listen: %v", httpErr) + } + }() + } + cmd.Execute() }