Skip to content

Commit

Permalink
add benchmarks (mennanov#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeym authored Jun 2, 2024
1 parent ed360c3 commit d00872d
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
all: test
all: test benchmark

test:
docker-compose-up:
docker-compose up -d
ETCD_ENDPOINTS="127.0.0.1:2379" REDIS_ADDR="127.0.0.1:6379" REDIS_NODES="127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004,127.0.0.1:7005" ZOOKEEPER_ENDPOINTS="127.0.0.1" CONSUL_ADDR="127.0.0.1:8500" AWS_ADDR="127.0.0.1:8000" MEMCACHED_ADDR="127.0.0.1:11211" POSTGRES_URL="postgres://postgres@localhost:5432/?sslmode=disable" go test -race -v -failfast -bench=.

test: docker-compose-up
ETCD_ENDPOINTS="127.0.0.1:2379" REDIS_ADDR="127.0.0.1:6379" REDIS_NODES="127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004,127.0.0.1:7005" ZOOKEEPER_ENDPOINTS="127.0.0.1" CONSUL_ADDR="127.0.0.1:8500" AWS_ADDR="127.0.0.1:8000" MEMCACHED_ADDR="127.0.0.1:11211" POSTGRES_URL="postgres://postgres@localhost:5432/?sslmode=disable" go test -race -v -failfast

benchmark: docker-compose-up
ETCD_ENDPOINTS="127.0.0.1:2379" REDIS_ADDR="127.0.0.1:6379" REDIS_NODES="127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003,127.0.0.1:7004,127.0.0.1:7005" ZOOKEEPER_ENDPOINTS="127.0.0.1" CONSUL_ADDR="127.0.0.1:8500" AWS_ADDR="127.0.0.1:8000" MEMCACHED_ADDR="127.0.0.1:11211" POSTGRES_URL="postgres://postgres@localhost:5432/?sslmode=disable" go test -race -run=nonexistent -bench=.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,13 @@ If memcached exists already and it is okay to handle burst traffic caused by une

Run tests locally:
```bash
make test
```
Run benchmarks locally:
```bash
make benchmark
```
Run both locally:
```bash
make
```
19 changes: 19 additions & 0 deletions concurrent_buffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"sync"
"testing"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -110,3 +111,21 @@ func (s *LimitersTestSuite) TestConcurrentBufferDuplicateKeys() {
})
}
}

func BenchmarkConcurrentBuffers(b *testing.B) {
s := new(LimitersTestSuite)
s.SetT(&testing.T{})
s.SetupSuite()
capacity := int64(1)
ttl := time.Second
clock := newFakeClock()
buffers := s.concurrentBuffers(capacity, ttl, clock)
for name, buffer := range buffers {
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
s.Require().NoError(buffer.Limit(context.TODO(), "key1"))
}
})
}
s.TearDownSuite()
}
20 changes: 20 additions & 0 deletions fixedwindow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package limiters_test

import (
"context"
"testing"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -102,3 +103,22 @@ func (s *LimitersTestSuite) TestFixedWindowOverflow() {
})
}
}

func BenchmarkFixedWindows(b *testing.B) {
s := new(LimitersTestSuite)
s.SetT(&testing.T{})
s.SetupSuite()
capacity := int64(1)
rate := time.Second
clock := newFakeClock()
windows := s.fixedWindows(capacity, rate, clock)
for name, window := range windows {
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := window.Limit(context.TODO())
s.Require().NoError(err)
}
})
}
s.TearDownSuite()
}
20 changes: 20 additions & 0 deletions leakybucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package limiters_test
import (
"context"
"sync"
"testing"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -140,3 +141,22 @@ func (s *LimitersTestSuite) TestLeakyBucketOverflow() {
})
}
}

func BenchmarkLeakyBuckets(b *testing.B) {
s := new(LimitersTestSuite)
s.SetT(&testing.T{})
s.SetupSuite()
capacity := int64(1)
rate := time.Second
clock := newFakeClock()
buckets := s.leakyBuckets(capacity, rate, clock)
for name, bucket := range buckets {
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := bucket.Limit(context.TODO())
s.Require().NoError(err)
}
})
}
s.TearDownSuite()
}
21 changes: 21 additions & 0 deletions slidingwindow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package limiters_test

import (
"context"
"testing"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -306,3 +307,23 @@ func (s *LimitersTestSuite) TestSlidingWindowOverflowAndWait() {
}
}
}

func BenchmarkSlidingWindows(b *testing.B) {
s := new(LimitersTestSuite)
s.SetT(&testing.T{})
s.SetupSuite()
capacity := int64(1)
rate := time.Second
clock := newFakeClock()
epsilon := 1e-9
windows := s.slidingWindows(capacity, rate, clock, epsilon)
for name, window := range windows {
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := window.Limit(context.TODO())
s.Require().NoError(err)
}
})
}
s.TearDownSuite()
}
20 changes: 20 additions & 0 deletions tokenbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package limiters_test
import (
"context"
"sync"
"testing"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -203,3 +204,22 @@ func (s *LimitersTestSuite) TestTokenBucketRefill() {
})
}
}

func BenchmarkTokenBuckets(b *testing.B) {
s := new(LimitersTestSuite)
s.SetT(&testing.T{})
s.SetupSuite()
capacity := int64(1)
rate := time.Second
clock := newFakeClock()
buckets := s.tokenBuckets(capacity, rate, clock)
for name, bucket := range buckets {
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := bucket.Limit(context.TODO())
s.Require().NoError(err)
}
})
}
s.TearDownSuite()
}

0 comments on commit d00872d

Please sign in to comment.