User Tools

Site Tools



C# - How to use performance counter ?

How to monitor the execution of an application ? When you are developer, you can offer tools to do that using the concept of Performance Monitor on the framework .Net

Performance counters

TODO : Define what are performance counter ?

They are registred in the namespace System.Diagnostics.PerformanceCounter.

I can't list all of them. That's depends of your machine.

This programs helps you to list the category and the performance counter associate on your computer. The reporting is done on the console out or in a file if you give a file name (Warning, the console output is unreadable on french installation if you simply flush the console out. There is an encoding issue).

var list_category = PerformanceCounterCategory
// Get list of generic counter
List<string> list = list_category
  .Where(cat => cat.GetInstanceNames().Any() == false) // all the generic counter
  .Select(cat => cat.GetCounters("")).SelectMany(counter => counter)
  .Select(counter => string.Format("{0} - {1}", counter.CategoryName, counter.CounterName)).ToList();
// Get list of performance counter rattached to an instance
List<string> list_instance = list_category
  .Where(cat => cat.CategoryName != "Thread") // Filter Thread category that expose 15000 counters on my computer but access to some of them provoke crash
  .Where(cat => cat.GetInstanceNames().Any() == true) // all the instanciated counter
  .SelectMany(cat => cat.GetInstanceNames().Select(inst => cat.GetCounters(inst)).SelectMany(counter => counter))
  .Select(counter => string.Format("{0} - {1} - {2}", counter.InstanceName, counter.CategoryName, counter.CounterName)).ToList();

Exemple of category

Exceptions CLR .NET
Fournisseur de données .NET pour SqlServer
ASP.NET v4.0.30319
Windows Workflow Foundation
WF (System.Workflow)
Sécurité CLR .NET

Exemple of generic performance counter

Mémoire - Défauts de page/s
Mémoire - Octets disponibles
Mémoire - Octets validés
Mémoire - Limite de mémoire dédiée
Mémoire - Copies pour écriture/s
Mémoire - Défauts en transit/s
Mémoire - Défauts de cache/s
Mémoire - Défauts de page à zéro/s
Mémoire - Pages/s

Exemple of instanciated performance counter

chrome#7 - Processus - % temps processeur
chrome#7 - Processus - % temps utilisateur
chrome#7 - Processus - % temps privilégié
chrome#7 - Processus - Taille virtuelle (octets)
chrome#7 - Processus - Taille virtuelle (octets)
chrome#7 - Processus - Défauts de page/s

Add performance counter in your application

Performance monitoring

You can monitor your application through the administration tool performance monitor.

technical_notes/public/20131013-cs-use-performance-counter.txt · Last modified: 2013/10/13 23:29 by Fabien Arcellier