Closed jraby closed 3 years ago
Thanks, this is great.
Yes, we do want to keep Pinger.rtts
, as some people use this library to get all of them for later processing (for example, calculating quantiles). But adding the live stats update makes it so we don't need the full rtts
list to calculate simple stats.
This PR implements Welford's online algorithm to compute the standard deviation of the round trip times as packets are received without storing the full list. (the average is taken from this algorithm too) As for min and max, they are also computed as we go.
I didn't touch the
rtts
array since it is part of the public interface of the Statistics, but with this PR it is not needed to get the stats.I wasn't sure what to do about the tests which set
p.PacketsRecv
and then filledp.rtts
with test values. Since the packet count is an inherent part of the algorithm, I opted to move the increment into theupdateStatistic
method and repeatedly call it with test data.One other thing: I think calling
Statistics()
from another goroutine could yield incorrect results sinceupdateStatistics()
could be running at the same time. Adding a RWLock should fix that. (I haven't done it yet since I do not use Pinger in this fashion and this particular problem was present in the original code where rtts can be modified whileStatistics()
is called)There's also a commit that renames
outPkt
toinPkt
inprocessPacket
since that function is dealing with received packets.(And this is all branched off b92053f because otherwise go-ping doesn't work on linux at the moment)
It might be easier to review commit by commit since they are self contained.