github.com/phuslu/fastdns@v0.8.3-0.20240310041952-69506fc67dd1/stats_test.go (about)

     1  package fastdns
     2  
     3  import (
     4  	"encoding/hex"
     5  	"net/netip"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func BenchmarkUpdateStats(b *testing.B) {
    11  	payload, _ := hex.DecodeString("8e5281800001000200000000047632657803636f6d0000020001c00c000200010000545f0014036b696d026e730a636c6f7564666c617265c011c00c000200010000545f000704746f6464c02a")
    12  
    13  	resp := AcquireMessage()
    14  	defer ReleaseMessage(resp)
    15  
    16  	err := ParseMessage(resp, payload, true)
    17  	if err != nil {
    18  		b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
    19  	}
    20  
    21  	stats := &CoreStats{
    22  		Prefix: "coredns_",
    23  		Family: "1",
    24  		Proto:  "udp",
    25  		Server: "dns://:53",
    26  		Zone:   ".",
    27  	}
    28  
    29  	addr := netip.AddrPortFrom(netip.AddrFrom4([4]byte{127, 0, 0, 1}), 12345)
    30  
    31  	b.ResetTimer()
    32  	for i := 0; i < b.N; i++ {
    33  		stats.UpdateStats(addr, resp, time.Millisecond)
    34  	}
    35  }
    36  
    37  func BenchmarkAppendOpenMetrics(b *testing.B) {
    38  	payload, _ := hex.DecodeString("8e5281800001000200000000047632657803636f6d0000020001c00c000200010000545f0014036b696d026e730a636c6f7564666c617265c011c00c000200010000545f000704746f6464c02a")
    39  
    40  	resp := AcquireMessage()
    41  	defer ReleaseMessage(resp)
    42  
    43  	err := ParseMessage(resp, payload, true)
    44  	if err != nil {
    45  		b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
    46  	}
    47  
    48  	stats := &CoreStats{
    49  		Prefix: "coredns_",
    50  		Family: "1",
    51  		Proto:  "udp",
    52  		Server: "dns://:53",
    53  		Zone:   ".",
    54  	}
    55  
    56  	addr := netip.AddrPortFrom(netip.AddrFrom4([4]byte{127, 0, 0, 1}), 12345)
    57  
    58  	stats.UpdateStats(addr, resp, time.Millisecond)
    59  
    60  	buf := make([]byte, 0, 32*1024)
    61  
    62  	b.ResetTimer()
    63  	for i := 0; i < b.N; i++ {
    64  		stats.AppendOpenMetrics(buf[:0])
    65  	}
    66  }