-
Notifications
You must be signed in to change notification settings - Fork 5
/
pool_test.go
252 lines (207 loc) · 6.55 KB
/
pool_test.go
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package pool
import (
"io/ioutil"
"log"
"net/http"
"net/url"
"testing"
)
// Set up a quick HTTP server for local testing
func init() {
http.HandleFunc("/",
func(respOut http.ResponseWriter, reqIn *http.Request) {
defer reqIn.Body.Close()
},
)
go func() {
log.Fatalf("%s\n", http.ListenAndServe(":8080", nil))
}()
}
// doPoolTest is the workhorse testing function
func doPoolTest(client Client) error {
testURL, err := url.Parse("http://127.0.0.1:8080/")
if err != nil {
return err
}
var doFunc func(req *http.Request) (*http.Response, error)
switch cp := client.(type) {
case *PClient:
doFunc = cp.DoPool
case *http.Client:
doFunc = cp.Do
}
resp, err := doFunc(&http.Request{URL: testURL})
if err != nil {
return err
}
_, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
return nil
}
// TestNewPClient tests creating a PClient
func TestNewPClient(t *testing.T) {
t.Logf("[INFO] Starting TestNewPClient")
standardLibClient := &http.Client{}
_ = NewPClient(standardLibClient, 25, 200) // Max 25 connections, 200 requests-per-second
_ = NewPClient(standardLibClient, 0, 0) // Why do this? Just use http.Client
_ = NewPClient(standardLibClient, -1, -1) // What
t.Logf("[INFO] Completed TestNewPClient")
}
// TestPClient_Do tests performing a drop-in http.Client with pooling
func TestPClient_Do(t *testing.T) {
t.Logf("[INFO] Starting TestPClient_Do")
pClient := NewPClient(&http.Client{}, 0, 0)
if err := doPoolTest(pClient); err != nil {
t.Error("pool: ", err)
}
t.Logf("[INFO] Completed TestPClient_Do")
}
// TestPClient_DoPool tests performing a request with the pooling logic
func TestPClient_DoPool(t *testing.T) {
t.Logf("[INFO] Starting TestPClient_DoPool")
pClient := NewPClient(&http.Client{}, 0, 0)
if err := doPoolTest(pClient); err != nil {
t.Error("pool: ", err)
}
pClient2 := NewPClient(&http.Client{}, 25, 200)
if err := doPoolTest(pClient2); err != nil {
t.Error("pool: ", err)
}
pClient3 := NewPClient(&http.Client{}, -1, -1)
if err := doPoolTest(pClient3); err != nil {
t.Error("pool: ", err)
}
t.Logf("[INFO] Completed TestPClient_DoPool")
}
// BenchmarkPClient_Do benchmarks the pooling logic
func BenchmarkPClient_Do(b *testing.B) {
b.Logf("[INFO] Starting BenchmarkPClient_Do")
pClient := NewPClient(&http.Client{}, 0, 0)
for n := 0; n < b.N; n++ {
if err := doPoolTest(pClient); err != nil {
b.Error("pool: ", err)
}
}
b.Logf("[INFO] Completed BenchmarkPClient_Do")
}
// BenchmarkPClient_DoPool benchmarks the pooling logic
func BenchmarkPClient_DoPool(b *testing.B) {
b.Logf("[INFO] Starting BenchmarkPClient_DoPool")
pClient := NewPClient(&http.Client{}, 0, 0)
for n := 0; n < b.N; n++ {
if err := doPoolTest(pClient); err != nil {
b.Error("pool: ", err)
}
}
b.Logf("[INFO] Completed BenchmarkPClient_DoPool")
}
// BenchmarkBaseline benchmarks a request with the standard library http.Client
func BenchmarkBaseline(b *testing.B) {
b.Logf("[INFO] Starting BenchmarkBaseline")
stdClient := &http.Client{}
for n := 0; n < b.N; n++ {
if err := doPoolTest(stdClient); err != nil {
b.Error("pool: ", err)
}
}
b.Logf("[INFO] Completed BenchmarkBaseline")
}
// BenchmarkPClient_DoPool_10_0 benchmarks the pooling logic
func BenchmarkPClient_DoPool_10_0(b *testing.B) {
b.Logf("[INFO] Starting BenchmarkPClient_DoPool_10_0")
pClient := NewPClient(&http.Client{}, 10, 0)
for n := 0; n < b.N; n++ {
if err := doPoolTest(pClient); err != nil {
b.Error("pool: ", err)
}
}
b.Logf("[INFO] Completed BenchmarkPClient_DoPool_10_0")
}
// BenchmarkPClient_DoPool_0_10 benchmarks the pooling logic
func BenchmarkPClient_DoPool_0_10(b *testing.B) {
b.Logf("[INFO] Starting BenchmarkPClient_DoPool_0_10")
pClient := NewPClient(&http.Client{}, 0, 10)
for n := 0; n < b.N; n++ {
if err := doPoolTest(pClient); err != nil {
b.Error("pool: ", err)
}
}
b.Logf("[INFO] Completed BenchmarkPClient_DoPool_0_10")
}
// BenchmarkPClient_DoPool_10_10 benchmarks the pooling logic
func BenchmarkPClient_DoPool_10_10(b *testing.B) {
b.Logf("[INFO] Starting BenchmarkPClient_DoPool_10_10")
pClient := NewPClient(&http.Client{}, 10, 10)
for n := 0; n < b.N; n++ {
if err := doPoolTest(pClient); err != nil {
b.Error("pool: ", err)
}
}
b.Logf("[INFO] Completed BenchmarkPClient_DoPool_10_10")
}
// BenchmarkPClient_DoPool_10_100 benchmarks the pooling logic
func BenchmarkPClient_DoPool_10_100(b *testing.B) {
b.Logf("[INFO] Starting BenchmarkPClient_DoPool_10_100")
pClient := NewPClient(&http.Client{}, 10, 100)
for n := 0; n < b.N; n++ {
if err := doPoolTest(pClient); err != nil {
b.Error("pool: ", err)
}
}
b.Logf("[INFO] Completed BenchmarkPClient_DoPool_10_100")
}
// BenchmarkPClient_DoPool_10_200 benchmarks the pooling logic
func BenchmarkPClient_DoPool_10_200(b *testing.B) {
b.Logf("[INFO] Starting BenchmarkPClient_DoPool_10_200")
pClient := NewPClient(&http.Client{}, 10, 200)
for n := 0; n < b.N; n++ {
if err := doPoolTest(pClient); err != nil {
b.Error("pool: ", err)
}
}
b.Logf("[INFO] Completed BenchmarkPClient_DoPool_10_200")
}
// BenchmarkPClient_DoPool_20_100 benchmarks the pooling logic
func BenchmarkPClient_DoPool_20_100(b *testing.B) {
b.Logf("[INFO] Starting BenchmarkPClient_DoPool_20_100")
pClient := NewPClient(&http.Client{}, 20, 100)
for n := 0; n < b.N; n++ {
if err := doPoolTest(pClient); err != nil {
b.Error("pool: ", err)
}
}
b.Logf("[INFO] Completed BenchmarkPClient_DoPool_20_100")
}
// BenchmarkPClient_DoPool_20_200 benchmarks the pooling logic
func BenchmarkPClient_DoPool_20_200(b *testing.B) {
b.Logf("[INFO] Starting BenchmarkPClient_DoPool_20_200")
pClient := NewPClient(&http.Client{}, 20, 200)
for n := 0; n < b.N; n++ {
if err := doPoolTest(pClient); err != nil {
b.Error("pool: ", err)
}
}
b.Logf("[INFO] Completed BenchmarkPClient_DoPool_20_200")
}
// BenchmarkPClient_DoPool_30_100 benchmarks the pooling logic
func BenchmarkPClient_DoPool_30_100(b *testing.B) {
b.Logf("[INFO] Starting BenchmarkPClient_DoPool_30_100")
pClient := NewPClient(&http.Client{}, 30, 100)
for n := 0; n < b.N; n++ {
if err := doPoolTest(pClient); err != nil {
b.Error("pool: ", err)
}
}
b.Logf("[INFO] Completed BenchmarkPClient_DoPool_30_100")
}
// BenchmarkPClient_DoPool_30_200 benchmarks the pooling logic
func BenchmarkPClient_DoPool_30_200(b *testing.B) {
b.Logf("[INFO] Starting BenchmarkPClient_DoPool_30_200")
pClient := NewPClient(&http.Client{}, 30, 200)
for n := 0; n < b.N; n++ {
if err := doPoolTest(pClient); err != nil {
b.Error("pool: ", err)
}
}
b.Logf("[INFO] Completed BenchmarkPClient_DoPool_30_200")
}