feat(cmd/sointu-track): add command line parameters for cpu & mem profiling

This commit is contained in:
5684185+vsariola@users.noreply.github.com 2023-10-15 09:06:26 +03:00
parent b455ef0f3c
commit 1a256b1f01

View File

@ -3,7 +3,10 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"runtime/pprof"
"gioui.org/app"
"github.com/vsariola/sointu/cmd"
@ -23,8 +26,22 @@ func (NullContext) BPM() (bpm float64, ok bool) {
return 0, false
}
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
var memprofile = flag.String("memprofile", "", "write memory profile to `file`")
func main() {
flag.Parse()
if *cpuprofile != "" {
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()
}
audioContext, err := oto.NewContext()
if err != nil {
fmt.Println(err)
@ -51,6 +68,17 @@ func main() {
}()
go func() {
tracker.Main()
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close() // error handling omitted for example
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
os.Exit(0)
}()
app.Main()