-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_monitor.go
More file actions
137 lines (116 loc) · 2.67 KB
/
performance_monitor.go
File metadata and controls
137 lines (116 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"log"
"os"
"runtime"
"time"
"gocraft/platform"
"sync/atomic"
rl "github.com/gen2brain/raylib-go/raylib"
)
type PerformanceMonitor struct {
file *os.File
updateTicker *time.Ticker
Metrics PerfMetrics
chunksMeshed int
chunksLoaded int
chunksUnloaded int
startTime time.Time
}
type PerfMetrics struct {
FPS int32
FrameTime float32
HeapAllocMB uint64
NumGC uint32
Goroutines int
MeshesPerSec int
ChunksPerSec int
UnloadsPerSec int
ActiveMeshes int64
}
func NewPerformanceMonitor() *PerformanceMonitor {
f, err := os.Create("performance.log")
if err != nil {
log.Printf("Failed to create performance log: %v", err)
return nil
}
// Write CSV Header
_, _ = f.WriteString("Timestamp,FPS,FrameTime(ms),HeapAlloc(MB),Goroutines,MeshesBuilt/s,ChunksLoaded/s,ChunksUnloaded/s,ActiveMeshes\n")
pm := &PerformanceMonitor{
file: f,
updateTicker: time.NewTicker(1 * time.Second), // Log every second
startTime: time.Now(),
}
return pm
}
func (pm *PerformanceMonitor) Close() {
if pm.file != nil {
pm.file.Close()
}
pm.updateTicker.Stop()
}
func (pm *PerformanceMonitor) IncrementMeshBuild() {
if pm == nil {
return
}
pm.chunksMeshed++
}
func (pm *PerformanceMonitor) IncrementChunkLoad() {
if pm == nil {
return
}
pm.chunksLoaded++
}
func (pm *PerformanceMonitor) IncrementChunkUnload() {
if pm == nil {
return
}
pm.chunksUnloaded++
}
func (pm *PerformanceMonitor) Update() {
if pm == nil || pm.file == nil {
return
}
select {
case <-pm.updateTicker.C:
pm.logMetrics()
default:
// Do nothing
}
}
func (pm *PerformanceMonitor) logMetrics() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
if rl.IsWindowReady() {
pm.Metrics.FPS = rl.GetFPS()
pm.Metrics.FrameTime = rl.GetFrameTime() * 1000.0 // ms
}
pm.Metrics.HeapAllocMB = m.HeapAlloc / 1024 / 1024
pm.Metrics.NumGC = m.NumGC
pm.Metrics.Goroutines = runtime.NumGoroutine()
pm.Metrics.MeshesPerSec = pm.chunksMeshed
pm.Metrics.ChunksPerSec = pm.chunksLoaded
pm.Metrics.UnloadsPerSec = pm.chunksUnloaded
pm.Metrics.ActiveMeshes = atomic.LoadInt64(&platform.ActiveMeshCount)
// Reset counters
pm.chunksMeshed = 0
pm.chunksLoaded = 0
pm.chunksUnloaded = 0
timestamp := time.Since(pm.startTime).Seconds()
line := fmt.Sprintf("%.2f,%d,%.2f,%d,%d,%d,%d,%d,%d\n",
timestamp,
pm.Metrics.FPS,
pm.Metrics.FrameTime,
pm.Metrics.HeapAllocMB,
pm.Metrics.Goroutines,
pm.Metrics.MeshesPerSec,
pm.Metrics.ChunksPerSec,
pm.Metrics.UnloadsPerSec,
pm.Metrics.ActiveMeshes,
)
_, err := pm.file.WriteString(line)
if err != nil {
log.Printf("Error writing to perf log: %v", err)
}
}