profiling options
Ted Unangst tedu@tedunangst.com
Sun, 28 Jan 2024 17:00:00 -0500
M
main.go
→
main.go
@@ -23,6 +23,7 @@ golog "log"
"log/syslog" notrand "math/rand" "os" + "runtime/pprof" "strconv" "time"@@ -72,10 +73,28 @@ fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1) } +var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file") +var memprofile = flag.String("memprofile", "", "write memory profile to this file") +var memprofilefd *os.File + func main() { flag.StringVar(&dataDir, "datadir", dataDir, "data directory") flag.StringVar(&viewDir, "viewdir", viewDir, "view directory") flag.Parse() + if *cpuprofile != "" { + f, err := os.Create(*cpuprofile) + if err != nil { + errx("can't open cpu profile: %s", err) + } + pprof.StartCPUProfile(f) + } + if *memprofile != "" { + f, err := os.Create(*memprofile) + if err != nil { + errx("can't open mem profile: %s", err) + } + memprofilefd = f + } log.Init(log.Options{Progname: "honk", Facility: syslog.LOG_UUCP}) elog = log.E
M
web.go
→
web.go
@@ -32,6 +32,7 @@ "net/url"
"os" "os/signal" "regexp" + "runtime/pprof" "sort" "strconv" "strings"@@ -2661,6 +2662,13 @@ ilog.Printf("stopping...")
listenSocket.Close() for i := 0; i < workinprogress; i++ { endoftheworld <- true + } + if *cpuprofile != "" { + pprof.StopCPUProfile() + } + if *memprofile != "" { + pprof.WriteHeapProfile(memprofilefd) + memprofilefd.Close() } ilog.Printf("waiting...") go func() {