82 lines
1.8 KiB
Go
82 lines
1.8 KiB
Go
package benchmarks
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/explorer/backend/libs/go-rpc-gateway"
|
|
)
|
|
|
|
// BenchmarkInMemoryCache_Get benchmarks cache Get operations
|
|
func BenchmarkInMemoryCache_Get(b *testing.B) {
|
|
cache := gateway.NewInMemoryCache()
|
|
key := "bench-key"
|
|
value := []byte("bench-value")
|
|
cache.Set(key, value, 5*time.Minute)
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_, _ = cache.Get(key)
|
|
}
|
|
}
|
|
|
|
// BenchmarkInMemoryCache_Set benchmarks cache Set operations
|
|
func BenchmarkInMemoryCache_Set(b *testing.B) {
|
|
cache := gateway.NewInMemoryCache()
|
|
key := "bench-key"
|
|
value := []byte("bench-value")
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = cache.Set(key, value, 5*time.Minute)
|
|
}
|
|
}
|
|
|
|
// BenchmarkInMemoryRateLimiter_Allow benchmarks rate limiter Allow operations
|
|
func BenchmarkInMemoryRateLimiter_Allow(b *testing.B) {
|
|
config := gateway.RateLimitConfig{
|
|
RequestsPerSecond: 0,
|
|
RequestsPerMinute: 1000,
|
|
BurstSize: 100,
|
|
}
|
|
limiter := gateway.NewInMemoryRateLimiter(config)
|
|
key := "bench-key"
|
|
|
|
b.ResetTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = limiter.Allow(key)
|
|
}
|
|
}
|
|
|
|
// BenchmarkCache_Concurrent benchmarks concurrent cache operations
|
|
func BenchmarkCache_Concurrent(b *testing.B) {
|
|
cache := gateway.NewInMemoryCache()
|
|
key := "bench-key"
|
|
value := []byte("bench-value")
|
|
cache.Set(key, value, 5*time.Minute)
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
for pb.Next() {
|
|
_, _ = cache.Get(key)
|
|
}
|
|
})
|
|
}
|
|
|
|
// BenchmarkRateLimiter_Concurrent benchmarks concurrent rate limiter operations
|
|
func BenchmarkRateLimiter_Concurrent(b *testing.B) {
|
|
config := gateway.RateLimitConfig{
|
|
RequestsPerSecond: 0,
|
|
RequestsPerMinute: 10000,
|
|
BurstSize: 100,
|
|
}
|
|
limiter := gateway.NewInMemoryRateLimiter(config)
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
key := "bench-key"
|
|
for pb.Next() {
|
|
_ = limiter.Allow(key)
|
|
}
|
|
})
|
|
}
|
|
|