mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-14 16:07:42 +00:00
add a profile option to the commands
This commit is contained in:
parent
893e5bd5a3
commit
26ca63f31b
2 changed files with 23 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
tmp/
|
tmp/
|
||||||
.env
|
.env
|
||||||
dump.rdb
|
dump.rdb
|
||||||
|
cpu.pprof
|
||||||
|
|
|
@ -2,6 +2,8 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"os"
|
||||||
|
"runtime/pprof"
|
||||||
|
|
||||||
"github.com/DataDog/datadog-go/statsd"
|
"github.com/DataDog/datadog-go/statsd"
|
||||||
"github.com/adjust/rmq/v4"
|
"github.com/adjust/rmq/v4"
|
||||||
|
@ -25,11 +27,31 @@ type Command struct {
|
||||||
func Execute(ctx context.Context) int {
|
func Execute(ctx context.Context) int {
|
||||||
_ = godotenv.Load()
|
_ = godotenv.Load()
|
||||||
|
|
||||||
|
profile := false
|
||||||
|
|
||||||
rootCmd := &cobra.Command{
|
rootCmd := &cobra.Command{
|
||||||
Use: "apollo",
|
Use: "apollo",
|
||||||
Short: "Apollo is an amazing Reddit client for iOS. This isn't it, but it helps.",
|
Short: "Apollo is an amazing Reddit client for iOS. This isn't it, but it helps.",
|
||||||
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if !profile {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
f, perr := os.Create("cpu.pprof")
|
||||||
|
if perr != nil {
|
||||||
|
return perr
|
||||||
|
}
|
||||||
|
pprof.StartCPUProfile(f)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
||||||
|
if profile {
|
||||||
|
pprof.StopCPUProfile()
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rootCmd.PersistentFlags().BoolVarP(&profile, "profile", "p", false, "record CPU pprof")
|
||||||
|
|
||||||
rootCmd.AddCommand(APICmd(ctx))
|
rootCmd.AddCommand(APICmd(ctx))
|
||||||
rootCmd.AddCommand(SchedulerCmd(ctx))
|
rootCmd.AddCommand(SchedulerCmd(ctx))
|
||||||
rootCmd.AddCommand(WorkerCmd(ctx))
|
rootCmd.AddCommand(WorkerCmd(ctx))
|
||||||
|
|
Loading…
Reference in a new issue