Skip to content

Commit 6cdc1e0

Browse files
committed
Add CPU profiling option
1 parent 96399cf commit 6cdc1e0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

cmd/mc-router/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net"
1010
"os"
1111
"os/signal"
12+
"runtime/pprof"
1213
"strconv"
1314
"strings"
1415
)
@@ -20,6 +21,7 @@ var (
2021
versionFlag = flag.Bool("version", false, "Output version and exit")
2122
kubeConfigFile = flag.String("kube-config", "", "The path to a kubernetes configuration file")
2223
inKubeCluster = flag.Bool("in-kube-cluster", false, "Use in-cluster kubernetes config")
24+
cpuProfile = flag.String("cpu-profile", "", "Enables CPU profiling and writes to given path")
2325
)
2426

2527
var (
@@ -40,6 +42,20 @@ func main() {
4042
os.Exit(0)
4143
}
4244

45+
if *cpuProfile != "" {
46+
cpuProfileFile, err := os.Create(*cpuProfile)
47+
if err != nil {
48+
logrus.WithError(err).Fatal("trying to create cpu profile file")
49+
}
50+
51+
logrus.Info("Starting cpu profiling")
52+
err = pprof.StartCPUProfile(cpuProfileFile)
53+
if err != nil {
54+
logrus.WithError(err).Fatal("trying to start cpu profile")
55+
}
56+
defer pprof.StopCPUProfile()
57+
}
58+
4359
ctx, cancel := context.WithCancel(context.Background())
4460

4561
c := make(chan os.Signal, 1)

0 commit comments

Comments
 (0)