I have a go binary, which uses cobra for subcommands. Each subcommand has it's own flags. I'd like to be able to create a CPU profile for a command with a specific set of parameters, like for example:
myBinary dryRun -c configs/config.json
However, if I try to run it, like this:
go tool pprof -pdf myBinary -- dryRun -c configs/config.json
I get the following error:
-: open --: no such file or directory
dryRun: open dryRun: no such file or directory
-c: open -c: no such file or directory
configs/config.json: parsing profile: unrecognized profile format
If I try to quote the entire command, it's also not working. Is there any way to get go tool pprof
to pass other command line arguments?
EDIT: This is how I'm trying to profile:
func main() {
defer profile.Start().Stop()
fmt.Println("running version", version, "built on", date)
fmt.Println()
cmd.Execute()
time.Sleep(2 * time.Second)
}
@Geo
Had the same issue profiling a cobra command line app with pprof
you just need to embed your flag choices in a separate main.go and call the package directly
In my case I managed to work around this by
you will need to edit the main func for each combination you want to try but you end up with a binary you can pprof without any flags
the cobra command I am profiling
https://github.com/mutl3y/PRTG_VMware/blob/dev1/cmd/summary.go
my example of profiling it
/profiling/main.go