From 802217158c50a13f9b29249a71522c1f8cea5385 Mon Sep 17 00:00:00 2001 From: "T. von Dein" Date: Thu, 18 Jun 2026 08:54:00 +0200 Subject: [PATCH] feature/add-profiler (#42) --- .gitignore | 2 ++ Makefile | 7 +++++-- cmd/root.go | 28 ++++++++++++++++++++++++++++ pkg/cfg/config.go | 3 ++- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 77d8a3e..e056ce6 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,5 @@ esctl *.sh *.json *.log + +cpu.profile diff --git a/Makefile b/Makefile index c141875..8a6c325 100644 --- a/Makefile +++ b/Makefile @@ -106,5 +106,8 @@ show-versions: buildlocal @echo "### go version used for building:" @grep -m 1 go go.mod -# lint: -# golangci-lint run -p bugs -p unused +.PHONY: profile +profile: buildlocal + ./esctl api ls --profile-file cpu.profile + go tool pprof -text esctl cpu.profile + go tool pprof --http localhost:8888 ./esctl cpu.profile diff --git a/cmd/root.go b/cmd/root.go index 84d4804..f939257 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -19,7 +19,9 @@ package cmd import ( "context" "fmt" + golog "log" "os" + "runtime/pprof" "codeberg.org/scip/esctl/pkg/cfg" "codeberg.org/scip/esctl/pkg/es" @@ -83,6 +85,12 @@ func Main() int { Usage: "output mode (tsv, markdown, json, yaml) default: tsv", Destination: &conf.Output, }, + &cli.StringFlag{ + Name: "profile-file", + Usage: "golang profiler output file", + Destination: &conf.ProfileFile, + Hidden: true, + }, }, Commands: []*cli.Command{ @@ -115,6 +123,26 @@ func Main() int { log.Init(conf) + if conf.ProfileFile != "" { + // enable cpu profiling. Do NOT use q to stop the game but + // close the window to get a profile + fd, err := os.Create(conf.ProfileFile) + if err != nil { + return nil, err + } + defer func() { + if err := fd.Close(); err != nil { + golog.Fatal(err) + } + }() + + if err := pprof.StartCPUProfile(fd); err != nil { + golog.Fatal(err) + } + + defer pprof.StopCPUProfile() + } + return nil, nil }, } diff --git a/pkg/cfg/config.go b/pkg/cfg/config.go index 26b0f8c..5994f7a 100644 --- a/pkg/cfg/config.go +++ b/pkg/cfg/config.go @@ -54,7 +54,8 @@ type Config struct { Output string // -o Clusters map[string]*Cluster DefaultCluster *Cluster - HaveJQ bool // determined at runtime by ourselfes + HaveJQ bool // determined at runtime by ourselfes + ProfileFile string // for internal use (golang profiling) Index string // index: -i Failed, Partials bool // index: flags