You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.4 KiB
72 lines
1.4 KiB
//go:build example |
|
// +build example |
|
|
|
package system |
|
|
|
import ( |
|
"log" |
|
"os" |
|
"path" |
|
"runtime" |
|
"runtime/pprof" |
|
|
|
"code.rocketnine.space/tslocum/gohan" |
|
"code.rocketnine.space/tslocum/gohan/examples/twinstick/component" |
|
"github.com/hajimehoshi/ebiten/v2" |
|
"github.com/hajimehoshi/ebiten/v2/inpututil" |
|
) |
|
|
|
type profileSystem struct { |
|
player gohan.Entity |
|
cpuProfile *os.File |
|
} |
|
|
|
func NewProfileSystem(player gohan.Entity) *profileSystem { |
|
return &profileSystem{ |
|
player: player, |
|
} |
|
} |
|
|
|
func (s *profileSystem) Components() []gohan.ComponentID { |
|
return []gohan.ComponentID{ |
|
component.WeaponComponentID, |
|
} |
|
} |
|
|
|
func (s *profileSystem) Update(ctx *gohan.Context) error { |
|
if ebiten.IsKeyPressed(ebiten.KeyControl) && inpututil.IsKeyJustPressed(ebiten.KeyP) { |
|
if s.cpuProfile == nil { |
|
log.Println("CPU profiling started...") |
|
|
|
runtime.SetCPUProfileRate(0) |
|
runtime.SetCPUProfileRate(1000) |
|
|
|
homeDir, err := os.UserHomeDir() |
|
if err != nil { |
|
return err |
|
} |
|
|
|
s.cpuProfile, err = os.Create(path.Join(homeDir, "gohan.prof")) |
|
if err != nil { |
|
return err |
|
} |
|
|
|
err = pprof.StartCPUProfile(s.cpuProfile) |
|
if err != nil { |
|
return err |
|
} |
|
} else { |
|
pprof.StopCPUProfile() |
|
|
|
s.cpuProfile.Close() |
|
s.cpuProfile = nil |
|
|
|
log.Println("CPU profiling stopped") |
|
} |
|
} |
|
return nil |
|
} |
|
|
|
func (s *profileSystem) Draw(ctx *gohan.Context, _ *ebiten.Image) error { |
|
return gohan.ErrSystemWithoutDraw |
|
}
|
|
|