Skip to content

Commit 995bc1c

Browse files
committed
chore: add benchmark test of client
1 parent 1cd56a5 commit 995bc1c

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed

client_bench_test.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package meilisearch
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"io"
8+
"net/http"
9+
"net/http/httptest"
10+
"testing"
11+
)
12+
13+
func Benchmark_ExecuteRequest(b *testing.B) {
14+
b.ReportAllocs()
15+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
16+
if r.Method == http.MethodGet && r.URL.Path == "/test" {
17+
w.WriteHeader(http.StatusOK)
18+
_, _ = w.Write([]byte(`{"message":"get successful"}`))
19+
} else {
20+
w.WriteHeader(http.StatusNotFound)
21+
}
22+
}))
23+
defer ts.Close()
24+
25+
c := newClient(&http.Client{}, ts.URL, "testApiKey", clientConfig{
26+
disableRetry: true,
27+
})
28+
29+
b.ResetTimer()
30+
for i := 0; i < b.N; i++ {
31+
err := c.executeRequest(context.Background(), &internalRequest{
32+
endpoint: "/test",
33+
method: http.MethodGet,
34+
withResponse: &mockResponse{},
35+
acceptedStatusCodes: []int{http.StatusOK},
36+
})
37+
if err != nil {
38+
b.Fatal(err)
39+
}
40+
}
41+
}
42+
43+
func Benchmark_ExecuteRequestWithEncoding(b *testing.B) {
44+
b.ReportAllocs()
45+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
46+
if r.Method == http.MethodPost && r.URL.Path == "/test" {
47+
accept := r.Header.Get("Accept-Encoding")
48+
ce := r.Header.Get("Content-Encoding")
49+
50+
reqEnc := newEncoding(ContentEncoding(ce), DefaultCompression)
51+
respEnc := newEncoding(ContentEncoding(accept), DefaultCompression)
52+
req := new(mockData)
53+
54+
if len(ce) != 0 {
55+
body, err := io.ReadAll(r.Body)
56+
if err != nil {
57+
b.Fatal(err)
58+
}
59+
60+
err = reqEnc.Decode(body, req)
61+
if err != nil {
62+
b.Fatal(err)
63+
}
64+
}
65+
66+
if len(accept) != 0 {
67+
d, err := json.Marshal(req)
68+
if err != nil {
69+
b.Fatal(err)
70+
}
71+
res, err := respEnc.Encode(bytes.NewReader(d))
72+
if err != nil {
73+
b.Fatal(err)
74+
}
75+
_, _ = w.Write(res.Bytes())
76+
w.WriteHeader(http.StatusOK)
77+
}
78+
} else {
79+
w.WriteHeader(http.StatusNotFound)
80+
}
81+
}))
82+
defer ts.Close()
83+
84+
c := newClient(&http.Client{}, ts.URL, "testApiKey", clientConfig{
85+
disableRetry: true,
86+
contentEncoding: GzipEncoding,
87+
encodingCompressionLevel: DefaultCompression,
88+
})
89+
90+
b.ResetTimer()
91+
for i := 0; i < b.N; i++ {
92+
err := c.executeRequest(context.Background(), &internalRequest{
93+
endpoint: "/test",
94+
method: http.MethodPost,
95+
contentType: contentTypeJSON,
96+
withRequest: &mockData{Name: "foo", Age: 30},
97+
withResponse: &mockData{},
98+
acceptedStatusCodes: []int{http.StatusOK},
99+
})
100+
if err != nil {
101+
b.Fatal(err)
102+
}
103+
}
104+
}
105+
106+
func Benchmark_ExecuteRequestWithoutRetries(b *testing.B) {
107+
b.ReportAllocs()
108+
retryCount := 0
109+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
110+
if r.Method == http.MethodGet && r.URL.Path == "/test" {
111+
if retryCount == 2 {
112+
w.WriteHeader(http.StatusOK)
113+
return
114+
}
115+
w.WriteHeader(http.StatusBadGateway)
116+
retryCount++
117+
} else {
118+
w.WriteHeader(http.StatusNotFound)
119+
}
120+
}))
121+
defer ts.Close()
122+
123+
c := newClient(&http.Client{}, ts.URL, "testApiKey", clientConfig{
124+
disableRetry: false,
125+
maxRetries: 3,
126+
retryOnStatus: map[int]bool{
127+
502: true,
128+
503: true,
129+
504: true,
130+
},
131+
})
132+
133+
b.ResetTimer()
134+
for i := 0; i < b.N; i++ {
135+
err := c.executeRequest(context.Background(), &internalRequest{
136+
endpoint: "/test",
137+
method: http.MethodGet,
138+
withResponse: nil,
139+
withRequest: nil,
140+
acceptedStatusCodes: []int{http.StatusOK},
141+
})
142+
if err != nil {
143+
b.Fatal(err)
144+
}
145+
}
146+
}

0 commit comments

Comments
 (0)