From f3598e4ab807062e702e9f5b44ab508fff423e8e Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Tue, 8 Oct 2024 14:41:43 +0100 Subject: [PATCH] Add simple benchmark to track the allocations and speed for future improvements. --- main_bench_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 main_bench_test.go diff --git a/main_bench_test.go b/main_bench_test.go new file mode 100644 index 0000000..fa7c40a --- /dev/null +++ b/main_bench_test.go @@ -0,0 +1,57 @@ +package traefikoidc + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +// BenchmarkOIDCMiddleware benchmarks the OIDC middleware's ability to handle concurrent requests. +func BenchmarkOIDCMiddleware(b *testing.B) { + // Setup test environment + + ts := &TestSuite{} + ts.Setup() + ts.token = "valid.jwt.token" + + // Define the handler with OIDC middleware + ts.tOidc.next = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + }) + + // Create test server + server := httptest.NewServer(ts.tOidc.next) + defer server.Close() + + // Prepare HTTP client + client := &http.Client{} + + // Reset timer to exclude setup time + b.ResetTimer() + + // Run benchmark + for i := 0; i < b.N; i++ { + // Create new request + req, err := http.NewRequest("GET", server.URL, nil) + if err != nil { + b.Fatal(err) + } + + // Set necessary headers or cookies + req.Header.Set("Authorization", "Bearer "+ts.token) + + // Send the request + resp, err := client.Do(req) + if err != nil { + b.Fatal(err) + } + + // Close response body + resp.Body.Close() + + // Check response status code + if resp.StatusCode != http.StatusOK { + b.Errorf("Unexpected status code: got %v, want %v", resp.StatusCode, http.StatusOK) + } + } +}