Add CPU/Memory profiling flags

This commit is contained in:
2024-02-13 10:44:42 -06:00
parent e6283aefae
commit 85a3696631
2 changed files with 36 additions and 1 deletions

3
.gitignore vendored
View File

@@ -4,4 +4,5 @@ banner
.*.go
dumps/
js/
.vscode/
.vscode/
*.prof

34
main.go
View File

@@ -2,11 +2,15 @@ package main
import (
"context"
"flag"
"fmt"
"net/http"
"net/http/cookiejar"
_ "net/http/pprof"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"syscall"
"time"
_ "time/tzdata"
@@ -31,6 +35,8 @@ var (
environment string
CentralTimeLocation *time.Location
isClosing bool = false
cpuProfile = flag.String("cpuprofile", "", "write cpu profile to `file`")
memoryProfile = flag.String("memprofile", "", "write memory profile to `file`")
)
const (
@@ -129,6 +135,21 @@ func initRedis() {
}
func main() {
flag.Parse()
// CPU Profiling (if requested)
if *cpuProfile != "" {
f, err := os.Create(*cpuProfile)
if err != nil {
log.Fatal().Stack().Err(err).Msg("could not create CPU profile")
}
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal().Stack().Err(err).Msg("could not start CPU profile")
}
defer pprof.StopCPUProfile()
}
initRedis()
// Create cookie jar
@@ -303,6 +324,19 @@ func main() {
isClosing = true
// Write memory profile if requested
if *memoryProfile != "" {
f, err := os.Create(*memoryProfile)
if err != nil {
log.Fatal().Stack().Err(err).Msg("could not create memory profile")
}
defer f.Close() // error handling omitted for example
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal().Stack().Err(err).Msg("could not write memory profile")
}
}
// Defers are called after this
log.Warn().Str("signal", closingSignal.String()).Msg("Gracefully shutting down")
}