feature/add-profiler (#42)

This commit is contained in:
T. von Dein
2026-06-18 08:54:00 +02:00
parent 5d345aaa9a
commit 802217158c
4 changed files with 37 additions and 3 deletions

2
.gitignore vendored
View File

@@ -30,3 +30,5 @@ esctl
*.sh
*.json
*.log
cpu.profile

View File

@@ -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

View File

@@ -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
},
}

View File

@@ -54,7 +54,8 @@ type Config struct {
Output string // -o <mode>
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