From 0386f441d632a43cb2746bdb9dbf33467488200b Mon Sep 17 00:00:00 2001 From: bloeys Date: Mon, 6 May 2024 22:16:20 +0400 Subject: [PATCH] Profiling --- .gitignore | 5 ++++- main.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d19cbbc..8b8c111 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,7 @@ vendor/ .vscode/ imgui.ini -*~ \ No newline at end of file +*~ + +# Custom +*.pprod \ No newline at end of file diff --git a/main.go b/main.go index b363b24..6ef48ec 100755 --- a/main.go +++ b/main.go @@ -2,7 +2,9 @@ package main import ( "fmt" + "os" "runtime" + "runtime/pprof" "strconv" imgui "github.com/AllenDang/cimgui-go" @@ -188,6 +190,8 @@ const ( unscaledWindowWidth = 1280 unscaledWindowHeight = 720 + + PROFILE_CPU = true ) var ( @@ -346,7 +350,36 @@ func main() { } window.EventCallbacks = append(window.EventCallbacks, game.handleWindowEvents) + if PROFILE_CPU { + + pf, err := os.Create("cpu.pprof") + if err == nil { + defer pf.Close() + pprof.StartCPUProfile(pf) + } else { + logging.ErrLog.Printf("Creating cpu.pprof file failed. CPU profiling will not run. Err=%v\n", err) + } + } + engine.Run(game, &window, game.ImGUIInfo) + + if PROFILE_CPU { + pprof.StopCPUProfile() + + heapProfile, err := os.Create("heap.pprof") + if err == nil { + + err = pprof.WriteHeapProfile(heapProfile) + if err != nil { + logging.ErrLog.Printf("Writing heap profile to heap.pprof failed. Err=%v\n", err) + } + + heapProfile.Close() + + } else { + logging.ErrLog.Printf("Creating heap.pprof file failed. Err=%v\n", err) + } + } } func (g *Game) handleWindowEvents(e sdl.Event) {