2015-08-01 23:57:42 +08:00
|
|
|
package cmux
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"net"
|
2015-12-12 03:33:39 +08:00
|
|
|
"sync"
|
2015-08-01 23:57:42 +08:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
type mockConn struct {
|
|
|
|
net.Conn
|
|
|
|
r io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *mockConn) Read(b []byte) (n int, err error) {
|
|
|
|
return c.r.Read(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkCMuxConn(b *testing.B) {
|
|
|
|
benchHTTPPayload := make([]byte, 4096)
|
|
|
|
copy(benchHTTPPayload, []byte("GET http://www.w3.org/ HTTP/1.1"))
|
|
|
|
|
|
|
|
m := New(nil).(*cMux)
|
|
|
|
l := m.Match(HTTP1Fast())
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
if _, err := l.Accept(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-12-12 03:33:39 +08:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(b.N)
|
2015-08-01 23:57:42 +08:00
|
|
|
|
2015-12-12 03:33:39 +08:00
|
|
|
b.ResetTimer()
|
2015-08-01 23:57:42 +08:00
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
c := &mockConn{
|
|
|
|
r: bytes.NewReader(benchHTTPPayload),
|
|
|
|
}
|
2016-02-24 00:29:57 +08:00
|
|
|
m.serve(c, &wg)
|
2015-08-01 23:57:42 +08:00
|
|
|
}
|
|
|
|
}
|