Example #1
0
    public async Task VerifyCountersFireWithCorrectValues()
    {
        // Arrange
        var eventListener = new TestCounterListener(new[] {
            "requests-per-second",
            "total-requests",
            "current-requests",
            "failed-requests"
        });

        var hostingEventSource = GetHostingEventSource();

        using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));

        var rpsValues            = eventListener.GetCounterValues("requests-per-second", timeoutTokenSource.Token).GetAsyncEnumerator();
        var totalRequestValues   = eventListener.GetCounterValues("total-requests", timeoutTokenSource.Token).GetAsyncEnumerator();
        var currentRequestValues = eventListener.GetCounterValues("current-requests", timeoutTokenSource.Token).GetAsyncEnumerator();
        var failedRequestValues  = eventListener.GetCounterValues("failed-requests", timeoutTokenSource.Token).GetAsyncEnumerator();

        eventListener.EnableEvents(hostingEventSource, EventLevel.Informational, EventKeywords.None,
                                   new Dictionary <string, string>
        {
            { "EventCounterIntervalSec", "1" }
        });

        // Act & Assert
        hostingEventSource.RequestStart("GET", "/");

        Assert.Equal(1, await totalRequestValues.FirstOrDefault(v => v == 1));
        Assert.Equal(1, await rpsValues.FirstOrDefault(v => v == 1));
        Assert.Equal(1, await currentRequestValues.FirstOrDefault(v => v == 1));
        Assert.Equal(0, await failedRequestValues.FirstOrDefault(v => v == 0));

        hostingEventSource.RequestStop();

        Assert.Equal(1, await totalRequestValues.FirstOrDefault(v => v == 1));
        Assert.Equal(0, await rpsValues.FirstOrDefault(v => v == 0));
        Assert.Equal(0, await currentRequestValues.FirstOrDefault(v => v == 0));
        Assert.Equal(0, await failedRequestValues.FirstOrDefault(v => v == 0));

        hostingEventSource.RequestStart("POST", "/");

        Assert.Equal(2, await totalRequestValues.FirstOrDefault(v => v == 2));
        Assert.Equal(1, await rpsValues.FirstOrDefault(v => v == 1));
        Assert.Equal(1, await currentRequestValues.FirstOrDefault(v => v == 1));
        Assert.Equal(0, await failedRequestValues.FirstOrDefault(v => v == 0));

        hostingEventSource.RequestFailed();
        hostingEventSource.RequestStop();

        Assert.Equal(2, await totalRequestValues.FirstOrDefault(v => v == 2));
        Assert.Equal(0, await rpsValues.FirstOrDefault(v => v == 0));
        Assert.Equal(0, await currentRequestValues.FirstOrDefault(v => v == 0));
        Assert.Equal(1, await failedRequestValues.FirstOrDefault(v => v == 1));
    }
Example #2
0
    public async Task TracksDurationSpentInQueue()
    {
        // Arrange
        using var eventListener = new TestCounterListener(new[] {
            "queue-length",
            "queue-duration",
            "requests-rejected",
        });

        using var eventSource = GetConcurrencyLimiterEventSource();

        using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(5));

        var durationValues = eventListener.GetCounterValues("queue-duration", timeoutTokenSource.Token).GetAsyncEnumerator();

        eventListener.EnableEvents(eventSource, EventLevel.Informational, EventKeywords.None,
                                   new Dictionary <string, string>
        {
            { "EventCounterIntervalSec", ".1" }
        });

        // Act
        Assert.True(await UntilValueMatches(durationValues, 0));

        using (eventSource.QueueTimer())
        {
            Assert.True(await UntilValueMatches(durationValues, 0));
        }

        // check that something (anything!) has been written
        while (await durationValues.MoveNextAsync())
        {
            if (durationValues.Current > 0)
            {
                return;
            }
        }

        throw new TimeoutException();
    }
Example #3
0
    public async Task TracksQueueLength()
    {
        // Arrange
        using var eventListener = new TestCounterListener(new[] {
            "queue-length",
            "queue-duration",
            "requests-rejected",
        });

        using var eventSource = GetConcurrencyLimiterEventSource();

        using var timeoutTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));

        var lengthValues = eventListener.GetCounterValues("queue-length", timeoutTokenSource.Token).GetAsyncEnumerator();

        eventListener.EnableEvents(eventSource, EventLevel.Informational, EventKeywords.None,
                                   new Dictionary <string, string>
        {
            { "EventCounterIntervalSec", ".1" }
        });

        // Act
        eventSource.RequestRejected();

        Assert.True(await UntilValueMatches(lengthValues, 0));
        using (eventSource.QueueTimer())
        {
            Assert.True(await UntilValueMatches(lengthValues, 1));

            using (eventSource.QueueTimer())
            {
                Assert.True(await UntilValueMatches(lengthValues, 2));
            }

            Assert.True(await UntilValueMatches(lengthValues, 1));
        }

        Assert.True(await UntilValueMatches(lengthValues, 0));
    }