From 77815df398f3bc5a00a6b0e47d8f85b10cd0173b Mon Sep 17 00:00:00 2001 From: Soheil Hassas Yeganeh Date: Sat, 1 Aug 2015 11:57:42 -0400 Subject: [PATCH] Add micro-benchmarks --- bench_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 bench_test.go diff --git a/bench_test.go b/bench_test.go new file mode 100644 index 0000000..d5c24d9 --- /dev/null +++ b/bench_test.go @@ -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) + } +}