feat(metricstore): maintain running buffer stats on write

This commit is contained in:
Aditya Ujeniya
2026-07-20 10:22:16 +02:00
parent aa61ccb88b
commit 931a2a6e79
2 changed files with 77 additions and 0 deletions
+56
View File
@@ -8,6 +8,8 @@ package metricstore
import (
"math"
"testing"
"github.com/ClusterCockpit/cc-lib/v2/schema"
)
func TestNewBufferStatsInitialized(t *testing.T) {
@@ -28,3 +30,57 @@ func TestNewBufferStatsInitialized(t *testing.T) {
t.Error("statsValid = false, want true for a fresh empty buffer")
}
}
func TestWriteUpdatesRunningStats(t *testing.T) {
b := newBuffer(100, 10)
b.write(100, schema.Float(3.0))
b.write(110, schema.Float(1.0))
b.write(120, schema.Float(5.0))
if b.statSamples != 3 {
t.Errorf("statSamples = %d, want 3", b.statSamples)
}
if b.statSum != 9.0 {
t.Errorf("statSum = %v, want 9.0", b.statSum)
}
if b.statMin != 1.0 {
t.Errorf("statMin = %v, want 1.0", b.statMin)
}
if b.statMax != 5.0 {
t.Errorf("statMax = %v, want 5.0", b.statMax)
}
if !b.statsValid {
t.Error("statsValid = false, want true after appends only")
}
}
func TestWriteNaNExcludedFromStats(t *testing.T) {
b := newBuffer(100, 10)
b.write(100, schema.Float(2.0))
b.write(110, schema.NaN) // explicit NaN
b.write(130, schema.Float(4.0)) // leaves a NaN-filled gap at t=120
if b.statSamples != 2 {
t.Errorf("statSamples = %d, want 2 (NaN and gap excluded)", b.statSamples)
}
if b.statSum != 6.0 {
t.Errorf("statSum = %v, want 6.0", b.statSum)
}
if b.statMin != 2.0 || b.statMax != 4.0 {
t.Errorf("statMin/statMax = %v/%v, want 2.0/4.0", b.statMin, b.statMax)
}
}
func TestWriteOverwriteInvalidatesStats(t *testing.T) {
b := newBuffer(100, 10)
b.write(100, schema.Float(1.0))
b.write(110, schema.Float(2.0))
if !b.statsValid {
t.Fatal("precondition: statsValid should be true after appends")
}
b.write(100, schema.Float(99.0)) // overwrite existing index
if b.statsValid {
t.Error("statsValid = true, want false after overwrite")
}
}