Last active
January 1, 2020 12:09
-
-
Save erikdubbelboer/d925c6e271aa0c5c8bb20a6ec16455c5 to your computer and use it in GitHub Desktop.
net: 1.14 performance regression on mac: https://github.com/golang/go/issues/36298
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net" | |
"sync/atomic" | |
"time" | |
) | |
var ( | |
duration = int64(0) | |
count = int64(0) | |
connections = int64(0) | |
responseDelay = time.Millisecond * 200 // Wait 200ms before sending the response. | |
) | |
func handler(c net.Conn) { | |
buff := make([]byte, 4) | |
for { | |
if _, err := c.Read(buff); err != nil { | |
panic(err) | |
} | |
time.Sleep(responseDelay) | |
if _, err := c.Write(buff); err != nil { | |
panic(err) | |
} | |
} | |
} | |
func listener(ln net.Listener) { | |
for { | |
c, err := ln.Accept() | |
if err != nil { | |
panic(err) | |
} | |
go handler(c) | |
} | |
} | |
func worker() { | |
c, err := net.Dial("tcp", "127.0.0.1:5555") | |
if err != nil { | |
panic(err) | |
} | |
atomic.AddInt64(&connections, 1) | |
buff := []byte{1, 2, 3, 4} | |
for { | |
start := time.Now() | |
if _, err := c.Write(buff); err != nil { | |
panic(err) | |
} | |
if _, err := c.Read(buff); err != nil { | |
panic(err) | |
} | |
d := int64(time.Since(start)) | |
atomic.AddInt64(&duration, d) | |
atomic.AddInt64(&count, 1) | |
} | |
} | |
func startup() { | |
for i := 0; i < 9000; i++ { | |
go worker() | |
time.Sleep(time.Millisecond * 2) | |
} | |
} | |
func main() { | |
ln, err := net.Listen("tcp", "127.0.0.1:5555") | |
if err != nil { | |
panic(err) | |
} | |
go listener(ln) | |
go startup() | |
for { | |
time.Sleep(time.Second) | |
n := atomic.LoadInt64(&connections) | |
d := atomic.SwapInt64(&duration, 0) | |
c := atomic.SwapInt64(&count, 0) | |
if c > 0 { | |
fmt.Printf("connections: % 4d latency: % 12v (requests: %d)\n", n, time.Duration(d/c), c) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment