Skip to content

Commit

Permalink
quic: add throughput and stream creation benchmarks
Browse files Browse the repository at this point in the history
For golang/go#58547

Change-Id: Ie62fcf596bf020bda5a167f7a0d3d95bac9e591a
Reviewed-on: https://go-review.googlesource.com/c/net/+/564475
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Jonathan Amsterdam <[email protected]>
  • Loading branch information
neild committed Feb 15, 2024
1 parent 93be8fe commit 117945d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 2 deletions.
99 changes: 99 additions & 0 deletions internal/quic/bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.21

package quic

import (
"context"
"fmt"
"io"
"math"
"testing"
)

// BenchmarkThroughput is based on the crypto/tls benchmark of the same name.
func BenchmarkThroughput(b *testing.B) {
for size := 1; size <= 64; size <<= 1 {
name := fmt.Sprintf("%dMiB", size)
b.Run(name, func(b *testing.B) {
throughput(b, int64(size<<20))
})
}
}

func throughput(b *testing.B, totalBytes int64) {
// Same buffer size as crypto/tls's BenchmarkThroughput, for consistency.
const bufsize = 32 << 10

cli, srv := newLocalConnPair(b, &Config{}, &Config{})

go func() {
buf := make([]byte, bufsize)
for i := 0; i < b.N; i++ {
sconn, err := srv.AcceptStream(context.Background())
if err != nil {
panic(fmt.Errorf("AcceptStream: %v", err))
}
if _, err := io.CopyBuffer(sconn, sconn, buf); err != nil {
panic(fmt.Errorf("CopyBuffer: %v", err))
}
sconn.Close()
}
}()

b.SetBytes(totalBytes)
buf := make([]byte, bufsize)
chunks := int(math.Ceil(float64(totalBytes) / float64(len(buf))))
for i := 0; i < b.N; i++ {
cconn, err := cli.NewStream(context.Background())
if err != nil {
b.Fatalf("NewStream: %v", err)
}
closec := make(chan struct{})
go func() {
defer close(closec)
buf := make([]byte, bufsize)
if _, err := io.CopyBuffer(io.Discard, cconn, buf); err != nil {
panic(fmt.Errorf("Discard: %v", err))
}
}()
for j := 0; j < chunks; j++ {
_, err := cconn.Write(buf)
if err != nil {
b.Fatalf("Write: %v", err)
}
}
cconn.CloseWrite()
<-closec
cconn.Close()
}
}

func BenchmarkStreamCreation(b *testing.B) {
cli, srv := newLocalConnPair(b, &Config{}, &Config{})

go func() {
for i := 0; i < b.N; i++ {
sconn, err := srv.AcceptStream(context.Background())
if err != nil {
panic(fmt.Errorf("AcceptStream: %v", err))
}
sconn.Close()
}
}()

buf := make([]byte, 1)
for i := 0; i < b.N; i++ {
cconn, err := cli.NewStream(context.Background())
if err != nil {
b.Fatalf("NewStream: %v", err)
}
cconn.Write(buf)
cconn.Flush()
cconn.Read(buf)
cconn.Close()
}
}
4 changes: 2 additions & 2 deletions internal/quic/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestStreamTransfer(t *testing.T) {
}
}

func newLocalConnPair(t *testing.T, conf1, conf2 *Config) (clientConn, serverConn *Conn) {
func newLocalConnPair(t testing.TB, conf1, conf2 *Config) (clientConn, serverConn *Conn) {
t.Helper()
ctx := context.Background()
e1 := newLocalEndpoint(t, serverSide, conf1)
Expand All @@ -79,7 +79,7 @@ func newLocalConnPair(t *testing.T, conf1, conf2 *Config) (clientConn, serverCon
return c2, c1
}

func newLocalEndpoint(t *testing.T, side connSide, conf *Config) *Endpoint {
func newLocalEndpoint(t testing.TB, side connSide, conf *Config) *Endpoint {
t.Helper()
if conf.TLSConfig == nil {
newConf := *conf
Expand Down

0 comments on commit 117945d

Please sign in to comment.