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 16ddeea
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 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,52 @@ 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 := "1.1.1.1: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]
}
}

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(b *testing.PB) {
for b.Next() {
_, _ = resolver.LookupNetIP(context.Background(), "ip4", "www.google.com")
}
})
}

0 comments on commit 16ddeea

Please sign in to comment.