aboutsummaryrefslogtreecommitdiff
path: root/client_timing_wait_test.go
blob: 918f44299b96a562cfd1980e52fa6e4f16175347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package fasthttp

import (
	"io"
	"net"
	"net/http"
	"strings"
	"testing"
	"time"

	"github.com/valyala/fasthttp/fasthttputil"
)

func newFasthttpSleepEchoHandler(sleep time.Duration) RequestHandler {
	return func(ctx *RequestCtx) {
		time.Sleep(sleep)
		ctx.Success("text/plain", ctx.RequestURI())
	}
}

func BenchmarkClientGetEndToEndWaitConn1Inmemory(b *testing.B) {
	benchmarkClientGetEndToEndWaitConnInmemory(b, 1)
}

func BenchmarkClientGetEndToEndWaitConn10Inmemory(b *testing.B) {
	benchmarkClientGetEndToEndWaitConnInmemory(b, 10)
}

func BenchmarkClientGetEndToEndWaitConn100Inmemory(b *testing.B) {
	benchmarkClientGetEndToEndWaitConnInmemory(b, 100)
}

func BenchmarkClientGetEndToEndWaitConn1000Inmemory(b *testing.B) {
	benchmarkClientGetEndToEndWaitConnInmemory(b, 1000)
}

func benchmarkClientGetEndToEndWaitConnInmemory(b *testing.B, parallelism int) {
	ln := fasthttputil.NewInmemoryListener()

	ch := make(chan struct{})
	sleepDuration := 50 * time.Millisecond
	go func() {
		if err := Serve(ln, newFasthttpSleepEchoHandler(sleepDuration)); err != nil {
			b.Errorf("error when serving requests: %v", err)
		}
		close(ch)
	}()

	c := &Client{
		MaxConnsPerHost:    1,
		Dial:               func(addr string) (net.Conn, error) { return ln.Dial() },
		MaxConnWaitTimeout: 5 * time.Second,
	}

	requestURI := "/foo/bar?baz=123&sleep=10ms"
	url := "http://unused.host" + requestURI
	b.SetParallelism(parallelism)
	b.RunParallel(func(pb *testing.PB) {
		var buf []byte
		for pb.Next() {
			statusCode, body, err := c.Get(buf, url)
			if err != nil {
				if err != ErrNoFreeConns {
					b.Fatalf("unexpected error: %v", err)
				}
			} else {
				if statusCode != StatusOK {
					b.Fatalf("unexpected status code: %d. Expecting %d", statusCode, StatusOK)
				}
				if string(body) != requestURI {
					b.Fatalf("unexpected response %q. Expecting %q", body, requestURI)
				}
			}
			buf = body
		}
	})

	ln.Close()
	select {
	case <-ch:
	case <-time.After(time.Second):
		b.Fatalf("server wasn't stopped")
	}
}

func newNethttpSleepEchoHandler(sleep time.Duration) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		time.Sleep(sleep)
		w.Header().Set(HeaderContentType, "text/plain")
		w.Write([]byte(r.RequestURI)) //nolint:errcheck
	}
}

func BenchmarkNetHTTPClientGetEndToEndWaitConn1Inmemory(b *testing.B) {
	benchmarkNetHTTPClientGetEndToEndWaitConnInmemory(b, 1)
}

func BenchmarkNetHTTPClientGetEndToEndWaitConn10Inmemory(b *testing.B) {
	benchmarkNetHTTPClientGetEndToEndWaitConnInmemory(b, 10)
}

func BenchmarkNetHTTPClientGetEndToEndWaitConn100Inmemory(b *testing.B) {
	benchmarkNetHTTPClientGetEndToEndWaitConnInmemory(b, 100)
}

func BenchmarkNetHTTPClientGetEndToEndWaitConn1000Inmemory(b *testing.B) {
	benchmarkNetHTTPClientGetEndToEndWaitConnInmemory(b, 1000)
}

func benchmarkNetHTTPClientGetEndToEndWaitConnInmemory(b *testing.B, parallelism int) {
	ln := fasthttputil.NewInmemoryListener()

	ch := make(chan struct{})
	sleep := 50 * time.Millisecond
	go func() {
		if err := http.Serve(ln, newNethttpSleepEchoHandler(sleep)); err != nil && !strings.Contains(
			err.Error(), "use of closed network connection") {
			b.Errorf("error when serving requests: %v", err)
		}
		close(ch)
	}()

	c := &http.Client{
		Transport: &http.Transport{
			Dial:            func(_, _ string) (net.Conn, error) { return ln.Dial() },
			MaxConnsPerHost: 1,
		},
		Timeout: 5 * time.Second,
	}

	requestURI := "/foo/bar?baz=123"
	url := "http://unused.host" + requestURI
	b.SetParallelism(parallelism)
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			resp, err := c.Get(url)
			if err != nil {
				if netErr, ok := err.(net.Error); !ok || !netErr.Timeout() {
					b.Fatalf("unexpected error: %v", err)
				}
			} else {
				if resp.StatusCode != http.StatusOK {
					b.Fatalf("unexpected status code: %d. Expecting %d", resp.StatusCode, http.StatusOK)
				}
				body, err := io.ReadAll(resp.Body)
				resp.Body.Close()
				if err != nil {
					b.Fatalf("unexpected error when reading response body: %v", err)
				}
				if string(body) != requestURI {
					b.Fatalf("unexpected response %q. Expecting %q", body, requestURI)
				}
			}
		}
	})

	ln.Close()
	select {
	case <-ch:
	case <-time.After(time.Second):
		b.Fatalf("server wasn't stopped")
	}
}