github.com/TeaOSLab/EdgeNode@v1.3.8/internal/metrics/stat_test.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package metrics_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/metrics"
     7  	"github.com/iwind/TeaGo/assert"
     8  	"testing"
     9  )
    10  
    11  func TestStat_EncodeValueKey(t *testing.T) {
    12  	var a = assert.NewAssertion(t)
    13  
    14  	var stat = &metrics.Stat{
    15  		ServerId: 1,
    16  		Keys:     []string{"${remoteAddr}"},
    17  		Hash:     "123456",
    18  		Value:    123,
    19  		Time:     "20240101",
    20  	}
    21  
    22  	var valueKey = stat.EncodeValueKey(100)
    23  	t.Log(valueKey)
    24  
    25  	serverId, timeString, version, value, hash, err := metrics.DecodeValueKey(valueKey)
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	t.Log(serverId, timeString, value, version, hash)
    30  	a.IsTrue(serverId == 1)
    31  	a.IsTrue(timeString == "20240101")
    32  	a.IsTrue(value == 123)
    33  	a.IsTrue(version == 100)
    34  	a.IsTrue(hash == "123456")
    35  }
    36  
    37  func TestStat_EncodeSumKey(t *testing.T) {
    38  	var a = assert.NewAssertion(t)
    39  
    40  	var stat = &metrics.Stat{
    41  		ServerId: 1,
    42  		Keys:     []string{"${remoteAddr}"},
    43  		Hash:     "123456",
    44  		Value:    123,
    45  		Time:     "20240101",
    46  	}
    47  	var sumKey = stat.EncodeSumKey(100)
    48  	t.Log(sumKey)
    49  
    50  	serverId, timeString, version, err := metrics.DecodeSumKey(sumKey)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	t.Log(serverId, timeString, version)
    55  	a.IsTrue(serverId == 1)
    56  	a.IsTrue(timeString == "20240101")
    57  	a.IsTrue(version == 100)
    58  }
    59  
    60  func TestStat_EncodeSumValue(t *testing.T) {
    61  	var a = assert.NewAssertion(t)
    62  
    63  	var b = metrics.EncodeSumValue(123, 456)
    64  	t.Log(b)
    65  
    66  	count, sum := metrics.DecodeSumValue(b)
    67  	a.IsTrue(count == 123)
    68  	a.IsTrue(sum == 456)
    69  }