mirror of
https://github.com/soheilhy/cmux.git
synced 2025-01-18 18:56:26 +08:00
Add micro-benchmarks
This commit is contained in:
parent
5b048e6641
commit
77815df398
44
bench_test.go
Normal file
44
bench_test.go
Normal file
@ -0,0 +1,44 @@
|
||||
package cmux
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"net"
|
||||
"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) {
|
||||
b.StopTimer()
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
b.StartTimer()
|
||||
|
||||
for i := 0; i < b.N; i++ {
|
||||
c := &mockConn{
|
||||
r: bytes.NewReader(benchHTTPPayload),
|
||||
}
|
||||
m.serve(c)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user