mirror of
				https://github.com/tobychui/zoraxy.git
				synced 2025-11-03 23:44:08 +01:00 
			
		
		
		
	improoved benchmark
This commit is contained in:
		@@ -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})
 | 
			
		||||
	})
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user