Benchmark for websocketconn.Conn read/write.

Current output:
	$ go test -bench=BenchmarkReadWrite -benchmem -benchtime=5s
	BenchmarkReadWrite/c←s_150-4              451840             13904 ns/op          10.79 MB/s       34954 B/op          4 allocs/op
	BenchmarkReadWrite/s←c_150-4              452560             16134 ns/op           9.30 MB/s       36378 B/op          4 allocs/op
	BenchmarkReadWrite/c←s_3000-4             202950             40846 ns/op          73.45 MB/s       69833 B/op          8 allocs/op
	BenchmarkReadWrite/s←c_3000-4             189262             37930 ns/op          79.09 MB/s       69768 B/op          8 allocs/op
This commit is contained in:
David Fifield 2022-09-22 15:40:10 -06:00
parent 0780f2e809
commit 8cadcaee70

View file

@ -263,3 +263,44 @@ func TestClose(t *testing.T) {
t.Fatalf("Write after Close returned %v, expected %v", err, io.ErrClosedPipe)
}
}
// Benchmark read/write in the client←server and server←client directions, with
// messages of different sizes. Run with -benchmem to see memory allocations.
func BenchmarkReadWrite(b *testing.B) {
trial := func(b *testing.B, readConn, writeConn *Conn, msgSize int) {
go func() {
io.Copy(ioutil.Discard, readConn)
}()
data := make([]byte, msgSize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
n, err := writeConn.Write(data[:])
b.SetBytes(int64(n))
if err != nil {
b.Fatal(err)
}
}
}
for _, msgSize := range []int{150, 3000} {
s, c, err := connPair()
if err != nil {
b.Fatal(err)
}
b.Run(fmt.Sprintf("c←s %d", msgSize), func(b *testing.B) {
trial(b, c, s, msgSize)
})
b.Run(fmt.Sprintf("s←c %d", msgSize), func(b *testing.B) {
trial(b, s, c, msgSize)
})
err = s.Close()
if err != nil {
b.Fatal(err)
}
err = c.Close()
if err != nil {
b.Fatal(err)
}
}
}