Skip to content

Commit f992216

Browse files
feat(benchmarks): add protected route benchmarks
1 parent 2c46a27 commit f992216

1 file changed

Lines changed: 160 additions & 0 deletions

File tree

tests/benchmarks/benchmark_test.go

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package benchmarks
22

33
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
47
"io"
58
"net/http"
69
"testing"
@@ -80,3 +83,160 @@ func BenchmarkAuthMicrosoftInitiation(b *testing.B) {
8083
resp.Body.Close()
8184
}
8285
}
86+
87+
func BenchmarkGetMe(b *testing.B) {
88+
b.ResetTimer()
89+
b.ReportAllocs()
90+
for i := 0; i < b.N; i++ {
91+
req, _ := http.NewRequest("GET", benchServer.URL+"/v1/auth/me", nil)
92+
req.AddCookie(&http.Cookie{Name: "capy_auth", Value: benchJWTToken})
93+
resp, err := benchClient.Do(req)
94+
if err != nil {
95+
b.Fatalf("failed to make request: %v", err)
96+
}
97+
if resp.StatusCode != http.StatusOK {
98+
b.Fatalf("unexpected status code: %d", resp.StatusCode)
99+
}
100+
io.Copy(io.Discard, resp.Body)
101+
resp.Body.Close()
102+
}
103+
}
104+
105+
func BenchmarkGetUser(b *testing.B) {
106+
b.ResetTimer()
107+
b.ReportAllocs()
108+
for i := 0; i < b.N; i++ {
109+
req, _ := http.NewRequest("GET", benchServer.URL+"/v1/users/"+benchUserID, nil)
110+
req.AddCookie(&http.Cookie{Name: "capy_auth", Value: benchJWTToken})
111+
resp, err := benchClient.Do(req)
112+
if err != nil {
113+
b.Fatalf("failed to make request: %v", err)
114+
}
115+
if resp.StatusCode != http.StatusOK {
116+
b.Fatalf("unexpected status code: %d", resp.StatusCode)
117+
}
118+
io.Copy(io.Discard, resp.Body)
119+
resp.Body.Close()
120+
}
121+
}
122+
123+
func BenchmarkListOrganizations(b *testing.B) {
124+
b.ResetTimer()
125+
b.ReportAllocs()
126+
for i := 0; i < b.N; i++ {
127+
req, _ := http.NewRequest("GET", benchServer.URL+"/v1/organizations", nil)
128+
req.AddCookie(&http.Cookie{Name: "capy_auth", Value: benchJWTToken})
129+
resp, err := benchClient.Do(req)
130+
if err != nil {
131+
b.Fatalf("failed to make request: %v", err)
132+
}
133+
if resp.StatusCode != http.StatusOK {
134+
b.Fatalf("unexpected status code: %d", resp.StatusCode)
135+
}
136+
io.Copy(io.Discard, resp.Body)
137+
resp.Body.Close()
138+
}
139+
}
140+
141+
func BenchmarkCreateOrganization(b *testing.B) {
142+
b.ResetTimer()
143+
b.ReportAllocs()
144+
for i := 0; i < b.N; i++ {
145+
body := map[string]string{
146+
"name": fmt.Sprintf("Bench Org %d", i),
147+
"slug": fmt.Sprintf("bench-org-%d", i),
148+
}
149+
jsonBody, _ := json.Marshal(body)
150+
req, _ := http.NewRequest("POST", benchServer.URL+"/v1/organizations", bytes.NewBuffer(jsonBody))
151+
req.AddCookie(&http.Cookie{Name: "capy_auth", Value: benchJWTToken})
152+
req.Header.Set("Content-Type", "application/json")
153+
resp, err := benchClient.Do(req)
154+
if err != nil {
155+
b.Fatalf("failed to make request: %v", err)
156+
}
157+
if resp.StatusCode != http.StatusCreated {
158+
b.Fatalf("unexpected status code: %d", resp.StatusCode)
159+
}
160+
io.Copy(io.Discard, resp.Body)
161+
resp.Body.Close()
162+
}
163+
}
164+
165+
func BenchmarkGetOrganization(b *testing.B) {
166+
b.ResetTimer()
167+
b.ReportAllocs()
168+
for i := 0; i < b.N; i++ {
169+
req, _ := http.NewRequest("GET", benchServer.URL+"/v1/organizations/"+benchOrgID, nil)
170+
req.AddCookie(&http.Cookie{Name: "capy_auth", Value: benchJWTToken})
171+
resp, err := benchClient.Do(req)
172+
if err != nil {
173+
b.Fatalf("failed to make request: %v", err)
174+
}
175+
if resp.StatusCode != http.StatusOK {
176+
b.Fatalf("unexpected status code: %d", resp.StatusCode)
177+
}
178+
io.Copy(io.Discard, resp.Body)
179+
resp.Body.Close()
180+
}
181+
}
182+
183+
func BenchmarkListEvents(b *testing.B) {
184+
b.ResetTimer()
185+
b.ReportAllocs()
186+
for i := 0; i < b.N; i++ {
187+
req, _ := http.NewRequest("GET", benchServer.URL+"/v1/events", nil)
188+
req.AddCookie(&http.Cookie{Name: "capy_auth", Value: benchJWTToken})
189+
resp, err := benchClient.Do(req)
190+
if err != nil {
191+
b.Fatalf("failed to make request: %v", err)
192+
}
193+
if resp.StatusCode != http.StatusOK {
194+
b.Fatalf("unexpected status code: %d", resp.StatusCode)
195+
}
196+
io.Copy(io.Discard, resp.Body)
197+
resp.Body.Close()
198+
}
199+
}
200+
201+
func BenchmarkCreateEvent(b *testing.B) {
202+
b.ResetTimer()
203+
b.ReportAllocs()
204+
for i := 0; i < b.N; i++ {
205+
body := map[string]interface{}{
206+
"location": "Bench Location",
207+
"description": "Bench Description",
208+
"org_id": benchOrgID,
209+
}
210+
jsonBody, _ := json.Marshal(body)
211+
req, _ := http.NewRequest("POST", benchServer.URL+"/v1/events", bytes.NewBuffer(jsonBody))
212+
req.AddCookie(&http.Cookie{Name: "capy_auth", Value: benchJWTToken})
213+
req.Header.Set("Content-Type", "application/json")
214+
resp, err := benchClient.Do(req)
215+
if err != nil {
216+
b.Fatalf("failed to make request: %v", err)
217+
}
218+
if resp.StatusCode != http.StatusCreated {
219+
b.Fatalf("unexpected status code: %d", resp.StatusCode)
220+
}
221+
io.Copy(io.Discard, resp.Body)
222+
resp.Body.Close()
223+
}
224+
}
225+
226+
func BenchmarkGetEvent(b *testing.B) {
227+
b.ResetTimer()
228+
b.ReportAllocs()
229+
for i := 0; i < b.N; i++ {
230+
req, _ := http.NewRequest("GET", benchServer.URL+"/v1/events/"+benchEventID, nil)
231+
req.AddCookie(&http.Cookie{Name: "capy_auth", Value: benchJWTToken})
232+
resp, err := benchClient.Do(req)
233+
if err != nil {
234+
b.Fatalf("failed to make request: %v", err)
235+
}
236+
if resp.StatusCode != http.StatusOK {
237+
b.Fatalf("unexpected status code: %d", resp.StatusCode)
238+
}
239+
io.Copy(io.Discard, resp.Body)
240+
resp.Body.Close()
241+
}
242+
}

0 commit comments

Comments
 (0)