|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | + |
| 4 | +namespace Bytewizer.Backblaze.Utility |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Calculates transfer speed as a rolling average. Every time the position changes, |
| 8 | + /// the consumer notifies us and a timestamped sample is logged. |
| 9 | + /// </summary> |
| 10 | + public class SpeedCalculator |
| 11 | + { |
| 12 | + List<Sample> _samples = new List<Sample>(); |
| 13 | + |
| 14 | + struct Sample |
| 15 | + { |
| 16 | + public long Position; |
| 17 | + public DateTime DateTimeUTC; |
| 18 | + } |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// The length in seconds of the window across which speed is averaged. |
| 22 | + /// </summary> |
| 23 | + public const int WindowSeconds = 10; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Adds a position sample to the set. It is automatically timestamped. Samples |
| 27 | + /// should be monotonically increasing. If, for whatever reason, they are not, |
| 28 | + /// previously-added samples that are later in the file are discarded so that |
| 29 | + /// the set remains strictly increasing. |
| 30 | + /// </summary> |
| 31 | + /// <param name="position">The updated position of the operation.</param> |
| 32 | + public void AddSample(long position) |
| 33 | + { |
| 34 | + var sample = new Sample(); |
| 35 | + |
| 36 | + sample.Position = position; |
| 37 | + sample.DateTimeUTC = DateTime.UtcNow; |
| 38 | + |
| 39 | + // If we have walked backward for whatever reason, discard any samples past this |
| 40 | + // point so that we maintain the invariant of the sample set increasing position |
| 41 | + // monotonically. |
| 42 | + while ((_samples.Count > 0) && (_samples[_samples.Count - 1].Position > position)) |
| 43 | + _samples.RemoveAt(_samples.Count - 1); |
| 44 | + |
| 45 | + _samples.Add(sample); |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Calculates the current speed based on samples previously added by calls to |
| 50 | + /// <see cref="AddSample" />. The value of this function will change over time, |
| 51 | + /// even with no changes to the state of the <see cref="SpeedCalculator" /> |
| 52 | + /// instance, because the value is relative to the current date/time, and the |
| 53 | + /// samples with which the calculation is being made are timestamped. |
| 54 | + /// </summary> |
| 55 | + /// <returns>The average number of bytes per second being processed.</returns> |
| 56 | + public long CalculateBytesPerSecond() |
| 57 | + { |
| 58 | + var cutoff = DateTime.UtcNow.AddSeconds(-WindowSeconds); |
| 59 | + |
| 60 | + // Discard any samples that are outside of the averaging window. We will never |
| 61 | + // need them again. |
| 62 | + while ((_samples.Count > 0) && (_samples[0].DateTimeUTC < cutoff)) |
| 63 | + _samples.RemoveAt(0); |
| 64 | + |
| 65 | + if (_samples.Count < 2) |
| 66 | + return 0; |
| 67 | + |
| 68 | + var firstSample = _samples[0]; |
| 69 | + var lastSample = _samples[_samples.Count - 1]; |
| 70 | + |
| 71 | + long bytes = lastSample.Position - firstSample.Position; |
| 72 | + double seconds = (lastSample.DateTimeUTC - firstSample.DateTimeUTC).TotalSeconds; |
| 73 | + |
| 74 | + // If we don't have a meaningful span of time, clamp it. The number wouldn't |
| 75 | + // be terribly meaningful anyway. |
| 76 | + if (seconds < 0.01) |
| 77 | + seconds = 0.01; |
| 78 | + |
| 79 | + return (long)Math.Round(bytes / seconds); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments