...

Source file src/github.com/moby/spdystream/spdy_bench_test.go

Documentation: github.com/moby/spdystream

     1  /*
     2     Copyright 2014-2021 Docker Inc.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package spdystream
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"net"
    23  	"net/http"
    24  	"sync"
    25  	"testing"
    26  )
    27  
    28  func configureServer() (io.Closer, string, *sync.WaitGroup) {
    29  	authenticated = true
    30  	wg := &sync.WaitGroup{}
    31  	server, listen, serverErr := runServer(wg)
    32  
    33  	if serverErr != nil {
    34  		panic(serverErr)
    35  	}
    36  
    37  	return server, listen, wg
    38  }
    39  
    40  func BenchmarkDial10000(b *testing.B) {
    41  	server, addr, wg := configureServer()
    42  
    43  	defer func() {
    44  		server.Close()
    45  		wg.Wait()
    46  	}()
    47  
    48  	for i := 0; i < b.N; i++ {
    49  		conn, dialErr := net.Dial("tcp", addr)
    50  		if dialErr != nil {
    51  			panic(fmt.Sprintf("Error dialing server: %s", dialErr))
    52  		}
    53  		conn.Close()
    54  	}
    55  }
    56  
    57  func BenchmarkDialWithSPDYStream10000(b *testing.B) {
    58  	server, addr, wg := configureServer()
    59  
    60  	defer func() {
    61  		server.Close()
    62  		wg.Wait()
    63  	}()
    64  
    65  	for i := 0; i < b.N; i++ {
    66  		conn, dialErr := net.Dial("tcp", addr)
    67  		if dialErr != nil {
    68  			b.Fatalf("Error dialing server: %s", dialErr)
    69  		}
    70  
    71  		spdyConn, spdyErr := NewConnection(conn, false)
    72  		if spdyErr != nil {
    73  			b.Fatalf("Error creating spdy connection: %s", spdyErr)
    74  		}
    75  		go spdyConn.Serve(NoOpStreamHandler)
    76  
    77  		closeErr := spdyConn.Close()
    78  		if closeErr != nil {
    79  			b.Fatalf("Error closing connection: %s", closeErr)
    80  		}
    81  	}
    82  }
    83  
    84  func benchmarkStreamWithDataAndSize(size uint64, b *testing.B) {
    85  	server, addr, wg := configureServer()
    86  
    87  	defer func() {
    88  		server.Close()
    89  		wg.Wait()
    90  	}()
    91  
    92  	for i := 0; i < b.N; i++ {
    93  		conn, dialErr := net.Dial("tcp", addr)
    94  		if dialErr != nil {
    95  			b.Fatalf("Error dialing server: %s", dialErr)
    96  		}
    97  
    98  		spdyConn, spdyErr := NewConnection(conn, false)
    99  		if spdyErr != nil {
   100  			b.Fatalf("Error creating spdy connection: %s", spdyErr)
   101  		}
   102  
   103  		go spdyConn.Serve(MirrorStreamHandler)
   104  
   105  		stream, err := spdyConn.CreateStream(http.Header{}, nil, false)
   106  
   107  		writer := make([]byte, size)
   108  
   109  		stream.Write(writer)
   110  
   111  		if err != nil {
   112  			panic(err)
   113  		}
   114  
   115  		reader := make([]byte, size)
   116  		stream.Read(reader)
   117  
   118  		stream.Close()
   119  
   120  		closeErr := spdyConn.Close()
   121  		if closeErr != nil {
   122  			b.Fatalf("Error closing connection: %s", closeErr)
   123  		}
   124  	}
   125  }
   126  
   127  func BenchmarkStreamWith1Byte10000(b *testing.B)     { benchmarkStreamWithDataAndSize(1, b) }
   128  func BenchmarkStreamWith1KiloByte10000(b *testing.B) { benchmarkStreamWithDataAndSize(1024, b) }
   129  func BenchmarkStreamWith1Megabyte10000(b *testing.B) { benchmarkStreamWithDataAndSize(1024*1024, b) }
   130  

View as plain text