Files
3x-ui-agent/internal/web/service/metric_history_test.go
T
Muse f887f47bb9 P7: restore agent node type, agent heartbeat expiry, build-version injection, remove log/history/xray-metrics modules, optional sub port
- nodes: port node-type (traditional/agent) picker back from web-src into
  the embedded frontend; backend annotates NodeView.nodeType from
  agent_nodes bindings; fix agent-node update path (skip reachability
  probe and apiToken requirement for agent-managed nodes); restore i18n
  keys in all 13 locales; install command now uses the Gitea mirror
- jobs: expire agent-bound nodes whose agent_nodes.last_seen is older
  than max(3x heartbeat interval, 120s) = 180s; last_seen=0 (never
  registered) is left untouched; transitions reuse UpdateHeartbeat +
  node event broadcast
- config: GetBaseVersion honors ldflags-injected buildVersion; release
  builds inject 3.7.0-agent.1 so the panel no longer reports a phantom
  update against the Gitea release tag
- logger/server: remove the three log viewers (panel/xray/amneziawg),
  their API endpoints and the 10240-entry in-memory log buffer; file
  logging via lumberjack unchanged; tests assert via a new optional
  logger hook instead of the removed buffer
- server: remove system history and xray metrics modules end to end
  (endpoints, sampling appends in RefreshStatus, per-minute persist
  cron, gob snapshot/restore, XrayMetricsService); realtime status and
  per-node history are untouched
- settings: subPort may be 0/unset - subscription links are generated
  without a port while the subscription service keeps binding the
  default 2096; the settings form declares the cleared state via
  onNumber's new onClear option instead of snapping back to 1
2026-10-21 06:44:00 +00:00

153 lines
3.5 KiB
Go

package service
import (
"fmt"
"runtime"
"testing"
"time"
)
func TestMetricMemoryFootprint(t *testing.T) {
const metrics = 16
retained := func(build func() any) uint64 {
runtime.GC()
runtime.GC()
var m0 runtime.MemStats
runtime.ReadMemStats(&m0)
obj := build()
runtime.GC()
var m1 runtime.MemStats
runtime.ReadMemStats(&m1)
runtime.KeepAlive(obj)
if m1.HeapAlloc < m0.HeapAlloc {
return 0
}
return m1.HeapAlloc - m0.HeapAlloc
}
fill := func(buf []MetricSample) {
for j := range buf {
buf[j] = MetricSample{T: int64(j), V: float64(j)}
}
}
oldFlat := retained(func() any {
m := make(map[string][]MetricSample, metrics)
for i := range metrics {
buf := make([]MetricSample, 86400)
fill(buf)
m[fmt.Sprintf("m%d", i)] = buf
}
return m
})
newTiered := retained(func() any {
h := newMetricHistory()
for i := range metrics {
s := newSeries()
for _, tb := range s.tiers {
buf := make([]MetricSample, tb.capacity)
fill(buf)
tb.samples = buf
}
h.series[fmt.Sprintf("m%d", i)] = s
}
return h
})
t.Logf("metric history footprint (16 system metrics, full):")
t.Logf(" before (flat 48h@2s): %d KiB", oldFlat/1024)
t.Logf(" after (tiered 7d): %d KiB", newTiered/1024)
if newTiered >= oldFlat {
t.Fatalf("expected tiered footprint smaller: old=%d new=%d", oldFlat, newTiered)
}
}
func TestTierBufRollupAveragesClosedBuckets(t *testing.T) {
tb := &tierBuf{resolution: 10, capacity: 100}
tb.add(0, 2)
tb.add(2, 4)
tb.add(5, 6)
tb.add(10, 10)
if len(tb.samples) != 1 || tb.samples[0].T != 0 || tb.samples[0].V != 4 {
t.Fatalf("expected one closed bucket {0,4}, got %+v", tb.samples)
}
got := tb.readSamples()
if len(got) != 2 || got[1].T != 10 || got[1].V != 10 {
t.Fatalf("expected open bucket {10,10} appended on read, got %+v", got)
}
}
func TestTierBufRespectsCapacity(t *testing.T) {
tb := &tierBuf{resolution: 1, capacity: 5}
for i := range int64(20) {
tb.add(i, float64(i))
}
if len(tb.samples) != 5 {
t.Fatalf("expected closed buckets capped at 5, got %d", len(tb.samples))
}
if last := tb.samples[len(tb.samples)-1]; last.T != 18 {
t.Fatalf("expected last closed bucket T=18 (19 still open), got %d", last.T)
}
}
func TestSeriesPickTierBySpan(t *testing.T) {
s := newSeries()
cases := []struct {
span int64
res int
}{
{120, 2},
{3600, 2},
{7200, 60},
{172800, 60},
{604800, 600},
{9999999, 600},
}
for _, c := range cases {
if got := s.pickTier(c.span); got.resolution != c.res {
t.Errorf("span %d: expected resolution %d, got %d", c.span, c.res, got.resolution)
}
}
}
func TestAggregateFineRealtime(t *testing.T) {
h := newMetricHistory()
now := time.Now().Unix()
for i := int64(59); i >= 0; i-- {
h.append("cpu", time.Unix(now-i*2, 0), float64(100-i))
}
out := h.aggregate("cpu", 2, 60)
if len(out) == 0 {
t.Fatalf("expected non-empty realtime aggregate")
}
if _, ok := out[len(out)-1]["v"].(float64); !ok {
t.Fatalf("expected float64 value, got %T", out[len(out)-1]["v"])
}
}
func TestAggregateLongSpanUsesCoarseTier(t *testing.T) {
h := newMetricHistory()
now := time.Now().Unix()
for i := range int64(200) {
ts := now - (200-i)*600
h.append("cpu", time.Unix(ts, 0), float64(i))
}
out := h.aggregate("cpu", 10080, 60)
if len(out) == 0 {
t.Fatalf("expected non-empty 7d aggregate from the archive tier")
}
}
func TestAggregateMissingMetricIsEmpty(t *testing.T) {
h := newMetricHistory()
if out := h.aggregate("nope", 2, 60); len(out) != 0 {
t.Fatalf("expected empty result for unknown metric, got %d points", len(out))
}
}