/// <summary> /// Specify that some bytes were transferred in a particular window. /// </summary> /// <param name="window">Number representing time, e.g. 1 = 100ms to 200ms, 2 = 200ms to 300ms, etc</param> /// <param name="value">Count of bytes transferred</param> public void Accumulate(long window, long value) { if (window < 0) { throw new ArgumentOutOfRangeException("window"); } // Special case: initialize the top window on the first call to Accumulate() if (currentWindow == -1) { currentWindow = window; } // If trying to accumulate old data, put it in the current window if (window < currentWindow) { window = currentWindow; } // If these bytes are not in the current window, put the total bytes transferred // in the current window to the previous bytes queue if (window > currentWindow) { previousBytesTransferred.Add(currentBytesAccumulator.Total()); currentBytesAccumulator.Clear(); currentWindow++; } // If there is a gap between the current window and this window, enqueue // the appropriate number of empty windows if (window != currentWindow) { previousBytesTransferred.AddN(window - currentWindow); currentWindow = window; } // finally accumulate the bytes transferred currentBytesAccumulator.Accumulate(value); }
public void TestAddN() { var a = new AccumulatingCircularBuffer <long, long>(3, new SumAccumulator()); CheckBuffer(a, new long[] { }, 0); a.AddN(0); CheckBuffer(a, new long[] { }, 0); a.AddN(1); CheckBuffer(a, new long[] { 0 }, 0); a.Add(10); a.Add(20); a.Add(30); CheckBuffer(a, new long[] { 10, 20, 30 }, 60); a.AddN(1); CheckBuffer(a, new long[] { 20, 30, 0 }, 50); a.Add(10); a.Add(20); a.Add(30); CheckBuffer(a, new long[] { 10, 20, 30 }, 60); a.AddN(2); CheckBuffer(a, new long[] { 30, 0, 0 }, 30); a.Add(10); a.Add(20); a.Add(30); CheckBuffer(a, new long[] { 10, 20, 30 }, 60); a.AddN(3); CheckBuffer(a, new long[] { 0, 0, 0 }, 0); // Test that adding a huge number is optimised away a.Add(10); a.Add(20); a.Add(30); CheckBuffer(a, new long[] { 10, 20, 30 }, 60); a.AddN(long.MaxValue); CheckBuffer(a, new long[] { 0, 0, 0 }, 0); }