Allow ebiten.Run execution to be profiled

This commit is contained in:
2020-03-19 18:36:20 +00:00
parent 73553cb8b0
commit 276885298a

View File

@@ -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)
}