Something went wrong. Try again.
Metric and Time-Series Engine: A Minimal Archiving Time Series Storage Service
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152package types
import ( "bytes" "math" "testing")
func TestKeyCodec_RoundTrip(t *testing.T) { origB := BucketID(492001) origM := MetricID(10842) origTS := int64(1725537600123)
var buf [KeySize]byte EncodeKey(buf[:], origB, origM, origTS)
decB, decM, decTS := DecodeKey(buf[:]) if decB != origB { t.Fatalf("expected bucket %d, got %d", origB, decB) } if decM != origM { t.Fatalf("expected metric %d, got %d", origM, decM) } if decTS != origTS { t.Fatalf("expected timestamp %d, got %d", origTS, decTS) }}
func TestValueCodec_RoundTrip(t *testing.T) { testValues := []float64{ 0.0, math.Copysign(0.0, -1.0), 1.0, -1.0, 42.58912, -100234.9812, math.MaxFloat64, math.SmallestNonzeroFloat64, math.Inf(1), math.Inf(-1), }
for _, val := range testValues { var buf [ValueSize]byte EncodeValue(buf[:], val) got := DecodeValue(buf[:]) if math.IsInf(val, 1) { if !math.IsInf(got, 1) { t.Fatalf("expected +Inf, got %v", got) } } else if math.IsInf(val, -1) { if !math.IsInf(got, -1) { t.Fatalf("expected -Inf, got %v", got) } } else if got != val { t.Fatalf("expected %v, got %v", val, got) } }}
func TestLexicographicalOrdering(t *testing.T) { // 1. Buckets order var k1, k2 [KeySize]byte EncodeKey(k1[:], BucketID(10), MetricID(100), 1000) EncodeKey(k2[:], BucketID(11), MetricID(1), 1) if bytes.Compare(k1[:], k2[:]) >= 0 { t.Fatalf("expected k1 < k2 for different buckets") }
// 2. Metric order inside same bucket EncodeKey(k1[:], BucketID(10), MetricID(5), 9999) EncodeKey(k2[:], BucketID(10), MetricID(6), 1000) if bytes.Compare(k1[:], k2[:]) >= 0 { t.Fatalf("expected k1 < k2 for different metrics in same bucket") }
// 3. Timestamp order inside same bucket & metric EncodeKey(k1[:], BucketID(10), MetricID(5), 1000) EncodeKey(k2[:], BucketID(10), MetricID(5), 1001) if bytes.Compare(k1[:], k2[:]) >= 0 { t.Fatalf("expected k1 < k2 for timestamps") }}
func TestBucketBounds(t *testing.T) { b := BucketID(42) start, end := BucketBounds(b)
// Lowest possible key in bucket b var minKey [KeySize]byte EncodeKey(minKey[:], b, 0, 0) if bytes.Compare(start, minKey[:]) > 0 { t.Fatalf("start bound must be <= minKey") }
// Highest possible key in bucket b var maxKey [KeySize]byte EncodeKey(maxKey[:], b, MetricID(math.MaxInt64), math.MaxInt64) if bytes.Compare(maxKey[:], end) >= 0 { t.Fatalf("maxKey must be strictly < end bound") }
// Lowest key in bucket b+1 must be >= end bound var nextMinKey [KeySize]byte EncodeKey(nextMinKey[:], b+1, 0, 0) if bytes.Compare(nextMinKey[:], end) < 0 { t.Fatalf("nextMinKey must be >= end bound") }}
func TestHashMetricID(t *testing.T) { h1 := HashMetricID("router-01", "cpu.idle", "") h2 := HashMetricID("router-01", "cpu.idle", "") if h1 != h2 { t.Fatalf("expected identical hash for same series, got %d vs %d", h1, h2) } if h1 <= 0 { t.Fatalf("expected positive hash ID, got %d", h1) }
hDiffHost := HashMetricID("router-02", "cpu.idle", "") if hDiffHost == h1 { t.Fatalf("expected different hash for different host") }
hDiffService := HashMetricID("router-01", "mem.used", "") if hDiffService == h1 { t.Fatalf("expected different hash for different service") }
hWithTags := HashMetricID("router-01", "cpu.idle", "core=0") if hWithTags == h1 { t.Fatalf("expected different hash when tags are present") } if hWithTags <= 0 { t.Fatalf("expected positive hash ID with tags, got %d", hWithTags) }}
func BenchmarkEncodeKey(b *testing.B) { var buf [KeySize]byte bID := BucketID(100) mID := MetricID(200) ts := int64(1700000000)
b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { EncodeKey(buf[:], bID, mID, ts) }}