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());
        }
Пример #2
0
        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");
        }
Пример #3
0
        public void UsesIsoDateFormat()
        {
            EventData e = new EventData();

            e.Payload.Add("DateTimeProperty", new DateTime(2017, 4, 19, 10, 15, 23, DateTimeKind.Utc));
            e.Payload.Add("DateTimeOffsetProperty", new DateTimeOffset(2017, 4, 19, 10, 16, 07, TimeSpan.Zero));

            var    messagingData = EventDataExtensions.ToMessagingEventData(e, EventFlowJsonUtilities.GetDefaultSerializerSettings(), out int messageSize);
            string messageBody   = Encoding.UTF8.GetString(messagingData.Body.Array, messagingData.Body.Offset, messagingData.Body.Count);

            var dateTimeRegex = new Regex(@"""DateTimeProperty"" \s* : \s* ""2017-04-19T10:15:23Z""", RegexOptions.IgnorePatternWhitespace, TimeSpan.FromMilliseconds(100));

            Assert.Matches(dateTimeRegex, messageBody);

            var dateTimeOffsetRegex = new Regex(@"""DateTimeOffsetProperty"" \s* : \s* ""2017-04-19T10:16:07\+00:00""", RegexOptions.IgnorePatternWhitespace, TimeSpan.FromMilliseconds(100));

            Assert.Matches(dateTimeOffsetRegex, messageBody);
        }
        public void UsesIsoDateFormat()
        {
            EventData e = new EventData();

            e.Payload.Add("DateTimeProperty", new DateTime(2017, 4, 19, 10, 15, 23, DateTimeKind.Utc));
            e.Payload.Add("DateTimeOffsetProperty", new DateTimeOffset(2017, 4, 19, 10, 16, 07, TimeSpan.Zero));

            var          messagingData = EventDataExtensions.ToMessagingEventData(e, out int messageSize);
            StreamReader sr            = new StreamReader(messagingData.GetBodyStream());
            string       messageBody   = sr.ReadToEnd();

            var dateTimeRegex = new Regex(@"""DateTimeProperty"" \s* : \s* ""2017-04-19T10:15:23Z""", RegexOptions.IgnorePatternWhitespace, TimeSpan.FromMilliseconds(100));

            Assert.Matches(dateTimeRegex, messageBody);

            var dateTimeOffsetRegex = new Regex(@"""DateTimeOffsetProperty"" \s* : \s* ""2017-04-19T10:16:07\+00:00""", RegexOptions.IgnorePatternWhitespace, TimeSpan.FromMilliseconds(100));

            Assert.Matches(dateTimeOffsetRegex, messageBody);
        }