optimize: shedding algorithm performance (#3908)

This commit is contained in:
Kevin Wan
2024-02-15 20:22:22 +08:00
committed by GitHub
parent 8ceb2885db
commit 9c17499757
8 changed files with 605 additions and 18 deletions

View File

@@ -19,6 +19,7 @@ import (
const (
buckets = 10
bucketDuration = time.Millisecond * 50
windowFactor = 0.01
)
func init() {
@@ -114,10 +115,10 @@ func TestAdaptiveShedderMaxFlight(t *testing.T) {
shedder := &adaptiveShedder{
passCounter: passCounter,
rtCounter: rtCounter,
windows: buckets,
windowScale: windowFactor,
droppedRecently: syncx.NewAtomicBool(),
}
assert.Equal(t, int64(54), shedder.maxFlight())
assert.Equal(t, float64(54), shedder.maxFlight())
}
func TestAdaptiveShedderShouldDrop(t *testing.T) {
@@ -136,7 +137,7 @@ func TestAdaptiveShedderShouldDrop(t *testing.T) {
shedder := &adaptiveShedder{
passCounter: passCounter,
rtCounter: rtCounter,
windows: buckets,
windowScale: windowFactor,
overloadTime: syncx.NewAtomicDuration(),
droppedRecently: syncx.NewAtomicBool(),
}
@@ -149,7 +150,8 @@ func TestAdaptiveShedderShouldDrop(t *testing.T) {
// cpu >= 800, inflight > maxPass
shedder.avgFlying = 80
shedder.flying = 50
// because of the overloadFactor, so we need to make sure maxFlight is greater than flying
shedder.flying = int64(shedder.maxFlight()*shedder.overloadFactor()) - 5
assert.False(t, shedder.shouldDrop())
// cpu >= 800, inflight > maxPass
@@ -190,7 +192,7 @@ func TestAdaptiveShedderStillHot(t *testing.T) {
shedder := &adaptiveShedder{
passCounter: passCounter,
rtCounter: rtCounter,
windows: buckets,
windowScale: windowFactor,
overloadTime: syncx.NewAtomicDuration(),
droppedRecently: syncx.ForAtomicBool(true),
}
@@ -239,6 +241,30 @@ func BenchmarkAdaptiveShedder_Allow(b *testing.B) {
b.Run("low load", bench)
}
func BenchmarkMaxFlight(b *testing.B) {
passCounter := newRollingWindow()
rtCounter := newRollingWindow()
for i := 0; i < 10; i++ {
if i > 0 {
time.Sleep(bucketDuration)
}
passCounter.Add(float64((i + 1) * 100))
for j := i*10 + 1; j <= i*10+10; j++ {
rtCounter.Add(float64(j))
}
}
shedder := &adaptiveShedder{
passCounter: passCounter,
rtCounter: rtCounter,
windowScale: windowFactor,
droppedRecently: syncx.NewAtomicBool(),
}
for i := 0; i < b.N; i++ {
_ = shedder.maxFlight()
}
}
func newRollingWindow() *collection.RollingWindow {
return collection.NewRollingWindow(buckets, bucketDuration, collection.IgnoreCurrentBucket())
}