github.com/tumi8/quic-go@v0.37.4-tum/integrationtests/self/benchmark_test.go (about)

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