From 0f35fc94f96f57b90ab000bf373c40a0fde24fa7 Mon Sep 17 00:00:00 2001 From: phuslu Date: Tue, 22 Oct 2024 21:53:37 +0800 Subject: [PATCH] add fastdns resolver benchmark --- client_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/client_test.go b/client_test.go index ded8be6..124d8a9 100644 --- a/client_test.go +++ b/client_test.go @@ -6,7 +6,9 @@ import ( "net/http" "net/netip" "net/url" + "os" "reflect" + "regexp" "testing" "time" "unsafe" @@ -147,3 +149,56 @@ func TestClientLookup(t *testing.T) { } } } + +func BenchmarkResolverPureGo(b *testing.B) { + resolver := net.Resolver{PreferGo: true} + + b.ReportAllocs() + b.ResetTimer() + b.RunParallel(func(b *testing.PB) { + for b.Next() { + _, _ = resolver.LookupNetIP(context.Background(), "ip4", "www.google.com") + } + }) +} + +func BenchmarkResolverCGO(b *testing.B) { + resolver := net.Resolver{PreferGo: false} + + b.ReportAllocs() + b.ResetTimer() + b.RunParallel(func(b *testing.PB) { + for b.Next() { + _, _ = resolver.LookupNetIP(context.Background(), "ip4", "www.google.com") + } + }) +} + +func BenchmarkResolverFastdns(b *testing.B) { + server := "8.8.8.8:53" + if data, err := os.ReadFile("/etc/resolv.conf"); err == nil { + if m := regexp.MustCompile(`(^|\n)\s*nameserver\s+(\S+)`).FindAllStringSubmatch(string(data), -1); len(m) != 0 { + server = m[0][2] + ":53" + } + } + b.Logf("fastdns use dns server: %s", server) + + resolver := Client{ + Addr: server, + Dialer: &UDPDialer{ + Addr: func() (u *net.UDPAddr) { u, _ = net.ResolveUDPAddr("udp", server); return }(), + MaxConns: 1000, + }, + } + + b.ReportAllocs() + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + ips, err := resolver.LookupNetIP(context.Background(), "ip4", "www.google.com") + if len(ips) == 0 || err != nil { + b.Errorf("fastdns return ips: %+v error: %+v", ips, err) + } + } + }) +}