Skip to content
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

mathutil: support exponential average #39484

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions util/mathutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "mathutil",
srcs = [
"exponential_average.go",
"math.go",
"rand.go",
],
Expand All @@ -15,6 +16,7 @@ go_test(
name = "mathutil_test",
timeout = "short",
srcs = [
"exponential_average_test.go",
"main_test.go",
"math_test.go",
"rand_test.go",
Expand Down
54 changes: 54 additions & 0 deletions util/mathutil/exponential_average.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mathutil

// ExponentialMovingAverage is an exponential moving average measurement implementation. It is not thread-safe.
type ExponentialMovingAverage struct {
value float64
sum float64
factor float64
warmupWindow int
count int
}

// NewExponentialMovingAverage will create a new ExponentialMovingAverage.
func NewExponentialMovingAverage(
Copy link
Contributor

@Defined2014 Defined2014 Dec 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a default param for this struct? It's hard for me to decide what factor and warmupWindow to use in sometime.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is hard. Different businesses have different characteristics. Someone has high sampling rate. so they want to have a less warnup windows. All in all, developer must accord their business characteristics to decide.

factor float64,
warmupWindow int,
) *ExponentialMovingAverage {
if factor >= 1 || factor <= 0 {
panic("factor must be (0, 1)")
hawkingrei marked this conversation as resolved.
Show resolved Hide resolved
}
return &ExponentialMovingAverage{
factor: factor,
warmupWindow: warmupWindow,
}
}

// Add a single sample and update the internal state.
func (m *ExponentialMovingAverage) Add(value float64) {
if m.count < m.warmupWindow {
m.count++
m.sum += value
m.value = m.sum / float64(m.count)
hawkingrei marked this conversation as resolved.
Show resolved Hide resolved
} else {
m.value = m.value*(1-m.factor) + value*m.factor
}
}

// Get the current value.
func (m *ExponentialMovingAverage) Get() float64 {
return m.value
}
39 changes: 39 additions & 0 deletions util/mathutil/exponential_average_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package mathutil

import (
"testing"

"github.com/stretchr/testify/require"
)

var samples = [100]float64{
1576, 1524, 6746, 6426, 9476, 1721, 8528, 7827, 8613, 6969, 4200, 4686, 2408, 3956, 7105, 1341,
9938, 9789, 6199, 4868, 4280, 7738, 7219, 3388, 2431, 1193, 1954, 2147, 7726, 3545, 8043, 2379,
4859, 4247, 2873, 6419, 3114, 3132, 6534, 8515, 1632, 9710, 6699, 1552, 2412, 4679, 4499, 9577,
7528, 8931, 7904, 5104, 8533, 7633, 4933, 1078, 3209, 1168, 1421, 4495, 2333, 1439, 8584, 7814,
4320, 9569, 1370, 6635, 7870, 2828, 1599, 3592, 1934, 5944, 9418, 4143, 2285, 6756, 2674, 7293,
4206, 5279, 9744, 2610, 2760, 9176, 1731, 3877, 2084, 2016, 3505, 5951, 4797, 5948, 8287, 8641,
9349, 2690, 3820, 3895,
}

func TestExponential(t *testing.T) {
win := NewExponentialMovingAverage(0.8, 2)
for _, s := range samples {
win.Add(s)
}
require.Equal(t, int64(3886), int64(win.Get()))
}