Commit the receive counters once per batch instead of once per frame

This commit is contained in:
flamingcow
2026-08-04 21:25:04 -07:00
parent 21788fdd4f
commit 49de171ad0
2 changed files with 59 additions and 17 deletions
+20 -10
View File
@@ -9,9 +9,11 @@ import (
// got round to draining it.
func TestRxObserveBucketsByStamp(t *testing.T) {
var s rxStats
s.observe(3*int64(time.Millisecond), 100)
s.observe(5*int64(time.Millisecond), 200)
s.observe(rateBucketNs+int64(time.Millisecond), 300)
run := rateRun{stats: &s}
run.add(3*int64(time.Millisecond), 100)
run.add(5*int64(time.Millisecond), 200)
run.add(rateBucketNs+int64(time.Millisecond), 300)
run.flush()
if f, b := s.bucket(0); f != 2 || b != 300 {
t.Errorf("epoch 0 = %d frames, %d bytes; want 2, 300", f, b)
@@ -28,8 +30,10 @@ func TestRxObserveBucketsByStamp(t *testing.T) {
// reports nothing rather than the stale counts.
func TestRxBucketWraps(t *testing.T) {
var s rxStats
s.observe(1, 100)
s.observe(rateBuckets*rateBucketNs+1, 200)
run := rateRun{stats: &s}
run.add(1, 100)
run.add(rateBuckets*rateBucketNs+1, 200)
run.flush()
if f, b := s.bucket(0); f != 0 || b != 0 {
t.Errorf("evicted epoch = %d frames, %d bytes; want 0, 0", f, b)
@@ -42,9 +46,13 @@ func TestRxBucketWraps(t *testing.T) {
// The newest epoch may still be filling, so the rate comes from the one before.
func TestDirectionRateReadsOneBucketBack(t *testing.T) {
d := &direction{rxStats: []*rxStats{{}, {}}}
d.rxStats[0].observe(rateBucketNs+1, 500)
d.rxStats[1].observe(rateBucketNs+2, 700)
d.rxStats[0].observe(2*rateBucketNs+1, 900)
r0 := rateRun{stats: d.rxStats[0]}
r1 := rateRun{stats: d.rxStats[1]}
r0.add(rateBucketNs+1, 500)
r1.add(rateBucketNs+2, 700)
r0.add(2*rateBucketNs+1, 900)
r0.flush()
r1.flush()
d.readRateBucket(time.Now())
if d.rateFrames != 2 || d.rateBytes != 1200 {
@@ -57,8 +65,10 @@ func TestDirectionRateReadsOneBucketBack(t *testing.T) {
// a quiet wire and must not keep reporting the last bucket.
func TestDirectionRateGoesStale(t *testing.T) {
d := &direction{rxStats: []*rxStats{{}}}
d.rxStats[0].observe(rateBucketNs+1, 500)
d.rxStats[0].observe(2*rateBucketNs+1, 900)
run := rateRun{stats: d.rxStats[0]}
run.add(rateBucketNs+1, 500)
run.add(2*rateBucketNs+1, 900)
run.flush()
now := time.Now()
d.readRateBucket(now)