Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Fix benchmark test errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Baliedge committed Mar 11, 2024
1 parent bd38ee6 commit 65ee4fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test: ## Run unit tests and measure code coverage

.PHONY: bench
bench: ## Run Go benchmarks
go test ./... -bench . -benchtime 5s -timeout 0 -run=XXX -benchmem
go test ./... -bench . -benchtime 5s -timeout 0 -run='^$$' -benchmem

.PHONY: docker
docker: ## Build Docker image
Expand Down
35 changes: 16 additions & 19 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

guber "github.com/mailgun/gubernator/v2"
"github.com/mailgun/gubernator/v2/cluster"
"github.com/mailgun/holster/v4/clock"
"github.com/mailgun/holster/v4/syncutil"
"github.com/stretchr/testify/require"
)
Expand All @@ -31,6 +32,7 @@ func BenchmarkServer(b *testing.B) {
conf := guber.Config{}
err := conf.SetDefaults()
require.NoError(b, err, "Error in conf.SetDefaults")
requestTime := epochMillis(clock.Now())

b.Run("GetPeerRateLimit() with no batching", func(b *testing.B) {
client, err := guber.NewPeerClient(guber.PeerConfig{
Expand All @@ -40,17 +42,17 @@ func BenchmarkServer(b *testing.B) {
if err != nil {
b.Errorf("Error building client: %s", err)
}

b.ResetTimer()

for n := 0; n < b.N; n++ {
_, err := client.GetPeerRateLimit(context.Background(), &guber.RateLimitReq{
Name: "get_peer_rate_limits_benchmark",
UniqueKey: guber.RandomString(10),
Behavior: guber.Behavior_NO_BATCHING,
Limit: 10,
Duration: 5,
Hits: 1,
_, err := client.GetPeerRateLimit(ctx, &guber.RateLimitReq{
Name: b.Name(),
UniqueKey: guber.RandomString(10),
Behavior: guber.Behavior_NO_BATCHING,
Limit: 10,
Duration: 5,
Hits: 1,
RequestTime: &requestTime,
})
if err != nil {
b.Errorf("Error in client.GetPeerRateLimit: %s", err)
Expand All @@ -61,14 +63,13 @@ func BenchmarkServer(b *testing.B) {
b.Run("GetRateLimit()", func(b *testing.B) {
client, err := guber.DialV1Server(cluster.GetRandomPeer(cluster.DataCenterNone).GRPCAddress, nil)
require.NoError(b, err, "Error in guber.DialV1Server")

b.ResetTimer()

for n := 0; n < b.N; n++ {
_, err := client.GetRateLimits(ctx, &guber.GetRateLimitsReq{
Requests: []*guber.RateLimitReq{
{
Name: "get_rate_limit_benchmark",
Name: b.Name(),
UniqueKey: guber.RandomString(10),
Limit: 10,
Duration: guber.Second * 5,
Expand All @@ -85,14 +86,13 @@ func BenchmarkServer(b *testing.B) {
b.Run("GetRateLimitGlobal()", func(b *testing.B) {
client, err := guber.DialV1Server(cluster.GetRandomPeer(cluster.DataCenterNone).GRPCAddress, nil)
require.NoError(b, err, "Error in guber.DialV1Server")

b.ResetTimer()

for n := 0; n < b.N; n++ {
_, err := client.GetRateLimits(context.Background(), &guber.GetRateLimitsReq{
_, err := client.GetRateLimits(ctx, &guber.GetRateLimitsReq{
Requests: []*guber.RateLimitReq{
{
Name: "get_rate_limit_benchmark",
Name: b.Name(),
UniqueKey: guber.RandomString(10),
Behavior: guber.Behavior_GLOBAL,
Limit: 10,
Expand All @@ -110,11 +110,10 @@ func BenchmarkServer(b *testing.B) {
b.Run("HealthCheck", func(b *testing.B) {
client, err := guber.DialV1Server(cluster.GetRandomPeer(cluster.DataCenterNone).GRPCAddress, nil)
require.NoError(b, err, "Error in guber.DialV1Server")

b.ResetTimer()

for n := 0; n < b.N; n++ {
if _, err := client.HealthCheck(context.Background(), &guber.HealthCheckReq{}); err != nil {
if _, err := client.HealthCheck(ctx, &guber.HealthCheckReq{}); err != nil {
b.Errorf("Error in client.HealthCheck: %s", err)
}
}
Expand All @@ -123,17 +122,15 @@ func BenchmarkServer(b *testing.B) {
b.Run("Thundering herd", func(b *testing.B) {
client, err := guber.DialV1Server(cluster.GetRandomPeer(cluster.DataCenterNone).GRPCAddress, nil)
require.NoError(b, err, "Error in guber.DialV1Server")

b.ResetTimer()

fan := syncutil.NewFanOut(100)

for n := 0; n < b.N; n++ {
fan.Run(func(o interface{}) error {
_, err := client.GetRateLimits(context.Background(), &guber.GetRateLimitsReq{
_, err := client.GetRateLimits(ctx, &guber.GetRateLimitsReq{
Requests: []*guber.RateLimitReq{
{
Name: "get_rate_limit_benchmark",
Name: b.Name(),
UniqueKey: guber.RandomString(10),
Limit: 10,
Duration: guber.Second * 5,
Expand Down
33 changes: 14 additions & 19 deletions functional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,12 +975,12 @@ func TestMissingFields(t *testing.T) {

func TestGlobalRateLimits(t *testing.T) {
name := t.Name()
key := randomKey()
key := guber.RandomString(10)
owner, err := cluster.FindOwningDaemon(name, key)
require.NoError(t, err)
peers, err := cluster.ListNonOwningDaemons(name, key)
require.NoError(t, err)
var resetTime int64
var firstResetTime int64

sendHit := func(client guber.V1Client, status guber.Status, hits, remain int64) {
ctx, cancel := context.WithTimeout(context.Background(), clock.Second*10)
Expand All @@ -1006,10 +1006,10 @@ func TestGlobalRateLimits(t *testing.T) {
assert.Equal(t, int64(5), item.Limit)

// ResetTime should not change during test.
if resetTime == 0 {
resetTime = item.ResetTime
if firstResetTime == 0 {
firstResetTime = item.ResetTime
}
assert.Equal(t, resetTime, item.ResetTime)
assert.Equal(t, firstResetTime, item.ResetTime)

// ensure that we have a canonical host
assert.NotEmpty(t, item.Metadata["owner"])
Expand Down Expand Up @@ -1051,12 +1051,11 @@ func TestGlobalRateLimits(t *testing.T) {
func TestGlobalRateLimitsWithLoadBalancing(t *testing.T) {
ctx := context.Background()
name := t.Name()
key := randomKey()
key := guber.RandomString(10)

// Determine owner and non-owner peers.
owner, err := cluster.FindOwningDaemon(name, key)
require.NoError(t, err)
// ownerAddr := owner.ownerPeerInfo.GRPCAddress
peers, err := cluster.ListNonOwningDaemons(name, key)
require.NoError(t, err)
nonOwner := peers[0]
Expand Down Expand Up @@ -1110,7 +1109,7 @@ func TestGlobalRateLimitsWithLoadBalancing(t *testing.T) {

func TestGlobalRateLimitsPeerOverLimit(t *testing.T) {
name := t.Name()
key := randomKey()
key := guber.RandomString(10)
owner, err := cluster.FindOwningDaemon(name, key)
require.NoError(t, err)
peers, err := cluster.ListNonOwningDaemons(name, key)
Expand Down Expand Up @@ -1161,7 +1160,7 @@ func TestGlobalRateLimitsPeerOverLimit(t *testing.T) {

func TestGlobalRequestMoreThanAvailable(t *testing.T) {
name := t.Name()
key := randomKey()
key := guber.RandomString(10)
owner, err := cluster.FindOwningDaemon(name, key)
require.NoError(t, err)
peers, err := cluster.ListNonOwningDaemons(name, key)
Expand Down Expand Up @@ -1221,7 +1220,7 @@ func TestGlobalRequestMoreThanAvailable(t *testing.T) {

func TestGlobalNegativeHits(t *testing.T) {
name := t.Name()
key := randomKey()
key := guber.RandomString(10)
owner, err := cluster.FindOwningDaemon(name, key)
require.NoError(t, err)
peers, err := cluster.ListNonOwningDaemons(name, key)
Expand Down Expand Up @@ -1275,7 +1274,7 @@ func TestGlobalNegativeHits(t *testing.T) {

func TestGlobalResetRemaining(t *testing.T) {
name := t.Name()
key := randomKey()
key := guber.RandomString(10)
owner, err := cluster.FindOwningDaemon(name, key)
require.NoError(t, err)
peers, err := cluster.ListNonOwningDaemons(name, key)
Expand Down Expand Up @@ -1527,7 +1526,7 @@ func TestResetRemaining(t *testing.T) {

func TestHealthCheck(t *testing.T) {
name := t.Name()
key := randomKey()
key := guber.RandomString(10)
client, err := guber.DialV1Server(cluster.DaemonAt(0).GRPCListeners[0].Addr().String(), nil)
require.NoError(t, err)

Expand Down Expand Up @@ -1608,7 +1607,7 @@ func TestHealthCheck(t *testing.T) {
func TestLeakyBucketDivBug(t *testing.T) {
defer clock.Freeze(clock.Now()).Unfreeze()
name := t.Name()
key := randomKey()
key := guber.RandomString(10)
client, err := guber.DialV1Server(cluster.GetRandomPeer(cluster.DataCenterNone).GRPCAddress, nil)
require.NoError(t, err)

Expand Down Expand Up @@ -1660,7 +1659,7 @@ func TestMultiRegion(t *testing.T) {

func TestGRPCGateway(t *testing.T) {
name := t.Name()
key := randomKey()
key := guber.RandomString(10)
address := cluster.GetRandomPeer(cluster.DataCenterNone).HTTPAddress
resp, err := http.DefaultClient.Get("http://" + address + "/v1/HealthCheck")
require.NoError(t, err)
Expand Down Expand Up @@ -1731,7 +1730,7 @@ func TestGetPeerRateLimits(t *testing.T) {
for i := 0; i < n; i++ {
req.Requests[i] = &guber.RateLimitReq{
Name: name,
UniqueKey: randomKey(),
UniqueKey: guber.RandomString(10),
Hits: 0,
Limit: 1000 + int64(i),
Duration: 1000,
Expand Down Expand Up @@ -2387,10 +2386,6 @@ func sendHit(t *testing.T, d *guber.Daemon, req *guber.RateLimitReq, expectStatu
assert.Equal(t, req.Limit, item.Limit)
}

func randomKey() string {
return fmt.Sprintf("%016x", rand.Int())
}

func epochMillis(t time.Time) int64 {
return t.UnixNano() / 1_000_000
}

0 comments on commit 65ee4fa

Please sign in to comment.