Performance Counter Usage

Component: Performance Counters
NuGet NServiceBus (6.x)

Install Counters

Refer to Performance Counters to see how to install the performance counters.

Enabling Counters For The Endpoint

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

// PerformanceCounters has been moved to an external nuget package: NServiceBus.Metrics.PerformanceCounters
// Old PerformanceCounter APIs marked obsolete in 6.2 of NServiceBus
#pragma warning disable 618
endpointConfiguration.EnableCriticalTimePerformanceCounter();
endpointConfiguration.EnableSLAPerformanceCounter(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 new data.

The Handler

The handler just has a random sleep to give achieve some fake load. Not that the max random number is greater than the above configured SLA to cause it to occasionally fire.

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 is in Program.cs is set to send 10 messages at a time.

Performance Counters Analysis

Critical Time

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

SLA Violation Countdown

The SLA Violation Countdown is the number of seconds left until the SLA for the particular endpoint is breached. So effectively SLA Violation Countdown is an inverse counter. Continually send message and the Critical Time will increase while the SLA Violation Countdown decreased.

Other counters

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 those performance counters.

Related Articles


Last modified