55 lines
850 B
Go
55 lines
850 B
Go
|
package road
|
||
|
|
||
|
import (
|
||
|
"go.uber.org/zap"
|
||
|
"sync"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type Monitor interface {
|
||
|
Increment()
|
||
|
Start()
|
||
|
}
|
||
|
|
||
|
type DefaultMonitor struct {
|
||
|
muCounter sync.Mutex
|
||
|
counter uint
|
||
|
}
|
||
|
|
||
|
func (d *DefaultMonitor) Increment() {
|
||
|
d.muCounter.Lock()
|
||
|
defer d.muCounter.Unlock()
|
||
|
d.counter = d.counter + 1
|
||
|
}
|
||
|
|
||
|
func (d *DefaultMonitor) Start() {
|
||
|
start := time.Now().UnixNano()
|
||
|
for {
|
||
|
select {
|
||
|
case <-time.After(10 * time.Second):
|
||
|
start = d.displayFPS(start)
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func (d *DefaultMonitor) displayFPS(start int64) int64 {
|
||
|
d.muCounter.Lock()
|
||
|
defer d.muCounter.Unlock()
|
||
|
|
||
|
end := time.Now().UnixNano()
|
||
|
count := int64(d.counter)
|
||
|
go func() {
|
||
|
zap.S().Infof("Current FPS: %d img/s", count/((end-start)/1_000_000_000))
|
||
|
}()
|
||
|
d.counter = 0
|
||
|
return end
|
||
|
}
|
||
|
|
||
|
type FakeMonitor struct{}
|
||
|
|
||
|
func (f *FakeMonitor) Increment() {}
|
||
|
|
||
|
func (f *FakeMonitor) Start() {}
|