-
Notifications
You must be signed in to change notification settings - Fork 0
/
steelix_bench_test.go
45 lines (36 loc) · 981 Bytes
/
steelix_bench_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
package steelix_test
import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/indrasaputra/steelix"
)
type doer interface {
Do(*http.Request) (*http.Response, error)
}
func BenchmarkHTTPClient(b *testing.B) {
benchmarkClient(b, http.DefaultClient)
}
func BenchmarkSteelixClient_Do_WithRetry(b *testing.B) {
rc := createRetryConfig(5)
client := steelix.NewClient(http.DefaultClient, rc, nil)
benchmarkClient(b, client)
}
func BenchmarkSteelixClient_Do_WithBreaker(b *testing.B) {
client := steelix.NewClient(http.DefaultClient, nil, createConsecutiveBreakerConfig())
benchmarkClient(b, client)
}
func benchmarkClient(b *testing.B, client doer) {
server := httptest.NewServer(http.HandlerFunc(createOkHandler()))
defer server.Close()
req, _ := http.NewRequest(http.MethodGet, server.URL, nil)
for i := 0; i < b.N; i++ {
resp, _ := client.Do(req)
if resp != nil {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}
}
}