Skip to content

Commit

Permalink
add fastdns resolver benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Oct 22, 2024
1 parent 585ec96 commit 0f35fc9
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"net/http"
"net/netip"
"net/url"
"os"
"reflect"
"regexp"
"testing"
"time"
"unsafe"
Expand Down Expand Up @@ -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)
}
}
})
}

0 comments on commit 0f35fc9

Please sign in to comment.