feat: profiling

Carlos Alexandro Becker created

Change summary

Taskfile.yaml | 22 ++++++++++++++++++++++
main.go       | 14 ++++++++++++++
2 files changed, 36 insertions(+)

Detailed changes

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'

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()
 }