Skip to content

Commit

Permalink
Update Redis in CI, lower #no of requests
Browse files Browse the repository at this point in the history
  • Loading branch information
VojtechVitek committed Aug 8, 2024
1 parent a2a09ff commit 34e2015
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

services:
redis:
image: redis:6
image: redis:7
ports:
- 6379:6379

Expand Down
15 changes: 10 additions & 5 deletions httprateredis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestRedisCounter(t *testing.T) {
limitCounter, err := httprateredis.NewRedisLimitCounter(&httprateredis.Config{
Host: "localhost",
Port: 6379,
MaxIdle: 100,
MaxActive: 200,
MaxIdle: 0,
MaxActive: 2,
DBIndex: 0,
ClientName: "httprateredis_test",
PrefixKey: fmt.Sprintf("httprate:test:%v", rand.Int31n(100000)), // Unique Redis key for each test
Expand Down Expand Up @@ -162,7 +162,10 @@ func BenchmarkLocalCounter(b *testing.B) {
DBIndex: 0,
ClientName: "httprateredis_test",
PrefixKey: fmt.Sprintf("httprate:test:%v", rand.Int31n(100000)), // Unique key for each test
MaxActive: 10,
MaxIdle: 0,
FallbackDisabled: true,
FallbackTimeout: 5 * time.Second,
})
if err != nil {
b.Fatalf("redis not available: %v", err)
Expand All @@ -174,6 +177,8 @@ func BenchmarkLocalCounter(b *testing.B) {
currentWindow := time.Now().UTC().Truncate(time.Minute)
previousWindow := currentWindow.Add(-time.Minute)

concurrentRequests := 100

b.ResetTimer()

for i := 0; i < b.N; i++ {
Expand All @@ -183,14 +188,14 @@ func BenchmarkLocalCounter(b *testing.B) {
previousWindow.Add(time.Duration(i) * time.Minute)

wg := sync.WaitGroup{}
wg.Add(1000)
for i := 0; i < 1000; i++ {
wg.Add(concurrentRequests)
for i := 0; i < concurrentRequests; i++ {
// Simulate concurrent requests with different rate-limit keys.
go func(i int) {
defer wg.Done()

_, _, _ = limitCounter.Get(fmt.Sprintf("key:%v", i), currentWindow, previousWindow)
_ = limitCounter.IncrementBy(fmt.Sprintf("key:%v", i), currentWindow, rand.Intn(100))
_ = limitCounter.IncrementBy(fmt.Sprintf("key:%v", i), currentWindow, rand.Intn(20))
}(i)
}
wg.Wait()
Expand Down

0 comments on commit 34e2015

Please sign in to comment.