-
Notifications
You must be signed in to change notification settings - Fork 720
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is an automated cherry-pick of #4396
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
- Loading branch information
1 parent
3a23716
commit e46a4e5
Showing
9 changed files
with
731 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2020 TiKV Project Authors. | ||
// | ||
// 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 movingaverage | ||
|
||
import "github.com/montanaflynn/stats" | ||
|
||
// MedianFilter works as a median filter with specified window size. | ||
// There are at most `size` data points for calculating. | ||
// References: https://en.wikipedia.org/wiki/Median_filter. | ||
type MedianFilter struct { | ||
records []float64 | ||
size uint64 | ||
count uint64 | ||
instantaneous float64 | ||
} | ||
|
||
// NewMedianFilter returns a MedianFilter. | ||
func NewMedianFilter(size int) *MedianFilter { | ||
return &MedianFilter{ | ||
records: make([]float64, size), | ||
size: uint64(size), | ||
} | ||
} | ||
|
||
// Add adds a data point. | ||
func (r *MedianFilter) Add(n float64) { | ||
r.instantaneous = n | ||
r.records[r.count%r.size] = n | ||
r.count++ | ||
} | ||
|
||
// Get returns the median of the data set. | ||
func (r *MedianFilter) Get() float64 { | ||
if r.count == 0 { | ||
return 0 | ||
} | ||
records := r.records | ||
if r.count < r.size { | ||
records = r.records[:r.count] | ||
} | ||
median, _ := stats.Median(records) | ||
return median | ||
} | ||
|
||
// Reset cleans the data set. | ||
func (r *MedianFilter) Reset() { | ||
r.instantaneous = 0 | ||
r.count = 0 | ||
} | ||
|
||
// Set = Reset + Add. | ||
func (r *MedianFilter) Set(n float64) { | ||
r.instantaneous = n | ||
r.records[0] = n | ||
r.count = 1 | ||
} | ||
|
||
// GetInstantaneous returns the value just added. | ||
func (r *MedianFilter) GetInstantaneous() float64 { | ||
return r.instantaneous | ||
} | ||
|
||
// Clone returns a copy of MedianFilter | ||
func (r *MedianFilter) Clone() *MedianFilter { | ||
records := make([]float64, len(r.records)) | ||
copy(records, r.records) | ||
return &MedianFilter{ | ||
records: records, | ||
size: r.size, | ||
count: r.count, | ||
instantaneous: r.instantaneous, | ||
} | ||
} |
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,79 @@ | ||
// Copyright 2020 TiKV Project Authors. | ||
// | ||
// 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 movingaverage | ||
|
||
import "time" | ||
|
||
// TimeMedian is AvgOverTime + MedianFilter | ||
// Size of MedianFilter should be larger than double size of AvgOverTime to denoisy. | ||
// Delay is aotSize * mfSize * reportInterval/4 | ||
// and the min filled period is aotSize * reportInterval, which is not related with mfSize | ||
type TimeMedian struct { | ||
aot *AvgOverTime | ||
mf *MedianFilter | ||
aotSize int | ||
mfSize int | ||
instantaneous float64 | ||
} | ||
|
||
// NewTimeMedian returns a TimeMedian with given size. | ||
func NewTimeMedian(aotSize, mfSize int, reportInterval time.Duration) *TimeMedian { | ||
return &TimeMedian{ | ||
aot: NewAvgOverTime(time.Duration(aotSize) * reportInterval), | ||
mf: NewMedianFilter(mfSize), | ||
aotSize: aotSize, | ||
mfSize: mfSize, | ||
} | ||
} | ||
|
||
// Get returns change rate in the median of the several intervals. | ||
func (t *TimeMedian) Get() float64 { | ||
return t.mf.Get() | ||
} | ||
|
||
// Add adds recent change to TimeMedian. | ||
func (t *TimeMedian) Add(delta float64, interval time.Duration) { | ||
t.instantaneous = delta / interval.Seconds() | ||
t.aot.Add(delta, interval) | ||
if t.aot.IsFull() { | ||
t.mf.Add(t.aot.Get()) | ||
} | ||
} | ||
|
||
// Set sets the given average. | ||
func (t *TimeMedian) Set(avg float64) { | ||
t.mf.Set(avg) | ||
} | ||
|
||
// GetFilledPeriod returns filled period. | ||
func (t *TimeMedian) GetFilledPeriod() int { // it is unrelated with mfSize | ||
return t.aotSize | ||
} | ||
|
||
// GetInstantaneous returns instantaneous speed | ||
func (t *TimeMedian) GetInstantaneous() float64 { | ||
return t.instantaneous | ||
} | ||
|
||
// Clone returns a copy of TimeMedian | ||
func (t *TimeMedian) Clone() *TimeMedian { | ||
return &TimeMedian{ | ||
aot: t.aot.Clone(), | ||
mf: t.mf.Clone(), | ||
aotSize: t.aotSize, | ||
mfSize: t.mfSize, | ||
instantaneous: t.instantaneous, | ||
} | ||
} |
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
Oops, something went wrong.