-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2032 from OffchainLabs/batch-size-moving-average
Use a moving average to determine messages per batch
- Loading branch information
Showing
4 changed files
with
118 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2023, Offchain Labs, Inc. | ||
// For license information, see https://github.com/nitro/blob/master/LICENSE | ||
|
||
package arbmath | ||
|
||
import "fmt" | ||
|
||
// A simple moving average of a generic number type. | ||
type MovingAverage[T Number] struct { | ||
buffer []T | ||
bufferPosition int | ||
sum T | ||
} | ||
|
||
func NewMovingAverage[T Number](period int) (*MovingAverage[T], error) { | ||
if period <= 0 { | ||
return nil, fmt.Errorf("MovingAverage period specified as %v but it must be positive", period) | ||
} | ||
return &MovingAverage[T]{ | ||
buffer: make([]T, 0, period), | ||
}, nil | ||
} | ||
|
||
func (a *MovingAverage[T]) Update(value T) { | ||
period := cap(a.buffer) | ||
if period == 0 { | ||
return | ||
} | ||
if len(a.buffer) < period { | ||
a.buffer = append(a.buffer, value) | ||
a.sum += value | ||
} else { | ||
a.sum += value | ||
a.sum -= a.buffer[a.bufferPosition] | ||
a.buffer[a.bufferPosition] = value | ||
a.bufferPosition = (a.bufferPosition + 1) % period | ||
} | ||
} | ||
|
||
// Average returns the current moving average, or zero if no values have been added. | ||
func (a *MovingAverage[T]) Average() T { | ||
if len(a.buffer) == 0 { | ||
return 0 | ||
} | ||
return a.sum / T(len(a.buffer)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2023, Offchain Labs, Inc. | ||
// For license information, see https://github.com/nitro/blob/master/LICENSE | ||
|
||
package arbmath | ||
|
||
import "testing" | ||
|
||
func TestMovingAverage(t *testing.T) { | ||
_, err := NewMovingAverage[int](0) | ||
if err == nil { | ||
t.Error("Expected error when creating moving average of period 0") | ||
} | ||
_, err = NewMovingAverage[int](-1) | ||
if err == nil { | ||
t.Error("Expected error when creating moving average of period -1") | ||
} | ||
|
||
ma, err := NewMovingAverage[int](5) | ||
if err != nil { | ||
t.Fatalf("Error creating moving average of period 5: %v", err) | ||
} | ||
if ma.Average() != 0 { | ||
t.Errorf("Average() = %v, want 0", ma.Average()) | ||
} | ||
ma.Update(2) | ||
if ma.Average() != 2 { | ||
t.Errorf("Average() = %v, want 2", ma.Average()) | ||
} | ||
ma.Update(4) | ||
if ma.Average() != 3 { | ||
t.Errorf("Average() = %v, want 3", ma.Average()) | ||
} | ||
|
||
for i := 0; i < 5; i++ { | ||
ma.Update(10) | ||
} | ||
if ma.Average() != 10 { | ||
t.Errorf("Average() = %v, want 10", ma.Average()) | ||
} | ||
|
||
for i := 0; i < 5; i++ { | ||
ma.Update(0) | ||
} | ||
if ma.Average() != 0 { | ||
t.Errorf("Average() = %v, want 0", ma.Average()) | ||
} | ||
} |