• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / ark-pixel / 13720364560

07 Mar 2025 12:09PM CUT coverage: 47.374% (+12.7%) from 34.646%
13720364560

push

github

web-flow
Add systems and performance monitor (#6)

162 of 190 new or added lines in 2 files covered. (85.26%)

451 of 952 relevant lines covered (47.37%)

94.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/plot/perf_stats.go
1
package plot
2

3
import (
4
        "fmt"
5
        "image/color"
6
        "time"
7

8
        px "github.com/gopxl/pixel/v2"
9
        "github.com/gopxl/pixel/v2/backends/opengl"
10
        "github.com/gopxl/pixel/v2/ext/imdraw"
11
        "github.com/gopxl/pixel/v2/ext/text"
12
        "github.com/mlange-42/ark/ecs"
13
)
14

15
// PerfStats drawer for performance statistics.
16
//
17
// Adds an overlay with performance statistics in the top left corner of the window.
18
type PerfStats struct {
19
        SampleInterval time.Duration // Approx. time between measurements. Optional, default 1 second.
20
        drawer         imdraw.IMDraw
21
        stats          tempStats
22
        summary        *text.Text
23
        frameTimer     frameTimer
24
        startTime      time.Time
25
        lastPlotUpdate time.Time
26
        step           int64
27
}
28

29
// Initialize the system
30
func (p *PerfStats) Initialize(w *ecs.World, win *opengl.Window) {
1✔
31
        if p.SampleInterval <= 0 {
2✔
32
                p.SampleInterval = time.Second
1✔
33
        }
1✔
34
        p.lastPlotUpdate = time.Now()
1✔
35
        p.startTime = p.lastPlotUpdate
1✔
36

1✔
37
        p.drawer = *imdraw.New(nil)
1✔
38

1✔
39
        p.summary = text.New(px.V(0, 0), defaultFont)
1✔
40
        p.summary.AlignedTo(px.BottomRight)
1✔
41

1✔
42
        p.step = 0
1✔
43

1✔
44
        st := w.Stats()
1✔
45
        p.stats.Entities = st.Entities.Used
1✔
46
        p.stats.Mem = st.Memory
1✔
47
}
48

49
// Update the drawer.
50
func (p *PerfStats) Update(w *ecs.World) {
100✔
51
        t := time.Now()
100✔
52
        p.frameTimer.Update(p.step, t)
100✔
53

100✔
54
        if t.Sub(p.lastPlotUpdate) >= p.SampleInterval {
103✔
55
                st := w.Stats()
3✔
56
                p.stats.Entities = st.Entities.Used
3✔
57
                p.stats.Mem = st.Memory
3✔
58
                p.lastPlotUpdate = t
3✔
59
        }
3✔
60

61
        p.step++
100✔
62
}
63

64
// UpdateInputs handles input events of the previous frame update.
65
func (p *PerfStats) UpdateInputs(w *ecs.World, win *opengl.Window) {}
100✔
66

67
// Draw the system
68
func (p *PerfStats) Draw(w *ecs.World, win *opengl.Window) {
100✔
69
        p.summary.Clear()
100✔
70
        mem, units := toMemText(p.stats.Mem)
100✔
71
        fmt.Fprintf(
100✔
72
                p.summary, "Tick: %7d\nEnt.: %7d\nTPS: %8.1f\nTPT: %6.2fms\nMem: %6.1f%s\nTime: %7s",
100✔
73
                p.step, p.stats.Entities, p.frameTimer.FPS(),
100✔
74
                float64(p.frameTimer.FrameTime().Microseconds())/1000,
100✔
75
                mem, units, time.Since(p.startTime).Round(time.Second),
100✔
76
        )
100✔
77

100✔
78
        dr := &p.drawer
100✔
79
        height := win.Canvas().Bounds().H()
100✔
80
        x0 := 10.0
100✔
81
        y0 := height - 10.0
100✔
82

100✔
83
        v1 := px.V(x0+p.summary.Bounds().Min.X-5, y0+p.summary.Bounds().Min.Y-12)
100✔
84
        v2 := px.V(x0+p.summary.Bounds().Max.X+5, y0+p.summary.Bounds().Max.Y-8)
100✔
85

100✔
86
        dr.Color = color.Black
100✔
87
        dr.Push(v1, v2)
100✔
88
        dr.Rectangle(0)
100✔
89

100✔
90
        dr.Color = color.White
100✔
91
        dr.Push(v1, v2)
100✔
92
        dr.Rectangle(1)
100✔
93

100✔
94
        dr.Draw(win)
100✔
95
        dr.Reset()
100✔
96
        dr.Clear()
100✔
97

100✔
98
        p.summary.Draw(win, px.IM.Moved(px.V(x0, y0)))
100✔
99

100✔
100
}
100✔
101

102
type tempStats struct {
103
        Entities int
104
        Mem      int
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc