net: implement query-response fast failover in builtin dns stub resolver

Speed improvements via code cleanup, and changes to make go dns behave more like glibc resolver.  See https://groups.google.com/forum/#!topic/golang-dev/lV-0aHqxVeo

Fixes #6579.

Benchmark results on linux/amd64

benchmark                                  old ns/op    new ns/op    delta
BenchmarkGoLookupIP                          4831903      2572937  -46.75%
BenchmarkGoLookupIPNoSuchHost               10114105      2419641  -76.08%
BenchmarkGoLookupIPWithBrokenNameServer  20007735624   5004490730  -74.99%

benchmark                                 old allocs   new allocs    delta
BenchmarkGoLookupIP                              287          288    0.35%
BenchmarkGoLookupIPNoSuchHost                    204          102  -50.00%
BenchmarkGoLookupIPWithBrokenNameServer          410          358  -12.68%

benchmark                                  old bytes    new bytes    delta
BenchmarkGoLookupIP                            13181        13271    0.68%
BenchmarkGoLookupIPNoSuchHost                  17260         8714  -49.51%
BenchmarkGoLookupIPWithBrokenNameServer        28160        22432  -20.34%

LGTM=mikioh.mikioh
R=golang-codereviews, mikioh.mikioh, bradfitz, josharian, abursavich
CC=golang-codereviews
https://golang.org/cl/128820043
This commit is contained in:
Alex A Skinner 2014-08-30 13:12:28 +09:00 committed by Mikio Hara
parent 2758cb75f0
commit 39a021bc0e
2 changed files with 54 additions and 41 deletions

View file

@ -218,8 +218,29 @@ func TestReloadResolvConfChange(t *testing.T) {
r.WantServers([]string{"[8.8.4.4]"})
}
func BenchmarkGoLookupIP(b *testing.B) {
for i := 0; i < b.N; i++ {
goLookupIP("www.example.com")
}
}
func BenchmarkGoLookupIPNoSuchHost(b *testing.B) {
for i := 0; i < b.N; i++ {
goLookupIP("some.nonexistent")
}
}
func BenchmarkGoLookupIPWithBrokenNameServer(b *testing.B) {
onceLoadConfig.Do(loadDefaultConfig)
if cfg.dnserr != nil || cfg.dnsConfig == nil {
b.Fatalf("loadConfig failed: %v", cfg.dnserr)
}
// This looks ugly but it's safe as long as benchmarks are run
// sequentially in package testing.
orig := cfg.dnsConfig
cfg.dnsConfig.servers = append([]string{"203.0.113.254"}, cfg.dnsConfig.servers...) // use TEST-NET-3 block, see RFC 5737
for i := 0; i < b.N; i++ {
goLookupIP("www.example.com")
}
cfg.dnsConfig = orig
}