Skip to content

Instantly share code, notes, and snippets.

@aloknnikhil
Created February 10, 2020 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aloknnikhil/5782d3c99b966b54c98cde91239d32a2 to your computer and use it in GitHub Desktop.
Save aloknnikhil/5782d3c99b966b54c98cde91239d32a2 to your computer and use it in GitHub Desktop.
UDS throughput benchmark (Golang)
package test
import (
"fmt"
"net"
"os"
"testing"
"time"
)
var (
done chan bool
msgSize int = 1024
)
func read(n net.Listener) {
defer n.Close()
defer os.Remove("/tmp/uds_test.sock")
c, err := n.Accept()
if err != nil {
panic(err)
}
buf := make([]byte, msgSize)
msgs := int64(1)
start := time.Now().UnixNano()
for {
_, err := c.Read(buf[:])
if err != nil {
panic(err)
}
if msgs%100000 == 0 {
sample := time.Now().UnixNano()
fmt.Printf("Throughput: %f/sec (%d message size)\n", float64(msgs*int64(time.Second))/float64((sample-start)), msgSize)
}
msgs++
}
}
func write() {
c, err := net.Dial("unix", "/tmp/uds_test.sock")
defer c.Close()
buf := make([]byte, msgSize)
for {
_, err = c.Write(buf)
if err != nil {
panic(err)
}
}
done <- true
}
func TestThroughput(t *testing.T) {
writers := 2
readers := 2
done = make(chan bool, writers)
n, err := net.Listen("unix", "/tmp/uds_test.sock")
if err != nil {
panic(err)
}
for i := 0; i < readers; i++ {
go read(n)
}
for i := 0; i < writers; i++ {
go write()
}
<-done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment