-
Notifications
You must be signed in to change notification settings - Fork 97
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
Add a test for qps.go #8391
base: master
Are you sure you want to change the base?
Add a test for qps.go #8391
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "qps", | ||
srcs = ["qps.go"], | ||
importpath = "github.com/buildbuddy-io/buildbuddy/server/util/qps", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
go_test( | ||
name = "qps_test", | ||
size = "small", | ||
srcs = ["qps_test.go"], | ||
deps = [ | ||
":qps", | ||
"@com_github_stretchr_testify//require", | ||
], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package qps_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/buildbuddy-io/buildbuddy/server/util/qps" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestQPS(t *testing.T) { | ||
ticker := make(chan time.Time, 1) | ||
counter := qps.NewCounterForTesting(5*time.Second, ticker) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably want to |
||
|
||
// Test cases where the ring buffer is not full. | ||
require.Equal(t, float64(0), counter.Get()) | ||
counter.Inc() | ||
require.Equal(t, float64(12), counter.Get()) | ||
for i := 0; i < 59; i++ { | ||
counter.Inc() | ||
} | ||
require.Equal(t, float64(720), counter.Get()) | ||
for i := 0; i < 29; i++ { | ||
ticker <- time.Now() | ||
} | ||
// The current bin is incremented asynchronously upon receiving a tick via | ||
// the ticker channel. Give it some time to run. | ||
time.Sleep(10 * time.Millisecond) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will likely be flaky. Could do one of these instead
|
||
require.Equal(t, float64(24), counter.Get()) | ||
|
||
// Test cases where the ring buffer is full. | ||
for i := 0; i < 100; i++ { | ||
ticker <- time.Now() | ||
} | ||
time.Sleep(10 * time.Millisecond) | ||
require.Equal(t, float64(0), counter.Get()) | ||
for i := 0; i < 61; i++ { | ||
ticker <- time.Now() | ||
time.Sleep(10 * time.Millisecond) | ||
counter.Inc() | ||
} | ||
require.Equal(t, float64(12), counter.Get()) | ||
for i := 0; i < 60; i++ { | ||
ticker <- time.Now() | ||
time.Sleep(10 * time.Millisecond) | ||
for j := 0; j < i; j++ { | ||
counter.Inc() | ||
} | ||
} | ||
// Sum(0, n) = n * (n-1) / 2 | ||
// So, sum(0, 60) = 60 * 59 / 2 = 1,770 | ||
// 1,770 Queries / 5s = 354 QPS. | ||
require.Equal(t, float64(354), counter.Get()) | ||
} | ||
|
||
func TestRaciness(t *testing.T) { | ||
ticker := make(chan time.Time, 1) | ||
counter := qps.NewCounterForTesting(5*time.Second, ticker) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
for i := 1; i < 1000; i++ { | ||
go func() { | ||
for j := i; j < 1000; j++ { | ||
counter.Inc() | ||
if i%2 == 0 { | ||
counter.Get() | ||
} | ||
} | ||
ticker <- time.Now() | ||
}() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: probably more conventional to have only a single constructor and have it accept an instance of
clockwork.Clock
rather than a ticker channel