Skip to content

Commit 117945d

Browse files
committed
quic: add throughput and stream creation benchmarks
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]>
1 parent 93be8fe commit 117945d

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

internal/quic/bench_test.go

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build go1.21
6+
7+
package quic
8+
9+
import (
10+
"context"
11+
"fmt"
12+
"io"
13+
"math"
14+
"testing"
15+
)
16+
17+
// BenchmarkThroughput is based on the crypto/tls benchmark of the same name.
18+
func BenchmarkThroughput(b *testing.B) {
19+
for size := 1; size <= 64; size <<= 1 {
20+
name := fmt.Sprintf("%dMiB", size)
21+
b.Run(name, func(b *testing.B) {
22+
throughput(b, int64(size<<20))
23+
})
24+
}
25+
}
26+
27+
func throughput(b *testing.B, totalBytes int64) {
28+
// Same buffer size as crypto/tls's BenchmarkThroughput, for consistency.
29+
const bufsize = 32 << 10
30+
31+
cli, srv := newLocalConnPair(b, &Config{}, &Config{})
32+
33+
go func() {
34+
buf := make([]byte, bufsize)
35+
for i := 0; i < b.N; i++ {
36+
sconn, err := srv.AcceptStream(context.Background())
37+
if err != nil {
38+
panic(fmt.Errorf("AcceptStream: %v", err))
39+
}
40+
if _, err := io.CopyBuffer(sconn, sconn, buf); err != nil {
41+
panic(fmt.Errorf("CopyBuffer: %v", err))
42+
}
43+
sconn.Close()
44+
}
45+
}()
46+
47+
b.SetBytes(totalBytes)
48+
buf := make([]byte, bufsize)
49+
chunks := int(math.Ceil(float64(totalBytes) / float64(len(buf))))
50+
for i := 0; i < b.N; i++ {
51+
cconn, err := cli.NewStream(context.Background())
52+
if err != nil {
53+
b.Fatalf("NewStream: %v", err)
54+
}
55+
closec := make(chan struct{})
56+
go func() {
57+
defer close(closec)
58+
buf := make([]byte, bufsize)
59+
if _, err := io.CopyBuffer(io.Discard, cconn, buf); err != nil {
60+
panic(fmt.Errorf("Discard: %v", err))
61+
}
62+
}()
63+
for j := 0; j < chunks; j++ {
64+
_, err := cconn.Write(buf)
65+
if err != nil {
66+
b.Fatalf("Write: %v", err)
67+
}
68+
}
69+
cconn.CloseWrite()
70+
<-closec
71+
cconn.Close()
72+
}
73+
}
74+
75+
func BenchmarkStreamCreation(b *testing.B) {
76+
cli, srv := newLocalConnPair(b, &Config{}, &Config{})
77+
78+
go func() {
79+
for i := 0; i < b.N; i++ {
80+
sconn, err := srv.AcceptStream(context.Background())
81+
if err != nil {
82+
panic(fmt.Errorf("AcceptStream: %v", err))
83+
}
84+
sconn.Close()
85+
}
86+
}()
87+
88+
buf := make([]byte, 1)
89+
for i := 0; i < b.N; i++ {
90+
cconn, err := cli.NewStream(context.Background())
91+
if err != nil {
92+
b.Fatalf("NewStream: %v", err)
93+
}
94+
cconn.Write(buf)
95+
cconn.Flush()
96+
cconn.Read(buf)
97+
cconn.Close()
98+
}
99+
}

internal/quic/endpoint_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func TestStreamTransfer(t *testing.T) {
6363
}
6464
}
6565

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

82-
func newLocalEndpoint(t *testing.T, side connSide, conf *Config) *Endpoint {
82+
func newLocalEndpoint(t testing.TB, side connSide, conf *Config) *Endpoint {
8383
t.Helper()
8484
if conf.TLSConfig == nil {
8585
newConf := *conf

0 commit comments

Comments
 (0)