CodingLand/pkg/internal/land/prof.go
2024-07-18 17:21:25 +08:00

40 lines
779 B
Go

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) RunResetter(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)
}