Skip to content

Implement benchmark for Redis backend #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Bechmark

on:
pull_request:

jobs:
benchmark:
name: Benchmark
runs-on: ubuntu-latest

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

steps:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ^1.17

- name: Git clone (master)
uses: actions/checkout@v4
with:
ref: master

- name: Run benchmark (master)
run: go test -bench=. -count=10 -benchmem | tee /tmp/master.txt

- name: Git clone (PR)
uses: actions/checkout@v4

- name: Run benchmark (PR)
run: go test -bench=. -count=10 -benchmem | tee /tmp/pr.txt

- name: Install benchstat
run: go install golang.org/x/perf/cmd/benchstat@latest

- name: Run benchstat
run: cd /tmp && benchstat master.txt pr.txt | tee /tmp/result.txt

- name: Comment on PR with benchmark results
uses: actions/github-script@v6
with:
script: |
const fs = require('fs');
const results = fs.readFileSync('/tmp/result.txt', 'utf8');
const issue_number = context.payload.pull_request.number;
const { owner, repo } = context.repo;

await github.rest.issues.createComment({
owner,
repo,
issue_number,
body: `### Benchmark Results\n\n\`\`\`\n${results}\n\`\`\``
});
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/httprate"
httprateredis "github.com/go-chi/httprate-redis"
httprateredis "github.com/go-chi/httprate-redis"
)

func main() {
Expand Down
44 changes: 44 additions & 0 deletions httprateredis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package httprateredis_test
import (
"fmt"
"math/rand"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -150,3 +151,46 @@ func TestRedisCounter(t *testing.T) {
}
}
}

func BenchmarkLocalCounter(b *testing.B) {
limitCounter, err := httprateredis.NewRedisLimitCounter(&httprateredis.Config{
Host: "localhost",
Port: 6379,
MaxIdle: 500,
MaxActive: 500,
DBIndex: 0,
ClientName: "httprateredis_test",
PrefixKey: fmt.Sprintf("httprate:test:%v", rand.Int31n(100000)), // Unique key for each test
})
if err != nil {
b.Fatalf("redis not available: %v", err)
}

limitCounter.Config(1000, time.Minute)

currentWindow := time.Now().UTC().Truncate(time.Minute)
previousWindow := currentWindow.Add(-time.Minute)

b.ResetTimer()

for i := 0; i < b.N; i++ {
for i := range []int{0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 0, 1, 0} {
// Simulate time.
currentWindow.Add(time.Duration(i) * time.Minute)
previousWindow.Add(time.Duration(i) * time.Minute)

wg := sync.WaitGroup{}
wg.Add(1000)
for i := 0; i < 1000; 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))
}(i)
}
wg.Wait()
}
}
}
Loading