|
| 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 | +} |
0 commit comments