diff options
author | Erik Dubbelboer <erik@dubbelboer.com> | 2019-02-12 21:26:41 +0100 |
---|---|---|
committer | Erik Dubbelboer <erik@dubbelboer.com> | 2019-02-12 21:26:41 +0100 |
commit | 61039c93c2a31d3be661c50269d5800090f44175 (patch) | |
tree | e8a7d23f96b623a60eeed9c2ce179cd3ef76a6d7 /allocation_test.go | |
parent | Fix TechEmpower performance degradation (diff) | |
download | fasthttp-61039c93c2a31d3be661c50269d5800090f44175.tar.gz fasthttp-61039c93c2a31d3be661c50269d5800090f44175.tar.bz2 fasthttp-61039c93c2a31d3be661c50269d5800090f44175.zip |
Add zero allocation guarantee tests
Diffstat (limited to 'allocation_test.go')
-rw-r--r-- | allocation_test.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/allocation_test.go b/allocation_test.go new file mode 100644 index 0000000..e57c3af --- /dev/null +++ b/allocation_test.go @@ -0,0 +1,70 @@ +// +build !race + +package fasthttp + +import ( + "net" + + "testing" +) + +func TestAllocationServeConn(t *testing.T) { + s := &Server{ + Handler: func(ctx *RequestCtx) { + }, + } + + rw := &readWriter{} + // Make space for the request and response here so it + // doesn't allocate within the test. + rw.r.Grow(1024) + rw.w.Grow(1024) + + n := testing.AllocsPerRun(100, func() { + rw.r.WriteString("GET / HTTP/1.1\r\nHost: google.com\r\nCookie: foo=bar\r\n\r\n") + if err := s.ServeConn(rw); err != nil { + t.Fatal(err) + } + + // Reset the write buffer to make space for the next response. + rw.w.Reset() + }) + + if n != 0 { + t.Fatalf("expected 0 allocations, got %f", n) + } +} + +func TestAllocationClient(t *testing.T) { + ln, err := net.Listen("tcp4", "127.0.0.1:0") + if err != nil { + t.Fatalf("cannot listen: %s", err) + } + defer ln.Close() + + s := &Server{ + Handler: func(ctx *RequestCtx) { + }, + } + go s.Serve(ln) + + c := &Client{} + url := "http://" + ln.Addr().String() + + n := testing.AllocsPerRun(100, func() { + req := AcquireRequest() + res := AcquireResponse() + + req.SetRequestURI(url) + if err := c.Do(req, res); err != nil { + t.Fatal(err) + } + + ReleaseRequest(req) + ReleaseResponse(res) + }) + + if n != 0 { + t.Fatalf("expected 0 allocations, got %f", n) + } +} |