github.com/MerlinKodo/quic-go@v0.39.2/integrationtests/self/benchmark_test.go (about)

     1  package self_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  	"testing"
     8  
     9  	"github.com/MerlinKodo/quic-go"
    10  )
    11  
    12  func BenchmarkHandshake(b *testing.B) {
    13  	b.ReportAllocs()
    14  
    15  	ln, err := quic.ListenAddr("localhost:0", tlsConfig, nil)
    16  	if err != nil {
    17  		b.Fatal(err)
    18  	}
    19  	defer ln.Close()
    20  
    21  	connChan := make(chan quic.Connection, 1)
    22  	go func() {
    23  		for {
    24  			conn, err := ln.Accept(context.Background())
    25  			if err != nil {
    26  				return
    27  			}
    28  			connChan <- conn
    29  		}
    30  	}()
    31  
    32  	conn, err := net.ListenUDP("udp", nil)
    33  	if err != nil {
    34  		b.Fatal(err)
    35  	}
    36  
    37  	b.ResetTimer()
    38  	for i := 0; i < b.N; i++ {
    39  		c, err := quic.Dial(context.Background(), conn, ln.Addr(), tlsClientConfig, nil)
    40  		if err != nil {
    41  			b.Fatal(err)
    42  		}
    43  		<-connChan
    44  		c.CloseWithError(0, "")
    45  	}
    46  }
    47  
    48  func BenchmarkStreamChurn(b *testing.B) {
    49  	b.ReportAllocs()
    50  
    51  	ln, err := quic.ListenAddr("localhost:0", tlsConfig, &quic.Config{MaxIncomingStreams: 1e10})
    52  	if err != nil {
    53  		b.Fatal(err)
    54  	}
    55  	defer ln.Close()
    56  
    57  	errChan := make(chan error, 1)
    58  	go func() {
    59  		conn, err := ln.Accept(context.Background())
    60  		if err != nil {
    61  			errChan <- err
    62  			return
    63  		}
    64  		close(errChan)
    65  		for {
    66  			str, err := conn.AcceptStream(context.Background())
    67  			if err != nil {
    68  				return
    69  			}
    70  			str.Close()
    71  		}
    72  	}()
    73  
    74  	c, err := quic.DialAddr(context.Background(), fmt.Sprintf("localhost:%d", ln.Addr().(*net.UDPAddr).Port), tlsClientConfig, nil)
    75  	if err != nil {
    76  		b.Fatal(err)
    77  	}
    78  	if err := <-errChan; err != nil {
    79  		b.Fatal(err)
    80  	}
    81  
    82  	b.ResetTimer()
    83  	for i := 0; i < b.N; i++ {
    84  		str, err := c.OpenStreamSync(context.Background())
    85  		if err != nil {
    86  			b.Fatal(err)
    87  		}
    88  		if err := str.Close(); err != nil {
    89  			b.Fatal(err)
    90  		}
    91  	}
    92  }