Performance monitoring with System.Diagnostics in .NET Framework

Performance of your application is very important to the end user. After you release your project for production deployment and users start hitting your web site you may run into problems with the performance of your site for example. Several areas to pay attention when dealing with performance issues are: User Interface Slowness, Database Connectivity Slowness, Network Connectivity Slowness, and File Input/Output slowness. As we can see there are many areas of your application where performance degradation can come from. It’s your job to find out where and why. Thankfully .NET Framework takes care of helping us with theSystem.Diagnostics namespace.

All the applications that we run on the server use processes and processes are at the center of our interest in understanding how our application runs. Process is a task performed by any give OS. Our application can have multiple processes behind it. Process can also be visualized as 4GB of space with the following memory slots from 0x00000000 to 0xFFFFFFF. 4GB is a standard space allocation in 32 bit machine and memory allocates whatever memory is required by the process. There is also concept of Virtual Memory when 4KB of memory saved to disk.

.NET Framework provides Process class that can access OS level process on local machine or on remote machine. Four methods can be used to determine what processes are currently running on the machine.

GetCurrentProcess – it does not take any parameters
Process MyProcess = Process.GetCurrentProcess();
Console.WriteLine("Process: " + MyProcess);

GetProcessById returns information about individual processes.
Process SpecificProcess = null;

try
{
    SpecificProcess = Process.GetProcessById(2);
    Console.WriteLine(SpecificProcess.ProcessName);
}
catch (ArgumentException Problem)
{
    Console.WriteLine(Problem.Message);
}

GetProcessesByName returns information about multiple modules loaded by the process.
Process[] SpecificProcesses = null;
SpecificProcesses = Process.GetProcessesByName("explorer", "machinename");
  foreach (Process ThisProcess in SpecificProcesses)
  {
    Console.WriteLine(ThisProcess.ProcessName);
  }

GetProcesses method takes machine names and returns all processes being currently executed on the machine. It has several Exceptions. They are: PlatformNotSupportedException, ArgumentNullException, InvalidOperationException, Win32Exception, ArgumentException

Process[] AllMyProcesses = null;
  try
  {
    AllMyProcesses = Process.GetProcesses("mymachinename");
    foreach (Process MyCurrent in AllMyProcesses)
    {
      Console.WriteLine(MyCurrent.ProcessName);
    }
  }
  catch (ArgumentException MyProblem)
  {
      Console.WriteLine(MyProblem.Message);
  }