58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package system
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"path"
|
|
"runtime"
|
|
"runtime/pprof"
|
|
|
|
"code.rocketnine.space/tslocum/gohan"
|
|
"code.rocketnine.space/tslocum/gohan-bunnymark/component"
|
|
"github.com/hajimehoshi/ebiten/v2"
|
|
"github.com/hajimehoshi/ebiten/v2/inpututil"
|
|
)
|
|
|
|
type Profile struct {
|
|
Settings *component.Settings
|
|
|
|
cpuProfile *os.File
|
|
}
|
|
|
|
func (s *Profile) Update(_ gohan.Entity) error {
|
|
if inpututil.IsKeyJustPressed(ebiten.KeyP) {
|
|
if s.cpuProfile != nil {
|
|
pprof.StopCPUProfile()
|
|
s.cpuProfile.Close()
|
|
s.cpuProfile = nil
|
|
|
|
log.Println("Stopped profiling")
|
|
} else {
|
|
log.Println("Starting profiling...")
|
|
|
|
runtime.SetCPUProfileRate(10000)
|
|
|
|
homeDir, err := os.UserHomeDir()
|
|
if err != nil {
|
|
return fmt.Errorf("failed to determine home directory: %s", err)
|
|
}
|
|
|
|
s.cpuProfile, err = os.Create(path.Join(homeDir, "gohan.prof"))
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create cpu profile: %s", err)
|
|
}
|
|
|
|
err = pprof.StartCPUProfile(s.cpuProfile)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to start cpu profile: %s", err)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Profile) Draw(_ gohan.Entity, _ *ebiten.Image) error {
|
|
return gohan.ErrUnregister
|
|
}
|