简体   繁体   中英

Can I run something under pprof and pass it command line arguments?

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

  • creating another folder at top level which I added to .gitignore
  • created package main with a func main()

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

package main

import (
    "github.com/mutl3y/PRTG_VMware/VMware"
    "github.com/pkg/profile"
    "log"
    "net/url"
    "time"
)

func main() {
    defer profile.Start().Stop()
    u, err := url.Parse("https://192.168.59.4/sdk")
    if err != nil {
        log.Fatalf("failed to parse url")
    }
    c, err := VMware.NewClient(u, "prtg@heynes.local", ".l3tm31n", 2*time.Second)
    if err != nil {
        log.Fatalf("failed %v", err)
    }

    err = c.VmSummary("", "vm-13", &VMware.LimitsStruct{}, time.Second, true,[]string{})
    if err != nil {
        log.Fatalf("vmSummaryfailed %v", err)

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM