-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
ti-chi-bot
merged 18 commits into
pingcap:master
from
hawkingrei:add_exponential_average_measurement
Dec 2, 2022
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f3cf6af
mathutil: add exponential average
hawkingrei 90f91ed
mathutil: add exponential average
hawkingrei 4c8d103
update
hawkingrei ccf2e7c
update
hawkingrei 3912813
update
hawkingrei 2655711
update
hawkingrei 7c19379
update
hawkingrei e89b935
update
hawkingrei bd63a4c
update
hawkingrei 1f20f08
Update util/mathutil/exponential_average.go
hawkingrei 543758a
Update util/mathutil/exponential_average.go
hawkingrei c866ddf
update test
hawkingrei 126788a
update
hawkingrei 021988a
update
hawkingrei bfb2f0d
Update util/mathutil/exponential_average.go
hawkingrei dde4036
Update util/mathutil/exponential_average.go
hawkingrei 254bbb6
Merge branch 'master' into add_exponential_average_measurement
hawkingrei c4824dd
Merge branch 'master' into add_exponential_average_measurement
ti-chi-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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( | ||
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 | ||
} |
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,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())) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could we add a default param for this struct? It's hard for me to decide what
factor
andwarmupWindow
to use in sometime.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.
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.