Fixes #10307.
Change-Id: If70f36a6f1c4e465a47a0bc4d38b318424111106
Reviewed-on: https://go-review.googlesource.com/8330
Reviewed-by: Ian Lance Taylor <iant@golang.org>
}
func BenchmarkGoLookupIP(b *testing.B) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
for i := 0; i < b.N; i++ {
goLookupIP("www.example.com")
}
func BenchmarkGoLookupIPNoSuchHost(b *testing.B) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
for i := 0; i < b.N; i++ {
goLookupIP("some.nonexistent")
}
func BenchmarkGoLookupIPWithBrokenNameServer(b *testing.B) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
onceLoadConfig.Do(loadDefaultConfig)
if cfg.dnserr != nil || cfg.dnsConfig == nil {
}
func BenchmarkDNSNames(b *testing.B) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
benchmarks := append(tests, []testCase{
{strings.Repeat("a", 63), true},
}
func BenchmarkInterfaces(b *testing.B) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
for i := 0; i < b.N; i++ {
if _, err := Interfaces(); err != nil {
}
func BenchmarkInterfaceByIndex(b *testing.B) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
ifi := loopbackInterface()
if ifi == nil {
}
func BenchmarkInterfaceByName(b *testing.B) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
ifi := loopbackInterface()
if ifi == nil {
}
func BenchmarkInterfaceAddrs(b *testing.B) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
for i := 0; i < b.N; i++ {
if _, err := InterfaceAddrs(); err != nil {
}
func BenchmarkInterfacesAndAddrs(b *testing.B) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
ifi := loopbackInterface()
if ifi == nil {
}
func BenchmarkInterfacesAndMulticastAddrs(b *testing.B) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
ifi := loopbackInterface()
if ifi == nil {
}
func BenchmarkParseIP(b *testing.B) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
for i := 0; i < b.N; i++ {
for _, tt := range parseIPTests {
}
func BenchmarkIPString(b *testing.B) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
for i := 0; i < b.N; i++ {
for _, tt := range ipStringTests {
}
func BenchmarkIPMaskString(b *testing.B) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
for i := 0; i < b.N; i++ {
for _, tt := range ipMaskStringTests {
"runtime"
"sort"
"strings"
+ "sync"
"testing"
)
-var sw socktest.Switch
+var (
+ sw socktest.Switch
+
+ // uninstallTestHooks runs just before a run of benchmarks.
+ testHookUninstaller sync.Once
+)
func TestMain(m *testing.M) {
installTestHooks()
st := m.Run()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
if !testing.Short() {
printLeakedGoroutines()
printLeakedSockets()
printSocketStats()
}
forceCloseSockets()
- uninstallTestHooks()
os.Exit(st)
}
}
func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) {
- uninstallTestHooks()
- defer installTestHooks()
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
const msgLen = 512
conns := b.N
}
func benchmarkTCPConcurrentReadWrite(b *testing.B, laddr string) {
+ testHookUninstaller.Do(func() { uninstallTestHooks() })
+
// The benchmark creates GOMAXPROCS client/server pairs.
// Each pair creates 4 goroutines: client reader/writer and server reader/writer.
// The benchmark stresses concurrent reading and writing to the same connection.
// Such pattern is used in net/http and net/rpc.
- uninstallTestHooks()
- defer installTestHooks()
b.StopTimer()
P := runtime.GOMAXPROCS(0)