Commit 93553dd2 authored by Mikio Hara's avatar Mikio Hara

net: fix data race in benchmarks

Fixes #10307.

Change-Id: If70f36a6f1c4e465a47a0bc4d38b318424111106
Reviewed-on: https://go-review.googlesource.com/8330Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent 9b696be0
...@@ -219,8 +219,7 @@ func TestReloadResolvConfChange(t *testing.T) { ...@@ -219,8 +219,7 @@ func TestReloadResolvConfChange(t *testing.T) {
} }
func BenchmarkGoLookupIP(b *testing.B) { func BenchmarkGoLookupIP(b *testing.B) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
goLookupIP("www.example.com") goLookupIP("www.example.com")
...@@ -228,8 +227,7 @@ func BenchmarkGoLookupIP(b *testing.B) { ...@@ -228,8 +227,7 @@ func BenchmarkGoLookupIP(b *testing.B) {
} }
func BenchmarkGoLookupIPNoSuchHost(b *testing.B) { func BenchmarkGoLookupIPNoSuchHost(b *testing.B) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
goLookupIP("some.nonexistent") goLookupIP("some.nonexistent")
...@@ -237,8 +235,7 @@ func BenchmarkGoLookupIPNoSuchHost(b *testing.B) { ...@@ -237,8 +235,7 @@ func BenchmarkGoLookupIPNoSuchHost(b *testing.B) {
} }
func BenchmarkGoLookupIPWithBrokenNameServer(b *testing.B) { func BenchmarkGoLookupIPWithBrokenNameServer(b *testing.B) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
onceLoadConfig.Do(loadDefaultConfig) onceLoadConfig.Do(loadDefaultConfig)
if cfg.dnserr != nil || cfg.dnsConfig == nil { if cfg.dnserr != nil || cfg.dnsConfig == nil {
......
...@@ -69,8 +69,7 @@ func TestDNSNames(t *testing.T) { ...@@ -69,8 +69,7 @@ func TestDNSNames(t *testing.T) {
} }
func BenchmarkDNSNames(b *testing.B) { func BenchmarkDNSNames(b *testing.B) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
benchmarks := append(tests, []testCase{ benchmarks := append(tests, []testCase{
{strings.Repeat("a", 63), true}, {strings.Repeat("a", 63), true},
......
...@@ -229,8 +229,7 @@ func testMulticastAddrs(t *testing.T, ifmat []Addr) (nmaf4, nmaf6 int) { ...@@ -229,8 +229,7 @@ func testMulticastAddrs(t *testing.T, ifmat []Addr) (nmaf4, nmaf6 int) {
} }
func BenchmarkInterfaces(b *testing.B) { func BenchmarkInterfaces(b *testing.B) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if _, err := Interfaces(); err != nil { if _, err := Interfaces(); err != nil {
...@@ -240,8 +239,7 @@ func BenchmarkInterfaces(b *testing.B) { ...@@ -240,8 +239,7 @@ func BenchmarkInterfaces(b *testing.B) {
} }
func BenchmarkInterfaceByIndex(b *testing.B) { func BenchmarkInterfaceByIndex(b *testing.B) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
ifi := loopbackInterface() ifi := loopbackInterface()
if ifi == nil { if ifi == nil {
...@@ -255,8 +253,7 @@ func BenchmarkInterfaceByIndex(b *testing.B) { ...@@ -255,8 +253,7 @@ func BenchmarkInterfaceByIndex(b *testing.B) {
} }
func BenchmarkInterfaceByName(b *testing.B) { func BenchmarkInterfaceByName(b *testing.B) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
ifi := loopbackInterface() ifi := loopbackInterface()
if ifi == nil { if ifi == nil {
...@@ -270,8 +267,7 @@ func BenchmarkInterfaceByName(b *testing.B) { ...@@ -270,8 +267,7 @@ func BenchmarkInterfaceByName(b *testing.B) {
} }
func BenchmarkInterfaceAddrs(b *testing.B) { func BenchmarkInterfaceAddrs(b *testing.B) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
if _, err := InterfaceAddrs(); err != nil { if _, err := InterfaceAddrs(); err != nil {
...@@ -281,8 +277,7 @@ func BenchmarkInterfaceAddrs(b *testing.B) { ...@@ -281,8 +277,7 @@ func BenchmarkInterfaceAddrs(b *testing.B) {
} }
func BenchmarkInterfacesAndAddrs(b *testing.B) { func BenchmarkInterfacesAndAddrs(b *testing.B) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
ifi := loopbackInterface() ifi := loopbackInterface()
if ifi == nil { if ifi == nil {
...@@ -296,8 +291,7 @@ func BenchmarkInterfacesAndAddrs(b *testing.B) { ...@@ -296,8 +291,7 @@ func BenchmarkInterfacesAndAddrs(b *testing.B) {
} }
func BenchmarkInterfacesAndMulticastAddrs(b *testing.B) { func BenchmarkInterfacesAndMulticastAddrs(b *testing.B) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
ifi := loopbackInterface() ifi := loopbackInterface()
if ifi == nil { if ifi == nil {
......
...@@ -53,8 +53,7 @@ func TestParseIP(t *testing.T) { ...@@ -53,8 +53,7 @@ func TestParseIP(t *testing.T) {
} }
func BenchmarkParseIP(b *testing.B) { func BenchmarkParseIP(b *testing.B) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for _, tt := range parseIPTests { for _, tt := range parseIPTests {
...@@ -111,8 +110,7 @@ func TestIPString(t *testing.T) { ...@@ -111,8 +110,7 @@ func TestIPString(t *testing.T) {
} }
func BenchmarkIPString(b *testing.B) { func BenchmarkIPString(b *testing.B) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for _, tt := range ipStringTests { for _, tt := range ipStringTests {
...@@ -164,8 +162,7 @@ func TestIPMaskString(t *testing.T) { ...@@ -164,8 +162,7 @@ func TestIPMaskString(t *testing.T) {
} }
func BenchmarkIPMaskString(b *testing.B) { func BenchmarkIPMaskString(b *testing.B) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for _, tt := range ipMaskStringTests { for _, tt := range ipMaskStringTests {
......
...@@ -11,23 +11,29 @@ import ( ...@@ -11,23 +11,29 @@ import (
"runtime" "runtime"
"sort" "sort"
"strings" "strings"
"sync"
"testing" "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) { func TestMain(m *testing.M) {
installTestHooks() installTestHooks()
st := m.Run() st := m.Run()
testHookUninstaller.Do(func() { uninstallTestHooks() })
if !testing.Short() { if !testing.Short() {
printLeakedGoroutines() printLeakedGoroutines()
printLeakedSockets() printLeakedSockets()
printSocketStats() printSocketStats()
} }
forceCloseSockets() forceCloseSockets()
uninstallTestHooks()
os.Exit(st) os.Exit(st)
} }
......
...@@ -59,8 +59,7 @@ func BenchmarkTCP6PersistentTimeout(b *testing.B) { ...@@ -59,8 +59,7 @@ func BenchmarkTCP6PersistentTimeout(b *testing.B) {
} }
func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) { func benchmarkTCP(b *testing.B, persistent, timeout bool, laddr string) {
uninstallTestHooks() testHookUninstaller.Do(func() { uninstallTestHooks() })
defer installTestHooks()
const msgLen = 512 const msgLen = 512
conns := b.N conns := b.N
...@@ -170,13 +169,13 @@ func BenchmarkTCP6ConcurrentReadWrite(b *testing.B) { ...@@ -170,13 +169,13 @@ func BenchmarkTCP6ConcurrentReadWrite(b *testing.B) {
} }
func benchmarkTCPConcurrentReadWrite(b *testing.B, laddr string) { func benchmarkTCPConcurrentReadWrite(b *testing.B, laddr string) {
testHookUninstaller.Do(func() { uninstallTestHooks() })
// The benchmark creates GOMAXPROCS client/server pairs. // The benchmark creates GOMAXPROCS client/server pairs.
// Each pair creates 4 goroutines: client reader/writer and server reader/writer. // Each pair creates 4 goroutines: client reader/writer and server reader/writer.
// The benchmark stresses concurrent reading and writing to the same connection. // The benchmark stresses concurrent reading and writing to the same connection.
// Such pattern is used in net/http and net/rpc. // Such pattern is used in net/http and net/rpc.
uninstallTestHooks()
defer installTestHooks()
b.StopTimer() b.StopTimer()
P := runtime.GOMAXPROCS(0) P := runtime.GOMAXPROCS(0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment