public void MeasuresEventTimeWithHighResolution() { if (Environment.OSVersion.Platform != PlatformID.Win32NT) { // We only use explicit high-precision timestamping on Windows, relying on .NET implementation defaults for other platforms return; } var healthReporterMock = new Mock <IHealthReporter>(); // Ensure the EventSource is instantiated EventSourceInputTestSource.Log.Message("warmup"); List <DateTimeOffset> eventTimes = new List <DateTimeOffset>(); Action <EventWrittenEventArgs> eventHandler = e => eventTimes.Add(EventDataExtensions.ToEventData(e, healthReporterMock.Object, "context-unused").Timestamp); var twoMilliseconds = Math.Round(Stopwatch.Frequency / 500.0); using (var listener = new EventSourceInputTestListener(eventHandler)) { for (int i = 0; i < 8; i++) { EventSourceInputTestSource.Log.Message(i.ToString()); var sw = Stopwatch.StartNew(); while (sw.ElapsedTicks < twoMilliseconds) { // Spin wait } } } // Event timestamps should have less than 1 ms resolution and thus should all be different Assert.Equal(8, eventTimes.Distinct().Count()); }
public void MeasuresEventTimeWithHighResolution() { var healthReporterMock = new Mock <IHealthReporter>(); // Ensure the EventSource is instantiated EventSourceInputTestSource.Log.Message("warmup"); List <DateTimeOffset> eventTimes = new List <DateTimeOffset>(); Action <EventWrittenEventArgs> eventHandler = e => eventTimes.Add(EventDataExtensions.ToEventData(e, healthReporterMock.Object, "context-unused").Timestamp); var hundredMicroseconds = Math.Round(Stopwatch.Frequency / 10000.0); using (var listener = new EventSourceInputTestListener(eventHandler)) { for (int i = 0; i < 8; i++) { EventSourceInputTestSource.Log.Message(i.ToString()); var sw = Stopwatch.StartNew(); while (sw.ElapsedTicks < hundredMicroseconds) { // Spin wait } } } Assert.True(eventTimes.Distinct().Count() == 8, "Event timestamps should have less than 1 ms resolution and thus should all be different"); }