2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-11-14 11:31:28 +08:00
cmux/bench_test.go

143 lines
2.7 KiB
Go
Raw Normal View History

// Copyright 2016 The CMux Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
2015-08-01 23:57:42 +08:00
package cmux
import (
"bytes"
"io"
"net"
"sync"
2015-08-01 23:57:42 +08:00
"testing"
2016-09-06 01:56:50 +08:00
"time"
"golang.org/x/net/http2"
)
var (
benchHTTP1Payload = make([]byte, 4096)
benchHTTP2Payload = make([]byte, 4096)
2015-08-01 23:57:42 +08:00
)
func init() {
copy(benchHTTP1Payload, []byte("GET http://www.w3.org/ HTTP/1.1"))
copy(benchHTTP2Payload, http2.ClientPreface)
}
2015-08-01 23:57:42 +08:00
type mockConn struct {
net.Conn
r io.Reader
}
func (c *mockConn) Read(b []byte) (n int, err error) {
return c.r.Read(b)
}
2016-09-06 01:56:50 +08:00
func (c *mockConn) SetReadDeadline(time.Time) error {
return nil
}
func discard(l net.Listener) {
for {
if _, err := l.Accept(); err != nil {
return
}
}
}
2015-08-01 23:57:42 +08:00
func BenchmarkCMuxConnHTTP1(b *testing.B) {
2015-08-01 23:57:42 +08:00
m := New(nil).(*cMux)
l := m.Match(HTTP1Fast())
go discard(l)
donec := make(chan struct{})
var wg sync.WaitGroup
wg.Add(b.N)
b.ResetTimer()
2016-07-13 00:13:54 +08:00
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
wg.Add(1)
m.serve(&mockConn{
r: bytes.NewReader(benchHTTP1Payload),
}, donec, &wg)
2015-08-01 23:57:42 +08:00
}
2016-07-13 00:13:54 +08:00
})
}
func BenchmarkCMuxConnHTTP2(b *testing.B) {
m := New(nil).(*cMux)
l := m.Match(HTTP2())
go discard(l)
donec := make(chan struct{})
var wg sync.WaitGroup
wg.Add(b.N)
b.ResetTimer()
2016-07-13 00:13:54 +08:00
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
wg.Add(1)
m.serve(&mockConn{
r: bytes.NewReader(benchHTTP2Payload),
}, donec, &wg)
}
2016-07-13 00:13:54 +08:00
})
}
func BenchmarkCMuxConnHTTP1n2(b *testing.B) {
m := New(nil).(*cMux)
l1 := m.Match(HTTP1Fast())
l2 := m.Match(HTTP2())
go discard(l1)
go discard(l2)
donec := make(chan struct{})
var wg sync.WaitGroup
b.ResetTimer()
2016-07-13 00:13:54 +08:00
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
wg.Add(1)
m.serve(&mockConn{
r: bytes.NewReader(benchHTTP2Payload),
}, donec, &wg)
}
2016-07-13 00:13:54 +08:00
})
}
func BenchmarkCMuxConnHTTP2n1(b *testing.B) {
m := New(nil).(*cMux)
l2 := m.Match(HTTP2())
l1 := m.Match(HTTP1Fast())
go discard(l1)
go discard(l2)
2015-08-01 23:57:42 +08:00
donec := make(chan struct{})
var wg sync.WaitGroup
2015-08-01 23:57:42 +08:00
b.ResetTimer()
2016-07-13 00:13:54 +08:00
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
wg.Add(1)
m.serve(&mockConn{
r: bytes.NewReader(benchHTTP1Payload),
}, donec, &wg)
2015-08-01 23:57:42 +08:00
}
2016-07-13 00:13:54 +08:00
})
2015-08-01 23:57:42 +08:00
}