From 61e4d45430685fecd51f8e85c9cfefc0cab359f3 Mon Sep 17 00:00:00 2001 From: kirari04 Date: Tue, 11 Jun 2024 16:53:29 +0200 Subject: [PATCH] improoved benchmark --- .../dynamicproxy/ratelimit_benchmark_test.go | 52 ++++++++++++++++--- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/src/mod/dynamicproxy/ratelimit_benchmark_test.go b/src/mod/dynamicproxy/ratelimit_benchmark_test.go index ed5b846..f19a40b 100644 --- a/src/mod/dynamicproxy/ratelimit_benchmark_test.go +++ b/src/mod/dynamicproxy/ratelimit_benchmark_test.go @@ -7,9 +7,25 @@ import ( "testing" ) -func BenchmarkRateLimitSyncMapInt64(b *testing.B) { - ipTableSyncMapInt64 = IpTableSyncMapInt64{} // Reset the table +func BenchmarkRateLimitLockMapInt64(b *testing.B) { + go InitRateLimit() + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + handleRateLimit(w, r, &ProxyEndpoint{RateLimiting: 10}) + }) + request := httptest.NewRequest("GET", "/", nil) + request.RemoteAddr = "192.168.1.1:1234" + + b.ResetTimer() + + for i := 0; i < b.N; i++ { + recorder := httptest.NewRecorder() + handler.ServeHTTP(recorder, request) + } +} + +func BenchmarkRateLimitSyncMapInt64(b *testing.B) { + go InitRateLimitSyncMapInt64() handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handleRateLimitSyncMapInt64(w, r, &ProxyEndpoint{RateLimiting: 10}) }) @@ -26,8 +42,7 @@ func BenchmarkRateLimitSyncMapInt64(b *testing.B) { } func BenchmarkRateLimitSyncMapAtomicInt64(b *testing.B) { - ipTableSyncMapAtomicInt64 = IpTableSyncMapAtomicInt64{} // Reset the table - + go InitRateLimitSyncMapAtomicInt64() handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handleRateLimitSyncMapAtomicInt64(w, r, &ProxyEndpoint{RateLimiting: 10}) }) @@ -43,9 +58,31 @@ func BenchmarkRateLimitSyncMapAtomicInt64(b *testing.B) { } } -func BenchmarkRateLimitSyncMapInt64Concurrent(b *testing.B) { - ipTableSyncMapInt64 = IpTableSyncMapInt64{} // Reset the table +func BenchmarkRateLimitLockMapInt64Concurrent(b *testing.B) { + go InitRateLimit() + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + handleRateLimit(w, r, &ProxyEndpoint{RateLimiting: 10}) + }) + request := httptest.NewRequest("GET", "/", nil) + request.RemoteAddr = "192.168.1.1:1234" + + b.ResetTimer() + + var wg sync.WaitGroup + for i := 0; i < b.N; i++ { + wg.Add(1) + go func() { + defer wg.Done() + recorder := httptest.NewRecorder() + handler.ServeHTTP(recorder, request) + }() + } + wg.Wait() +} + +func BenchmarkRateLimitSyncMapInt64Concurrent(b *testing.B) { + go InitRateLimitSyncMapInt64() handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handleRateLimitSyncMapInt64(w, r, &ProxyEndpoint{RateLimiting: 10}) }) @@ -68,8 +105,7 @@ func BenchmarkRateLimitSyncMapInt64Concurrent(b *testing.B) { } func BenchmarkRateLimitSyncMapAtomicInt64Concurrent(b *testing.B) { - ipTableSyncMapAtomicInt64 = IpTableSyncMapAtomicInt64{} // Reset the table - + go InitRateLimitSyncMapAtomicInt64() handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handleRateLimitSyncMapAtomicInt64(w, r, &ProxyEndpoint{RateLimiting: 10}) })