Skip to content
Snippets Groups Projects
Commit 411b9330 authored by Kamil Trzcinski's avatar Kamil Trzcinski
Browse files

Add CPU profile option

parent 46c91b57
No related branches found
No related tags found
No related merge requests found
package cli_helpers
import (
"os"
"runtime/pprof"
"github.com/codegangsta/cli"
)
func SetupCpuProfile(app *cli.App) {
app.Flags = append(app.Flags, cli.StringFlag{
Name: "cpuprofile",
Usage: "write cpu profile to file",
EnvVar: "CPU_PROFILE",
})
appBefore := app.Before
appAfter := app.After
app.Before = func(c *cli.Context) error {
if cpuProfile := c.String("cpuprofile"); cpuProfile != "" {
f, err := os.Create(cpuProfile)
if err != nil {
return err
}
pprof.StartCPUProfile(f)
}
if appBefore != nil {
return appBefore(c)
} else {
return nil
}
}
app.After = func(c *cli.Context) error {
pprof.StopCPUProfile()
if appAfter != nil {
return appAfter(c)
} else {
return nil
}
}
}
...@@ -60,6 +60,7 @@ func main() { ...@@ -60,6 +60,7 @@ func main() {
}, },
} }
cli_helpers.SetupLogLevelOptions(app) cli_helpers.SetupLogLevelOptions(app)
cli_helpers.SetupCpuProfile(app)
app.Commands = common.GetCommands() app.Commands = common.GetCommands()
app.CommandNotFound = func(context *cli.Context, command string) { app.CommandNotFound = func(context *cli.Context, command string) {
logrus.Fatalln("Command", command, "not found.") logrus.Fatalln("Command", command, "not found.")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment