-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathclient_bench_test.go
More file actions
190 lines (165 loc) · 4.78 KB
/
Copy pathclient_bench_test.go
File metadata and controls
190 lines (165 loc) · 4.78 KB
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
package meilisearch
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)
type benchDoc struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Categories []string `json:"categories"`
Price float64 `json:"price"`
}
func generatePayload(size int) []benchDoc {
docs := make([]benchDoc, size)
for i := 0; i < size; i++ {
docs[i] = benchDoc{
ID: fmt.Sprintf("doc_%d", i),
Title: "High Performance Go Architecture",
Description: "An in-depth look into SDK design, benchmarking, and real-world system integrations.",
Categories: []string{"programming", "go", "architecture"},
Price: 49.99,
}
}
return docs
}
func setupBenchServer() *httptest.Server {
retryCount := 0
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/get-fast":
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"status":"ok"}`))
case "/post-fast":
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(`{"taskUid": 1}`))
case "/ndjson-large":
w.Header().Set("Content-Type", "application/x-ndjson")
w.WriteHeader(http.StatusOK)
for i := 0; i < 500; i++ {
_, _ = w.Write([]byte(`{"id":"1","title":"test"}` + "\n"))
}
case "/retry-sim":
if retryCount%3 != 0 {
retryCount++
w.WriteHeader(http.StatusBadGateway)
return
}
retryCount++
w.WriteHeader(http.StatusOK)
default:
w.WriteHeader(http.StatusNotFound)
}
}))
}
func BenchmarkClient_ExecuteRequest(b *testing.B) {
ts := setupBenchServer()
defer ts.Close()
payloadSmall := generatePayload(10)
payloadLarge := generatePayload(1000)
baseCfg := &clientConfig{
disableRetry: false,
maxRetries: 3,
retryOnStatus: map[int]bool{502: true, 503: true, 504: true},
jsonMarshal: json.Marshal,
jsonUnmarshal: json.Unmarshal,
}
b.Run("GET_Simple", func(b *testing.B) {
c := newClient(&http.Client{}, ts.URL, "key", baseCfg)
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = c.executeRequest(ctx, &internalRequest{
endpoint: "/get-fast",
method: http.MethodGet,
acceptedStatusCodes: []int{http.StatusOK},
})
}
})
b.Run("POST_SmallPayload_Uncompressed", func(b *testing.B) {
c := newClient(&http.Client{}, ts.URL, "key", baseCfg)
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = c.executeRequest(ctx, &internalRequest{
endpoint: "/post-fast",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: payloadSmall,
acceptedStatusCodes: []int{http.StatusCreated},
})
}
})
b.Run("POST_LargePayload_Uncompressed", func(b *testing.B) {
c := newClient(&http.Client{}, ts.URL, "key", baseCfg)
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = c.executeRequest(ctx, &internalRequest{
endpoint: "/post-fast",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: payloadLarge,
acceptedStatusCodes: []int{http.StatusCreated},
})
}
})
b.Run("POST_LargePayload_Gzip", func(b *testing.B) {
cfgGzip := *baseCfg
cfgGzip.contentEncoding = GzipEncoding
cfgGzip.encodingCompressionLevel = DefaultCompression
c := newClient(&http.Client{}, ts.URL, "key", &cfgGzip)
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = c.executeRequest(ctx, &internalRequest{
endpoint: "/post-fast",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: payloadLarge,
acceptedStatusCodes: []int{http.StatusCreated},
})
}
})
b.Run("GET_NDJSON_Decoding", func(b *testing.B) {
c := newClient(&http.Client{}, ts.URL, "key", baseCfg)
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var resp []benchDoc
_ = c.executeRequest(ctx, &internalRequest{
endpoint: "/ndjson-large",
method: http.MethodGet,
acceptedContentType: contentTypeNDJSON,
withResponse: &resp,
acceptedStatusCodes: []int{http.StatusOK},
})
}
})
b.Run("GET_WithRetries", func(b *testing.B) {
c := newClient(&http.Client{}, ts.URL, "key", baseCfg)
c.retryBackoff = func(attempt uint8) time.Duration {
return 0
}
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = c.executeRequest(ctx, &internalRequest{
endpoint: "/retry-sim",
method: http.MethodGet,
acceptedStatusCodes: []int{http.StatusOK},
})
}
})
}