Profile for debugging cpu usage

This commit is contained in:
Karmanyaah Malhotra 2021-02-28 15:02:34 -05:00
parent daf1edffb7
commit 771d8c826a

18
main.go
View File

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"os" "os"
"os/signal" "os/signal"
"runtime/pprof"
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
@ -397,6 +398,8 @@ func (bridge *Bridge) Stop() {
} }
} }
var cpuprofile = flag.MakeFull("", "cpuprofile", "write cpu profile to `file`", "").String()
func (bridge *Bridge) Main() { func (bridge *Bridge) Main() {
if *generateRegistration { if *generateRegistration {
@ -411,6 +414,19 @@ func (bridge *Bridge) Main() {
bridge.Start() bridge.Start()
bridge.Log.Infoln("Bridge started!") bridge.Log.Infoln("Bridge started!")
if *cpuprofile != "" {
println("profiling")
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
c := make(chan os.Signal) c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM) signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c <-c
@ -418,7 +434,7 @@ func (bridge *Bridge) Main() {
bridge.Log.Infoln("Interrupt received, stopping...") bridge.Log.Infoln("Interrupt received, stopping...")
bridge.Stop() bridge.Stop()
bridge.Log.Infoln("Bridge stopped.") bridge.Log.Infoln("Bridge stopped.")
os.Exit(0) //os.Exit(0)
} }
func main() { func main() {