From 6cdc1e06ac7a5446c909ab079e4a657d4c413e23 Mon Sep 17 00:00:00 2001 From: Geoff Bourne Date: Sat, 4 May 2019 10:53:05 -0500 Subject: [PATCH] Add CPU profiling option --- cmd/mc-router/main.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cmd/mc-router/main.go b/cmd/mc-router/main.go index fc9d80e..26ad861 100644 --- a/cmd/mc-router/main.go +++ b/cmd/mc-router/main.go @@ -9,6 +9,7 @@ import ( "net" "os" "os/signal" + "runtime/pprof" "strconv" "strings" ) @@ -20,6 +21,7 @@ var ( versionFlag = flag.Bool("version", false, "Output version and exit") kubeConfigFile = flag.String("kube-config", "", "The path to a kubernetes configuration file") inKubeCluster = flag.Bool("in-kube-cluster", false, "Use in-cluster kubernetes config") + cpuProfile = flag.String("cpu-profile", "", "Enables CPU profiling and writes to given path") ) var ( @@ -40,6 +42,20 @@ func main() { os.Exit(0) } + if *cpuProfile != "" { + cpuProfileFile, err := os.Create(*cpuProfile) + if err != nil { + logrus.WithError(err).Fatal("trying to create cpu profile file") + } + + logrus.Info("Starting cpu profiling") + err = pprof.StartCPUProfile(cpuProfileFile) + if err != nil { + logrus.WithError(err).Fatal("trying to start cpu profile") + } + defer pprof.StopCPUProfile() + } + ctx, cancel := context.WithCancel(context.Background()) c := make(chan os.Signal, 1)