aboutsummaryrefslogtreecommitdiff
path: root/client_timing_test.go
diff options
context:
space:
mode:
authorGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-11-13 19:38:46 +0200
committerGravatar Aliaksandr Valialkin <valyala@gmail.com> 2015-11-13 19:38:46 +0200
commit69f3c67fce94d3f334cf78aaab602cbe36ba24a5 (patch)
tree30f81478dce41459e24f6d4827e944f25ebae378 /client_timing_test.go
parentAdded SetRequestURI* helpers to Request (diff)
downloadfasthttp-69f3c67fce94d3f334cf78aaab602cbe36ba24a5.tar.gz
fasthttp-69f3c67fce94d3f334cf78aaab602cbe36ba24a5.tar.bz2
fasthttp-69f3c67fce94d3f334cf78aaab602cbe36ba24a5.zip
Added benchmark for measuring the maximum client throughput
Diffstat (limited to 'client_timing_test.go')
-rw-r--r--client_timing_test.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/client_timing_test.go b/client_timing_test.go
index ec515b1..7cacb45 100644
--- a/client_timing_test.go
+++ b/client_timing_test.go
@@ -1,14 +1,94 @@
package fasthttp
import (
+ "bytes"
+ "fmt"
"io/ioutil"
"net"
"net/http"
"strings"
+ "sync"
"testing"
"time"
)
+type fakeClientConn struct {
+ net.Conn
+ s []byte
+ n int
+ v interface{}
+}
+
+func (c *fakeClientConn) Write(b []byte) (int, error) {
+ return len(b), nil
+}
+
+func (c *fakeClientConn) Read(b []byte) (int, error) {
+ n := 0
+ for len(b) > 0 {
+ if c.n == len(c.s) {
+ c.n = 0
+ return n, nil
+ }
+ n = copy(b, c.s[c.n:])
+ c.n += n
+ b = b[n:]
+ }
+ return n, nil
+}
+
+func (c *fakeClientConn) Close() error {
+ releaseFakeServerConn(c)
+ return nil
+}
+
+func releaseFakeServerConn(c *fakeClientConn) {
+ c.n = 0
+ fakeClientConnPool.Put(c.v)
+}
+
+func acquireFakeServerConn(s []byte) *fakeClientConn {
+ v := fakeClientConnPool.Get()
+ if v == nil {
+ c := &fakeClientConn{
+ s: s,
+ }
+ c.v = c
+ return c
+ }
+ return v.(*fakeClientConn)
+}
+
+var fakeClientConnPool sync.Pool
+
+func BenchmarkClientGetFastServer(b *testing.B) {
+ body := []byte("012345678912")
+ s := []byte(fmt.Sprintf("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %d\r\n\r\n%s", len(body), body))
+ requestURI := []byte("http://foobar.com/aaa/bbb")
+ c := &Client{
+ Dial: func(addr string) (net.Conn, error) {
+ return acquireFakeServerConn(s), nil
+ },
+ }
+
+ b.RunParallel(func(pb *testing.PB) {
+ var req Request
+ var resp Response
+ req.Header.RequestURI = requestURI
+ for pb.Next() {
+ if err := c.Do(&req, &resp); err != nil {
+ b.Fatalf("unexpected error: %s", err)
+ }
+ if resp.Header.StatusCode != StatusOK {
+ b.Fatalf("unexpected status code: %d", resp.Header.StatusCode)
+ }
+ if !bytes.Equal(resp.Body, body) {
+ b.Fatalf("unexpected response body: %q. Expected %q", resp.Body, body)
+ }
+ }
+ })
+}
+
func fasthttpEchoHandler(ctx *RequestCtx) {
ctx.Success("text/plain", ctx.Request.Header.RequestURI)
}