From 276885298aeb8b88cdb0a78f87c5fa1be8b372ae Mon Sep 17 00:00:00 2001 From: Nick Thomas Date: Thu, 19 Mar 2020 18:36:20 +0000 Subject: [PATCH] Allow ebiten.Run execution to be profiled --- internal/ui/window.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/internal/ui/window.go b/internal/ui/window.go index 834fa60..35337ca 100644 --- a/internal/ui/window.go +++ b/internal/ui/window.go @@ -3,6 +3,9 @@ package ui import ( "flag" "fmt" + "log" + "os" + "runtime/pprof" "github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten/ebitenutil" @@ -12,6 +15,8 @@ import ( var ( winX = flag.Int("win-x", 1280, "width of the view-map window") winY = flag.Int("win-y", 1024, "height of the view-map window") + + cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`") ) type Window struct { @@ -91,5 +96,17 @@ func (w *Window) Run(updateFn func() error, drawFn func(*ebiten.Image) error) er w.updateFn = updateFn w.drawFn = drawFn + 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() + } + return ebiten.Run(w.run, *winX, *winY, 1, w.Title) }