CodingLand/pkg/internal/land/prof.go

40 lines
781 B
Go
Raw Normal View History

2024-07-18 06:47:41 +00:00
package land
import (
"log"
"sync/atomic"
"time"
)
type PerformanceAnalyzer struct {
tickCount int64
drawCount int64
}
func (p *PerformanceAnalyzer) Tick() {
atomic.AddInt64(&p.tickCount, 1)
}
func (p *PerformanceAnalyzer) Draw() {
atomic.AddInt64(&p.drawCount, 1)
}
func (p *PerformanceAnalyzer) KeepResetting(duration time.Duration) {
ticker := time.NewTicker(duration)
defer ticker.Stop()
for {
<-ticker.C
log.Printf("TPS: %d FPS: %d\n", atomic.LoadInt64(&p.tickCount), atomic.LoadInt64(&p.drawCount))
atomic.StoreInt64(&p.tickCount, 0)
atomic.StoreInt64(&p.drawCount, 0)
}
}
func (p *PerformanceAnalyzer) GetTPS() int64 {
return atomic.LoadInt64(&p.tickCount)
}
func (p *PerformanceAnalyzer) GetFPS() int64 {
return atomic.LoadInt64(&p.drawCount)
}