aboutsummaryrefslogtreecommitdiff
path: root/server_timing_test.go
diff options
context:
space:
mode:
authorGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-11-23 19:41:43 +0200
committerGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-11-23 19:41:43 +0200
commit146145240d9850b4f1a17069eada06ebc3bc1f1d (patch)
treeb72729e9d887565339c304b2ab6f5d6989b8553c /server_timing_test.go
parentAdded a tip for writing tests and benchmarks for hot paths (diff)
downloadfasthttp-146145240d9850b4f1a17069eada06ebc3bc1f1d.tar.gz
fasthttp-146145240d9850b4f1a17069eada06ebc3bc1f1d.tar.bz2
fasthttp-146145240d9850b4f1a17069eada06ebc3bc1f1d.zip
Added RequestCtx.Hijack() for connections' hijacking
Diffstat (limited to 'server_timing_test.go')
-rw-r--r--server_timing_test.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/server_timing_test.go b/server_timing_test.go
index 91c8481..0859855 100644
--- a/server_timing_test.go
+++ b/server_timing_test.go
@@ -111,6 +111,53 @@ func BenchmarkNetHTTPServerGet10KReqPerConn1KClients(b *testing.B) {
benchmarkNetHTTPServerGet(b, 1000, 10000)
}
+func BenchmarkServerHijack(b *testing.B) {
+ clientsCount := 1000
+ requestsPerConn := 10000
+ ch := make(chan struct{}, b.N)
+ responseBody := []byte("123")
+ s := &Server{
+ Handler: func(ctx *RequestCtx) {
+ ctx.Hijack(func(c io.ReadWriter) {
+ // emulate server loop :)
+ conn := &fakeNetConn{
+ c: c,
+ }
+ err := ServeConn(conn, func(ctx *RequestCtx) {
+ ctx.Success("foobar", responseBody)
+ registerServedRequest(b, ch)
+ })
+ if err != nil {
+ b.Fatalf("error when serving connection")
+ }
+ })
+ ctx.Success("foobar", responseBody)
+ registerServedRequest(b, ch)
+ },
+ Concurrency: 16 * clientsCount,
+ }
+ req := "GET /foo HTTP/1.1\r\nHost: google.com\r\n\r\n"
+ benchmarkServer(b, s, clientsCount, requestsPerConn, req)
+ verifyRequestsServed(b, ch)
+}
+
+type fakeNetConn struct {
+ net.Conn
+ c io.ReadWriter
+}
+
+func (c *fakeNetConn) Read(p []byte) (int, error) {
+ return c.c.Read(p)
+}
+
+func (c *fakeNetConn) Write(p []byte) (int, error) {
+ return c.c.Write(p)
+}
+
+func (c *fakeNetConn) Close() error {
+ return nil
+}
+
func BenchmarkServerMaxConnsPerIP(b *testing.B) {
clientsCount := 1000
requestsPerConn := 10