mirror of
https://github.com/pommee/goaway.git
synced 2026-05-11 03:49:25 -05:00
test: update benchmark test to reflect any changes
This commit is contained in:
+14
-63
@@ -1,13 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"codeberg.org/miekg/dns"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -18,13 +19,11 @@ var (
|
||||
)
|
||||
|
||||
func initCache() {
|
||||
questionCache = new(dns.Msg)
|
||||
questionCache.SetQuestion(dns.Fqdn("google.com"), dns.TypeA)
|
||||
questionCache = dns.NewMsg("google.com.", dns.TypeA)
|
||||
questionCache.RecursionDesired = true
|
||||
|
||||
clientCache = new(dns.Client)
|
||||
clientCache.Timeout = 2 * time.Second
|
||||
clientCache.Net = "udp"
|
||||
clientCache = dns.NewClient()
|
||||
clientCache.Dialer.Timeout = 2 * time.Second
|
||||
}
|
||||
|
||||
func BenchmarkDNSRequest(b *testing.B) {
|
||||
@@ -34,7 +33,7 @@ func BenchmarkDNSRequest(b *testing.B) {
|
||||
|
||||
for b.Loop() {
|
||||
m := questionCache.Copy()
|
||||
_, _, err := clientCache.Exchange(m, server)
|
||||
_, _, err := clientCache.Exchange(context.TODO(), m, "udp", server)
|
||||
if err != nil {
|
||||
b.Errorf("DNS query failed: %v", err)
|
||||
continue
|
||||
@@ -42,69 +41,22 @@ func BenchmarkDNSRequest(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDNSRequestWithConn(b *testing.B) {
|
||||
once.Do(initCache)
|
||||
|
||||
conn, err := net.Dial("udp", server)
|
||||
if err != nil {
|
||||
b.Fatalf("Failed to create connection: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
dnsConn := &dns.Conn{Conn: conn}
|
||||
defer func(dnsConn *dns.Conn) {
|
||||
_ = dnsConn.Close()
|
||||
}(dnsConn)
|
||||
|
||||
b.ReportAllocs()
|
||||
|
||||
for b.Loop() {
|
||||
m := questionCache.Copy()
|
||||
|
||||
if err := dnsConn.WriteMsg(m); err != nil {
|
||||
b.Errorf("Failed to send DNS query: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := dnsConn.ReadMsg()
|
||||
if err != nil {
|
||||
b.Errorf("Failed to read DNS response: %v", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkDNSRequestParallelWithConn(b *testing.B) {
|
||||
func BenchmarkDNSRequestParallel(b *testing.B) {
|
||||
once.Do(initCache)
|
||||
|
||||
b.ResetTimer()
|
||||
b.ReportAllocs()
|
||||
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
conn, err := net.Dial("udp", server)
|
||||
if err != nil {
|
||||
b.Fatalf("Failed to create connection: %v", err)
|
||||
}
|
||||
defer func(conn net.Conn) {
|
||||
_ = conn.Close()
|
||||
}(conn)
|
||||
|
||||
dnsConn := &dns.Conn{Conn: conn}
|
||||
defer func(dnsConn *dns.Conn) {
|
||||
_ = dnsConn.Close()
|
||||
}(dnsConn)
|
||||
client := dns.NewClient()
|
||||
client.Dialer.Timeout = 2 * time.Second
|
||||
|
||||
for pb.Next() {
|
||||
m := questionCache.Copy()
|
||||
|
||||
if err := dnsConn.WriteMsg(m); err != nil {
|
||||
b.Errorf("Failed to send DNS query: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
_, err := dnsConn.ReadMsg()
|
||||
_, _, err := client.Exchange(context.TODO(), m, "udp", server)
|
||||
if err != nil {
|
||||
b.Errorf("Failed to read DNS response: %v", err)
|
||||
b.Errorf("DNS query failed: %v", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -114,7 +66,7 @@ func BenchmarkDNSRequestParallelWithConn(b *testing.B) {
|
||||
func TestDNSConnectivity(t *testing.T) {
|
||||
once.Do(initCache)
|
||||
|
||||
r, rtt, err := clientCache.Exchange(questionCache.Copy(), server)
|
||||
r, _, err := clientCache.Exchange(context.TODO(), questionCache.Copy(), "udp", server)
|
||||
|
||||
if err != nil {
|
||||
var netErr net.Error
|
||||
@@ -127,7 +79,7 @@ func TestDNSConnectivity(t *testing.T) {
|
||||
t.Fatalf("DNS query failed with response code: %v", r.Rcode)
|
||||
}
|
||||
|
||||
t.Logf("DNS server responded in %v", rtt)
|
||||
t.Logf("DNS server responded")
|
||||
if len(r.Answer) > 0 {
|
||||
t.Logf("Received %d answers", len(r.Answer))
|
||||
} else {
|
||||
@@ -142,8 +94,7 @@ func main() {
|
||||
},
|
||||
[]testing.InternalBenchmark{
|
||||
{Name: "BenchmarkDNSRequest", F: BenchmarkDNSRequest},
|
||||
{Name: "BenchmarkDNSRequestWithConn", F: BenchmarkDNSRequestWithConn},
|
||||
{Name: "BenchmarkDNSRequestParallelWithConn", F: BenchmarkDNSRequestParallelWithConn},
|
||||
{Name: "BenchmarkDNSRequestParallelWithConn", F: BenchmarkDNSRequestParallel},
|
||||
},
|
||||
[]testing.InternalExample{})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user