Performance Counter Usage

Component: Performance Counters
NuGet Package NServiceBus.Metrics.PerformanceCounters (3.x)
Target NServiceBus Version: 7.x

Install Counters

Refer to Performance Counters to see how the performance counters should be installed.

Enabling Counters For The Endpoint

Both the SLA and Critical Time counters are enabled and configured in configuration code.

var performanceCounters = endpointConfiguration.EnableWindowsPerformanceCounters();
performanceCounters.EnableSLAPerformanceCounters(TimeSpan.FromSeconds(100));

The other counters are enabled by default.

Regardless of whether the SLA and Critical Time counters are enabled, all endpoints must be restarted after installing the counters in order to start collecting the counter data.

The Handler

The handler has a random delay to give achieve some fake load. The maximum random number is greater than the above configured SLA value to occasionally cause SLA violations.

public class MyHandler :
    IHandleMessages<MyMessage>
{
    static ILog log = LogManager.GetLogger<MyHandler>();

    static Random random = new Random();

    public Task Handle(MyMessage message, IMessageHandlerContext context)
    {
        var sleepTime = random.Next(1, 1000);
        log.Info($"Hello from MyHandler. Slept for {sleepTime}ms");
        return Task.Delay(sleepTime);
    }
}

Run Solution

Run the solution so that the Performance Counter instances are registered.

Add Counters in Performance Monitor

  1. Start Windows Performance Monitor.
  2. Clear the default counters.
  3. Add the NServiceBus Counters

Send Messages

Send any number of messages and watch the effect on the specific Performance Counters.

The sending code in Program.cs is set to send 10 messages at a time.

Performance Counters Analysis

Critical Time

Continually sending more messages will cause the load on the endpoint to increase. This will eventually result a back-logged queue. Messages will spend longer in the queue resulting in a gradually increase in Critical Time. Stop sending messages and eventually the endpoint will catch up and the Critical Time will drop back to zero.

SLA Violation Countdown

The SLA Violation Countdown is the number of seconds left until the SLA for the particular endpoint is breached, so effectively this is an inverse counter. When messages are sent continually, the Critical Time will increase while the SLA Violation Countdown will decreased.

Other counters

The following counters are only available in Version 4 and above.

To visualize both success and failures in the same view change the handler code to the following.

int sleepTime = random.Next(1, 1000);
Thread.Sleep(sleepTime);
if (sleepTime%2 != 0)
{
    throw new Exception();
}
log.Info($"Hello from MyHandler. Slept for {sleepTime}ms");

Run the end point and send some messages and monitor the results of all the performance counters.

Related Articles


Last modified