...

Source file src/github.com/soheilhy/cmux/bench_test.go

Documentation: github.com/soheilhy/cmux

     1  // Copyright 2016 The CMux Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    12  // implied. See the License for the specific language governing
    13  // permissions and limitations under the License.
    14  
    15  package cmux
    16  
    17  import (
    18  	"bytes"
    19  	"io"
    20  	"net"
    21  	"sync"
    22  	"testing"
    23  	"time"
    24  
    25  	"golang.org/x/net/http2"
    26  )
    27  
    28  var (
    29  	benchHTTP1Payload = make([]byte, 4096)
    30  	benchHTTP2Payload = make([]byte, 4096)
    31  )
    32  
    33  func init() {
    34  	copy(benchHTTP1Payload, []byte("GET http://www.w3.org/ HTTP/1.1"))
    35  	copy(benchHTTP2Payload, http2.ClientPreface)
    36  }
    37  
    38  type mockConn struct {
    39  	net.Conn
    40  	r io.Reader
    41  }
    42  
    43  func (c *mockConn) Read(b []byte) (n int, err error) {
    44  	return c.r.Read(b)
    45  }
    46  
    47  func (c *mockConn) SetReadDeadline(time.Time) error {
    48  	return nil
    49  }
    50  
    51  func discard(l net.Listener) {
    52  	for {
    53  		if _, err := l.Accept(); err != nil {
    54  			return
    55  		}
    56  	}
    57  }
    58  
    59  func BenchmarkCMuxConnHTTP1(b *testing.B) {
    60  	m := New(nil).(*cMux)
    61  	l := m.Match(HTTP1Fast())
    62  
    63  	go discard(l)
    64  
    65  	donec := make(chan struct{})
    66  	var wg sync.WaitGroup
    67  	wg.Add(b.N)
    68  
    69  	b.ResetTimer()
    70  	b.RunParallel(func(pb *testing.PB) {
    71  		for pb.Next() {
    72  			wg.Add(1)
    73  			m.serve(&mockConn{
    74  				r: bytes.NewReader(benchHTTP1Payload),
    75  			}, donec, &wg)
    76  		}
    77  	})
    78  }
    79  
    80  func BenchmarkCMuxConnHTTP2(b *testing.B) {
    81  	m := New(nil).(*cMux)
    82  	l := m.Match(HTTP2())
    83  	go discard(l)
    84  
    85  	donec := make(chan struct{})
    86  	var wg sync.WaitGroup
    87  	wg.Add(b.N)
    88  
    89  	b.ResetTimer()
    90  	b.RunParallel(func(pb *testing.PB) {
    91  		for pb.Next() {
    92  			wg.Add(1)
    93  			m.serve(&mockConn{
    94  				r: bytes.NewReader(benchHTTP2Payload),
    95  			}, donec, &wg)
    96  		}
    97  	})
    98  }
    99  
   100  func BenchmarkCMuxConnHTTP1n2(b *testing.B) {
   101  	m := New(nil).(*cMux)
   102  	l1 := m.Match(HTTP1Fast())
   103  	l2 := m.Match(HTTP2())
   104  
   105  	go discard(l1)
   106  	go discard(l2)
   107  
   108  	donec := make(chan struct{})
   109  	var wg sync.WaitGroup
   110  
   111  	b.ResetTimer()
   112  	b.RunParallel(func(pb *testing.PB) {
   113  		for pb.Next() {
   114  			wg.Add(1)
   115  			m.serve(&mockConn{
   116  				r: bytes.NewReader(benchHTTP2Payload),
   117  			}, donec, &wg)
   118  		}
   119  	})
   120  }
   121  
   122  func BenchmarkCMuxConnHTTP2n1(b *testing.B) {
   123  	m := New(nil).(*cMux)
   124  	l2 := m.Match(HTTP2())
   125  	l1 := m.Match(HTTP1Fast())
   126  
   127  	go discard(l1)
   128  	go discard(l2)
   129  
   130  	donec := make(chan struct{})
   131  	var wg sync.WaitGroup
   132  
   133  	b.ResetTimer()
   134  	b.RunParallel(func(pb *testing.PB) {
   135  		for pb.Next() {
   136  			wg.Add(1)
   137  			m.serve(&mockConn{
   138  				r: bytes.NewReader(benchHTTP1Payload),
   139  			}, donec, &wg)
   140  		}
   141  	})
   142  }
   143  

View as plain text