split statistics from renderer file

This commit is contained in:
DataHoarder 2023-12-02 10:18:07 +01:00
parent 53a96783b3
commit 4fc74b0f33
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk
2 changed files with 91 additions and 84 deletions

View file

@ -23,12 +23,6 @@ type Renderer struct {
Statistics map[uint16]rendererStatsEntry
}
type rendererStatsEntry struct {
Frames *atomic.Uint64
Lines *atomic.Uint64
Size *atomic.Uint64
}
func NewRenderer(title string, frameRate float64, display shapes.Rectangle[float64]) *Renderer {
width := int64(display.Width() * settings.GlobalSettings.VideoScaleMultiplier)
height := int64(display.Height() * settings.GlobalSettings.VideoScaleMultiplier)
@ -73,84 +67,6 @@ func NewRenderer(title string, frameRate float64, display shapes.Rectangle[float
},
}
}
type RendererStatistics []RendererStatistic
func (s RendererStatistics) SortByFrames() {
slices.SortStableFunc(s, func(a, b RendererStatistic) int {
return int(b.Frames - a.Frames)
})
}
func (s RendererStatistics) SortByLines() {
slices.SortStableFunc(s, func(a, b RendererStatistic) int {
return int(b.Lines - a.Lines)
})
}
func (s RendererStatistics) SortBySize() {
slices.SortStableFunc(s, func(a, b RendererStatistic) int {
return int(b.Size - a.Size)
})
}
func (s RendererStatistics) Reverse() {
slices.Reverse(s)
}
func (s RendererStatistics) Strings() (r []string) {
maxObjectId := len("Object Id")
maxFrames := len("Frames")
maxLines := len("Lines")
maxSize := len("Lines")
for _, e := range s {
oId, frames, lines, size := e.Strings()
maxObjectId = max(maxObjectId, len(oId))
maxFrames = max(maxFrames, len(frames))
maxLines = max(maxLines, len(lines))
maxSize = max(maxSize, len(size))
}
maxIndex := len(strconv.FormatUint(uint64(len(s)), 10))
for i, e := range s {
oId, frames, lines, size := e.Strings()
i := strconv.FormatUint(uint64(i), 10)
r = append(r, fmt.Sprintf("| %*s | %*s | %*s | %*s | %*s |", maxIndex, i, maxObjectId, oId, maxFrames, frames, maxLines, lines, maxSize, size))
}
slices.Insert(r, 0, fmt.Sprintf("| %*s | %*s | %*s | %*s | %*s |", maxIndex, "#", maxObjectId, "Object Id", maxFrames, "Frames", maxLines, "Lines", maxSize, "Size"))
return r
}
type RendererStatistic struct {
ObjectId uint16
Frames uint64
Lines uint64
Size uint64
}
func (s RendererStatistic) Strings() (string, string, string, string) {
oId := strconv.FormatUint(uint64(s.ObjectId), 10)
frames := strconv.FormatUint(s.Frames, 10)
lines := strconv.FormatUint(s.Lines, 10)
size := strconv.FormatFloat(float64(s.Size)/(1<<20), 'f', 2, 64) + " MiB"
return oId, frames, lines, size
}
func (r *Renderer) AggregateStatistics() (s RendererStatistics) {
for objectId, stats := range r.Statistics {
s = append(s, RendererStatistic{
ObjectId: objectId,
Frames: stats.Frames.Load(),
Lines: stats.Lines.Load(),
Size: stats.Size.Load(),
})
}
return s
}
func (r *Renderer) RenderFrame(frameInfo types.FrameInformation, frame types.RenderedFrame, maxInterval int) (result []string) {
if len(r.Header) != 0 {
result = append(result, r.Header...)

91
ass/stats.go Normal file
View file

@ -0,0 +1,91 @@
package ass
import (
"fmt"
"slices"
"strconv"
"sync/atomic"
)
type rendererStatsEntry struct {
Frames *atomic.Uint64
Lines *atomic.Uint64
Size *atomic.Uint64
}
type RendererStatistics []RendererStatistic
func (s RendererStatistics) SortByFrames() {
slices.SortStableFunc(s, func(a, b RendererStatistic) int {
return int(b.Frames - a.Frames)
})
}
func (s RendererStatistics) SortByLines() {
slices.SortStableFunc(s, func(a, b RendererStatistic) int {
return int(b.Lines - a.Lines)
})
}
func (s RendererStatistics) SortBySize() {
slices.SortStableFunc(s, func(a, b RendererStatistic) int {
return int(b.Size - a.Size)
})
}
func (s RendererStatistics) Reverse() {
slices.Reverse(s)
}
func (s RendererStatistics) Strings() (r []string) {
maxObjectId := len("Object Id")
maxFrames := len("Frames")
maxLines := len("Lines")
maxSize := len("Lines")
for _, e := range s {
oId, frames, lines, size := e.Strings()
maxObjectId = max(maxObjectId, len(oId))
maxFrames = max(maxFrames, len(frames))
maxLines = max(maxLines, len(lines))
maxSize = max(maxSize, len(size))
}
maxIndex := len(strconv.FormatUint(uint64(len(s)), 10))
for i, e := range s {
oId, frames, lines, size := e.Strings()
i := strconv.FormatUint(uint64(i), 10)
r = append(r, fmt.Sprintf("| %*s | %*s | %*s | %*s | %*s |", maxIndex, i, maxObjectId, oId, maxFrames, frames, maxLines, lines, maxSize, size))
}
slices.Insert(r, 0, fmt.Sprintf("| %*s | %*s | %*s | %*s | %*s |", maxIndex, "#", maxObjectId, "Object Id", maxFrames, "Frames", maxLines, "Lines", maxSize, "Size"))
return r
}
type RendererStatistic struct {
ObjectId uint16
Frames uint64
Lines uint64
Size uint64
}
func (s RendererStatistic) Strings() (string, string, string, string) {
oId := strconv.FormatUint(uint64(s.ObjectId), 10)
frames := strconv.FormatUint(s.Frames, 10)
lines := strconv.FormatUint(s.Lines, 10)
size := strconv.FormatFloat(float64(s.Size)/(1<<20), 'f', 2, 64) + " MiB"
return oId, frames, lines, size
}
func (r *Renderer) AggregateStatistics() (s RendererStatistics) {
for objectId, stats := range r.Statistics {
s = append(s, RendererStatistic{
ObjectId: objectId,
Frames: stats.Frames.Load(),
Lines: stats.Lines.Load(),
Size: stats.Size.Load(),
})
}
return s
}