add a benchmark allocation test for opening / accepting streams

This commit is contained in:
Marten Seemann 2023-02-04 18:42:49 +13:00
parent b791bb6cdf
commit 97dae87bf4

View file

@ -2,6 +2,7 @@ package self_test
import (
"context"
"fmt"
"net"
"testing"
@ -43,3 +44,49 @@ func BenchmarkHandshake(b *testing.B) {
c.CloseWithError(0, "")
}
}
func BenchmarkStreamChurn(b *testing.B) {
b.ReportAllocs()
ln, err := quic.ListenAddr("localhost:0", tlsConfig, &quic.Config{MaxIncomingStreams: 1e10})
if err != nil {
b.Fatal(err)
}
defer ln.Close()
errChan := make(chan error, 1)
go func() {
conn, err := ln.Accept(context.Background())
if err != nil {
errChan <- err
return
}
close(errChan)
for {
str, err := conn.AcceptStream(context.Background())
if err != nil {
return
}
str.Close()
}
}()
c, err := quic.DialAddr(fmt.Sprintf("localhost:%d", ln.Addr().(*net.UDPAddr).Port), tlsClientConfig, nil)
if err != nil {
b.Fatal(err)
}
if err := <-errChan; err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
str, err := c.OpenStreamSync(context.Background())
if err != nil {
b.Fatal(err)
}
if err := str.Close(); err != nil {
b.Fatal(err)
}
}
}