How to use performance counters to diagnose performance of WCF applications

There are many ways to diagnose the performance of WCF applications. Here is a simple one that uses performance counters to get some rough ideas. There are two categories of performance counters that you can use:

· ASP.NET counters: https://msdn2.microsoft.com/en-us/library/fxk122b4(vs.71).aspx

· WCF counters: https://msdn2.microsoft.com/en-us/library/ms735098.aspx.

ASP.NET Counters

For web-hosted applications, you can use ASP.NET counters to monitor the overall request processing status. They are enabled by default and they are of type PerformanceCounterInstanceLifetime.Global. So you can open the PerfMon tool to monitor the counters without doing any extra work. Here are the two interesting counters that I want to mention here:

· Requests/sec

· Request Execution Time

The first one tells the throughput of the whole web server for ASP.NET requests (including WCF ones). You may have many clients hitting the same server and thus you will see the overall throughput.

The second counter tells you how long (in milliseconds) it takes for the server to process each request. It measures the latency of the request processing. If the requests are processed sequentially, the value (converted into seconds) of this counter is the inverse of the first one. In general when the requests are processed concurrently, this is not true.

Performance counters are a mechanism for monitoring services that you can get without

WCF Counters

WCF performance counters are quite different. Here are major ones:

· These counters are not enabled by default in V1. You have to do that through the config file.

· All counters are of type PerformanceCounterInstanceLifetime.Process. All of the three different categories (Service, Endpoint, and Operation) are of this type which is also called “instanced based”. This means that in order to monitor the counters from PerfMon, you have to start the WCF application with the counters enabled. When the application exits, you cannot monitor the counter anymore.

· The WCF counters are more finer grained than ASP.NET ones. You can monitor the performance of different service instances at different levels. To recap, the counter instance names have the following pattern:

(ServiceName).(ContractName).(OperationName)@(first endpoint listener address)

Here is the way of how to enable WCF performance counters through configuration:

<system.serviceModel>
<diagnostics performanceCounters="All" />
</system.serviceModel>

The possible settings for the scope of service counters are Off, ServiceOnly, and All for WCF V1.

Here are the two interesting performance counters that we can use for performance analysis:

· Calls Per Second

· Calls Duration

These counters have similar properties as those for ASP.NET. But they are scoped to WCF service (endpoint or operation) level. The value of the second counter is a floating number in seconds. The accuracy is 0.001 second. So if your service is faster than 1000 requests/second in sequential processing, you would always see 0.000.

How to Use the Values of the Counters

Once you have the data captured in PerfMon, you will be able to tell which WCF service is the performance bottleneck. This is useful when you have very slow service operations. If you want advanced performance analysis, you would need more granular tools such as ETW tracing, performance profiles, custom tracing, and debuggers.

Lastly, before adding the performance counters to PerfMon, make sure that 1) you have them enabled from the configuration file, and 2) you have started the WCF application. For web-hosted service, you can ping the service from the browser first to make sure the service has been started.