metric_history_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package service
  2. import (
  3. "fmt"
  4. "runtime"
  5. "testing"
  6. "time"
  7. )
  8. func TestMetricMemoryFootprint(t *testing.T) {
  9. const metrics = 16
  10. retained := func(build func() any) uint64 {
  11. runtime.GC()
  12. runtime.GC()
  13. var m0 runtime.MemStats
  14. runtime.ReadMemStats(&m0)
  15. obj := build()
  16. runtime.GC()
  17. var m1 runtime.MemStats
  18. runtime.ReadMemStats(&m1)
  19. runtime.KeepAlive(obj)
  20. if m1.HeapAlloc < m0.HeapAlloc {
  21. return 0
  22. }
  23. return m1.HeapAlloc - m0.HeapAlloc
  24. }
  25. fill := func(buf []MetricSample) {
  26. for j := range buf {
  27. buf[j] = MetricSample{T: int64(j), V: float64(j)}
  28. }
  29. }
  30. oldFlat := retained(func() any {
  31. m := make(map[string][]MetricSample, metrics)
  32. for i := 0; i < metrics; i++ {
  33. buf := make([]MetricSample, 86400)
  34. fill(buf)
  35. m[fmt.Sprintf("m%d", i)] = buf
  36. }
  37. return m
  38. })
  39. newTiered := retained(func() any {
  40. h := newMetricHistory()
  41. for i := 0; i < metrics; i++ {
  42. s := newSeries()
  43. for _, tb := range s.tiers {
  44. buf := make([]MetricSample, tb.capacity)
  45. fill(buf)
  46. tb.samples = buf
  47. }
  48. h.series[fmt.Sprintf("m%d", i)] = s
  49. }
  50. return h
  51. })
  52. t.Logf("metric history footprint (16 system metrics, full):")
  53. t.Logf(" before (flat 48h@2s): %d KiB", oldFlat/1024)
  54. t.Logf(" after (tiered 7d): %d KiB", newTiered/1024)
  55. if newTiered >= oldFlat {
  56. t.Fatalf("expected tiered footprint smaller: old=%d new=%d", oldFlat, newTiered)
  57. }
  58. }
  59. func TestTierBufRollupAveragesClosedBuckets(t *testing.T) {
  60. tb := &tierBuf{resolution: 10, capacity: 100}
  61. tb.add(0, 2)
  62. tb.add(2, 4)
  63. tb.add(5, 6)
  64. tb.add(10, 10)
  65. if len(tb.samples) != 1 || tb.samples[0].T != 0 || tb.samples[0].V != 4 {
  66. t.Fatalf("expected one closed bucket {0,4}, got %+v", tb.samples)
  67. }
  68. got := tb.readSamples()
  69. if len(got) != 2 || got[1].T != 10 || got[1].V != 10 {
  70. t.Fatalf("expected open bucket {10,10} appended on read, got %+v", got)
  71. }
  72. }
  73. func TestTierBufRespectsCapacity(t *testing.T) {
  74. tb := &tierBuf{resolution: 1, capacity: 5}
  75. for i := int64(0); i < 20; i++ {
  76. tb.add(i, float64(i))
  77. }
  78. if len(tb.samples) != 5 {
  79. t.Fatalf("expected closed buckets capped at 5, got %d", len(tb.samples))
  80. }
  81. if last := tb.samples[len(tb.samples)-1]; last.T != 18 {
  82. t.Fatalf("expected last closed bucket T=18 (19 still open), got %d", last.T)
  83. }
  84. }
  85. func TestSeriesPickTierBySpan(t *testing.T) {
  86. s := newSeries()
  87. cases := []struct {
  88. span int64
  89. res int
  90. }{
  91. {120, 2},
  92. {3600, 2},
  93. {7200, 60},
  94. {172800, 60},
  95. {604800, 600},
  96. {9999999, 600},
  97. }
  98. for _, c := range cases {
  99. if got := s.pickTier(c.span); got.resolution != c.res {
  100. t.Errorf("span %d: expected resolution %d, got %d", c.span, c.res, got.resolution)
  101. }
  102. }
  103. }
  104. func TestAggregateFineRealtime(t *testing.T) {
  105. h := newMetricHistory()
  106. now := time.Now().Unix()
  107. for i := int64(59); i >= 0; i-- {
  108. h.append("cpu", time.Unix(now-i*2, 0), float64(100-i))
  109. }
  110. out := h.aggregate("cpu", 2, 60)
  111. if len(out) == 0 {
  112. t.Fatalf("expected non-empty realtime aggregate")
  113. }
  114. if _, ok := out[len(out)-1]["v"].(float64); !ok {
  115. t.Fatalf("expected float64 value, got %T", out[len(out)-1]["v"])
  116. }
  117. }
  118. func TestAggregateLongSpanUsesCoarseTier(t *testing.T) {
  119. h := newMetricHistory()
  120. now := time.Now().Unix()
  121. for i := int64(0); i < 200; i++ {
  122. ts := now - (200-i)*600
  123. h.append("cpu", time.Unix(ts, 0), float64(i))
  124. }
  125. out := h.aggregate("cpu", 10080, 60)
  126. if len(out) == 0 {
  127. t.Fatalf("expected non-empty 7d aggregate from the archive tier")
  128. }
  129. }
  130. func TestSnapshotRestoreRoundTrip(t *testing.T) {
  131. h := newMetricHistory()
  132. now := time.Now().Unix()
  133. for i := int64(0); i < 10; i++ {
  134. h.append("cpu", time.Unix(now-(9-i)*2, 0), float64(i))
  135. }
  136. h2 := newMetricHistory()
  137. h2.restore(h.snapshot())
  138. if out := h2.aggregate("cpu", 2, 60); len(out) == 0 {
  139. t.Fatalf("expected restored series to aggregate")
  140. }
  141. }
  142. func TestAggregateMissingMetricIsEmpty(t *testing.T) {
  143. h := newMetricHistory()
  144. if out := h.aggregate("nope", 2, 60); len(out) != 0 {
  145. t.Fatalf("expected empty result for unknown metric, got %d points", len(out))
  146. }
  147. }