client: fix status bytes per second measurement

still far from perfect, but better than incorrect values
This commit is contained in:
Christian Schwarz 2018-11-05 01:37:51 +01:00
parent 80babe3ab4
commit dd286aa12e

View File

@ -22,39 +22,50 @@ import (
"time" "time"
) )
type byteProgressMeasurement struct {
time time.Time
val int64
}
type bytesProgressHistory struct { type bytesProgressHistory struct {
changeCounter int last *byteProgressMeasurement // pointer as poor man's optional
lastChangeAt time.Time changeCount int
last int64 lastChange time.Time
bpsIncreaseExpAvg float64 bpsAvg float64
} }
func (p *bytesProgressHistory) Update(currentVal int64) (bytesPerSecondAvg int64, changeCount int) { func (p *bytesProgressHistory) Update(currentVal int64) (bytesPerSecondAvg int64, changeCount int) {
if currentVal < p.last {
*p = bytesProgressHistory{ if p.last == nil {
last: currentVal, p.last = &byteProgressMeasurement{
lastChangeAt: time.Now(), time: time.Now(),
val: currentVal,
} }
} return 0, 0
defer func() {
p.last = currentVal
}()
if time.Now().Sub(p.lastChangeAt) > 3 *time.Second { // FIXME depends on refresh frequency
p.changeCounter = 0
p.bpsIncreaseExpAvg = 0
}
if currentVal != p.last {
p.changeCounter++
p.lastChangeAt = time.Now()
} }
byteIncrease := float64(currentVal - p.last) if p.last.val != currentVal {
if byteIncrease < 0 { p.changeCount++
byteIncrease = 0 p.lastChange = time.Now()
} }
const factor = 0.1
p.bpsIncreaseExpAvg = (1-factor) * p.bpsIncreaseExpAvg + factor *byteIncrease if time.Now().Sub(p.lastChange) > 3 * time.Second {
return int64(p.bpsIncreaseExpAvg), p.changeCounter p.last = nil
return 0, 0
}
deltaV := currentVal - p.last.val;
deltaT := time.Now().Sub(p.last.time)
rate := float64(deltaV) / deltaT.Seconds()
factor := 0.3
p.bpsAvg = (1-factor) * p.bpsAvg + factor * rate
p.last.time = time.Now()
p.last.val = currentVal
return int64(p.bpsAvg), p.changeCount
} }
type tui struct { type tui struct {