GOingTunneling/profiler.go

65 lines
2.3 KiB
Go
Raw Normal View History

2024-08-29 18:48:27 +02:00
package main
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
)
func profileSection(section func()) uint64 {
start := sdl.GetTicks64()
section()
end := sdl.GetTicks64()
return end - start
}
func logProfilingInfo(handleEventsTime, renderTime, scaleTime, frameTime, frameCount uint64) {
floatFrame := float64(frameCount)
fmt.Printf("Average handleEvents time: %f ms\n", float64(handleEventsTime)/floatFrame)
fmt.Printf("Average render time: %f ms\n", float64(renderTime)/floatFrame)
fmt.Printf("Average scaling time: %f ms\n", float64(scaleTime)/floatFrame)
fmt.Printf("Average frame time: %f ms\n", float64(frameTime)/floatFrame)
2024-08-31 15:35:19 +02:00
2024-08-29 18:48:27 +02:00
fmt.Print("\n")
}
2024-08-31 15:35:19 +02:00
func logMapProfilingInfo(renderTime, scaleTime, frameTime, totalGameLogicCatchUp, totalNormalGameLogic, totalTicking, totalRemotePlayerUpdate, tickCount, frameCount uint64) {
2024-08-29 21:12:18 +02:00
floatFrame := float64(frameCount)
2024-08-31 15:35:19 +02:00
floatTick := float64(tickCount)
2024-08-29 21:12:18 +02:00
fmt.Printf("Average map render time: %f ms\n", float64(renderTime)/floatFrame)
fmt.Printf("Average map scaling time: %f ms\n", float64(scaleTime)/floatFrame)
fmt.Printf("Average full render time: %f ms\n", float64(frameTime)/floatFrame)
2024-08-31 15:35:19 +02:00
fmt.Printf("Average total gamelogic catching up time: %f ms\n", float64(totalGameLogicCatchUp)/floatFrame)
fmt.Printf("Average normal gamelogic time: %f ms\n", float64(totalNormalGameLogic)/floatFrame)
fmt.Printf("Average ticking time: %f ms\n", float64(totalTicking)/floatTick)
fmt.Printf("Average remote player update time: %f ms\n", float64(totalRemotePlayerUpdate)/floatTick)
2024-08-29 21:12:18 +02:00
fmt.Print("\n")
}
2024-08-29 18:48:27 +02:00
func resetProfilingCounters(handleEventsTime, renderTime, scaleTime, frameTime *uint64, frameCount *uint64) {
*handleEventsTime = 0
*renderTime = 0
*scaleTime = 0
*frameTime = 0
*frameCount = 0
}
2024-08-31 15:35:19 +02:00
func resetMapProfilingCounters(renderTime, scaleTime, frameTime, totalGameLogicCatchUp, totalNormalGameLogic, totalTicking, totalRemotePlayerUpdate *uint64, frameCount, tickCount *uint64) {
2024-08-29 21:12:18 +02:00
*renderTime = 0
*scaleTime = 0
*frameTime = 0
*frameCount = 0
2024-08-31 15:35:19 +02:00
*totalGameLogicCatchUp = 0
*totalNormalGameLogic = 0
*totalTicking = 0
*totalRemotePlayerUpdate = 0
*tickCount = 0
2024-08-29 21:12:18 +02:00
}
2024-08-29 18:48:27 +02:00
func enforceFrameRate(frameStart uint64, targetFPS int) {
frameDuration := 1000 / targetFPS
elapsed := sdl.GetTicks64() - frameStart
if delay := frameDuration - int(elapsed); delay > 0 {
sdl.Delay(uint32(delay))
}
}