|
| 1 | +using System; |
| 2 | +using Xunit; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Xunit.Abstractions; |
| 5 | +using InfluxDB.Collector.Util; |
| 6 | + |
| 7 | +namespace InfluxDB.LineProtocol.Tests.Collector.Util |
| 8 | +{ |
| 9 | + |
| 10 | + public class PseudoHighResTimeStampSourceTests |
| 11 | + { |
| 12 | + private readonly ITestOutputHelper output; |
| 13 | + |
| 14 | + public PseudoHighResTimeStampSourceTests(ITestOutputHelper output) |
| 15 | + { |
| 16 | + this.output = output; |
| 17 | + } |
| 18 | + |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public void CanSupplyHighResTimestamps() |
| 22 | + { |
| 23 | + const int ITERATIONS = 100000; |
| 24 | + |
| 25 | + |
| 26 | + // Even if we call inside a very tight loop, |
| 27 | + // we should get better (pseudo) resolution than just DateTime.UtcNow |
| 28 | + long dateTimeCollisions = 0; |
| 29 | + DateTime previousDateTime = DateTime.UtcNow; |
| 30 | + for (int i = 0; i < ITERATIONS; i++) |
| 31 | + { |
| 32 | + // Attempt with date time directly |
| 33 | + var current = DateTime.UtcNow; |
| 34 | + |
| 35 | + if (previousDateTime == current) |
| 36 | + { |
| 37 | + dateTimeCollisions++; |
| 38 | + } |
| 39 | + previousDateTime = current; |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + if (0 == dateTimeCollisions) |
| 44 | + { |
| 45 | + // Warn because we do expect that datetime.now has collisions in such a tight loop |
| 46 | + output.WriteLine("Warning! Expected DateTime.UtcNow to have some collisions, but seemed to be high resolution already."); |
| 47 | + } |
| 48 | + |
| 49 | + // Now attempt to use our pseudo high precision provider that includes a sequence number to ensure that it |
| 50 | + // never collides |
| 51 | + var target = new PseudoHighResTimestampSource(); |
| 52 | + long highResTotalCollisions = 0; |
| 53 | + previousDateTime = target.GetUtcNow(); |
| 54 | + for (int i = 0; i < ITERATIONS; i++) |
| 55 | + { |
| 56 | + // Attempt with date time directly |
| 57 | + var current = target.GetUtcNow(); |
| 58 | + |
| 59 | + if (previousDateTime == current) |
| 60 | + { |
| 61 | + highResTotalCollisions++; |
| 62 | + } |
| 63 | + previousDateTime = current; |
| 64 | + } |
| 65 | + |
| 66 | + Assert.Equal(0, highResTotalCollisions); |
| 67 | + output.WriteLine($"No collisions detected with high resolution source, compared to {dateTimeCollisions} for DateTime.UtcNow."); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void CanSupplyHighResTimestampsInParallel() |
| 73 | + { |
| 74 | + const int ITERATIONS = 100000; |
| 75 | + |
| 76 | + // Even if we call inside a very tight loop, we should get better (pseudo) resolution than just DateTime.UtcNow |
| 77 | + int dateTimeCollisions = 0; |
| 78 | + DateTime previousDateTime = DateTime.UtcNow; |
| 79 | + Parallel.For(0, ITERATIONS, (i) => // (int i = 0; i < 100000; i++) |
| 80 | + { |
| 81 | + // Attempt with date time directly |
| 82 | + var current = DateTime.UtcNow; |
| 83 | + |
| 84 | + if (previousDateTime == current) |
| 85 | + { |
| 86 | + System.Threading.Interlocked.Increment(ref dateTimeCollisions); |
| 87 | + } |
| 88 | + previousDateTime = current; |
| 89 | + }); |
| 90 | + |
| 91 | + if (0 == dateTimeCollisions) |
| 92 | + { |
| 93 | + // Warn because we do expect that datetime.now has collisions in such a tight loop |
| 94 | + output.WriteLine("Warning! Expected DateTime.UtcNow to have some collisions, but seemed to be high resolution already."); |
| 95 | + } |
| 96 | + |
| 97 | + // Now attempt to use our pseudo high precision provider that includes a sequence number to ensure that it |
| 98 | + // never collides |
| 99 | + var target = new PseudoHighResTimestampSource(); |
| 100 | + int highResTotalCollisions = 0; |
| 101 | + previousDateTime = target.GetUtcNow(); |
| 102 | + Parallel.For(0, ITERATIONS, (i) => // (int i = 0; i < 100000; i++) |
| 103 | + { |
| 104 | + // Attempt with date time directly |
| 105 | + var current = target.GetUtcNow(); |
| 106 | + |
| 107 | + if (previousDateTime == current) |
| 108 | + { |
| 109 | + System.Threading.Interlocked.Increment(ref highResTotalCollisions); |
| 110 | + } |
| 111 | + previousDateTime = current; |
| 112 | + }); |
| 113 | + |
| 114 | + Assert.Equal(0, highResTotalCollisions); |
| 115 | + output.WriteLine($"No collisions detected with high resolution source, compared to {dateTimeCollisions} for DateTime.UtcNow."); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + [Fact] |
| 121 | + public void WillGiveUtcDateTimeKind() |
| 122 | + { |
| 123 | + var target = new PseudoHighResTimestampSource(); |
| 124 | + |
| 125 | + DateTime result = target.GetUtcNow(); |
| 126 | + Assert.Equal(DateTimeKind.Utc, result.Kind); |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + [Fact] |
| 131 | + public void WillNotDriftTooFarFromUtcNow() |
| 132 | + { |
| 133 | + var target = new PseudoHighResTimestampSource(); |
| 134 | + const int MAX_DRIFT_MS = 10; |
| 135 | + |
| 136 | + // Average over 10000 iterations and get the average drift |
| 137 | + decimal totalDrift = 0; |
| 138 | + const int ITERATIONS = 10000; |
| 139 | + for (int i = 0; i < ITERATIONS; i++) |
| 140 | + { |
| 141 | + DateTime current = DateTime.UtcNow; |
| 142 | + DateTime result = target.GetUtcNow(); |
| 143 | + |
| 144 | + totalDrift += Convert.ToDecimal((result - current).TotalMilliseconds); |
| 145 | + } |
| 146 | + var averageDrift = totalDrift / ITERATIONS; |
| 147 | + |
| 148 | + if (averageDrift > MAX_DRIFT_MS) |
| 149 | + { |
| 150 | + output.WriteLine($"Expected times were more than {MAX_DRIFT_MS}ms apart. Instead they were {averageDrift}ms apart."); |
| 151 | + Assert.True(false); // Force fail. |
| 152 | + } |
| 153 | + else |
| 154 | + { |
| 155 | + output.WriteLine ($"Total Drift over {ITERATIONS} iterations: {totalDrift}ms. Average {averageDrift}ms"); |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | + |
0 commit comments