Skip to main content

Performance Monitoring in .Net


Namespace: System.Diagnostics

Process
A Process component provides access to a process that is running on a computer. A process, in the simplest terms, is a running application. A thread is the basic unit to which the operating system allocates processor time. A thread can execute any part of the code of the process, including parts currently being executed by another thread.

The Process component is a useful tool for starting, stopping, controlling, and monitoring applications. Using the Process component, you can obtain a list of the processes that are running, or you can start a new process. A Process component is used to access system processes. After a Process component has been initialized, it can be used to obtain information about the running process. Such information includes the set of threads, the loaded modules (.dll and .exe files), and performance information such as the amount of memory the process is using.

Process Properties:
Process.GetProcessByID
Process.GetProcessByName
Process.GetProcesses

Performance Counter
It represents a Windows NT performance counter component.

Methods
NextSample       Obtains a counter sample, and returns the raw, or uncalculated, value for it.
NextValue          Obtains a counter sample and returns the calculated value for it.

CounterSampleCalculator.ComputeCounterValue(MyPerformanceCounter.NextSample(),MyPerformanceCounter.NextSample());

CounterCreationData
CounterCreationData is helpful in a scenario where we want to setup our own counter and PerformanceCounter Categories.

CounterCreationDataCollection CCDC = new CounterCreationDataCollection();
                                               
// Add the counter.
CounterCreationData averageCount64 = new CounterCreationData();
averageCount64.CounterType = PerformanceCounterType.AverageCount64;
averageCount64.CounterName = "AverageCounter64Sample";
CCDC.Add(averageCount64);

// Create the category.
PerformanceCounterCategory.Create("AverageCounter64SampleCategory",
"Demonstrates usage of the AverageCounter64 performance counter type.",CCDC);
               
Properties of CounterCreationData           
CounterHelp
CounterName
CounterType

PerformanceCounterCategory
Main Methods include Create, Delete, Exists, GetCategories and ReadCategory.
GetCategories gives a list of categories on a machine.
ReadCategory will read all the counter and performance object instance data that is associated with this category.

Some Important Performance Counters
PerformanceCounter pc = new PerformanceCounter(“ASP.NET”,”Applications Running”);

PerformanceCounter pc = new PerformanceCounter(“.NET CLR Memory”,”# Bytes in all Heaps”);
Pc.InstanceName = “_Global_”;

PerformanceCounter pc = new PerformanceCounter(“.NET CLR Exceptions”,”# of Exceps Thrown”);
Pc.InstanceName = “_Global_”;



Starting a Process

ProcessStartInfo info = new ProcessStartInfo();
Info.FileName = “iexplorer.exe”;
Process.Start(info);

Main ProcessStartInfo Properties
FileName                        executable path or system executable name;
Arguments                     String specifying multiple arguments with spaces.
UseShellExecute            Set to false if requiring Password from user.
UserName                      Set to Enviorment.UserName if required authentication.
Password                        Set to SecureString obtained from user if required authentication.
Domain                           Set to domain that user selects against which we have to authenticate.

StackFrame
A StackFrame is created and pushed on the call stack for every function call made during the execution of a thread. The stack frame always includes MethodBase information, and optionally includes file name, line number, and column number information.

Constructors
Name
Description
StackFrame ()
Initializes a new StackFrame object.
StackFrame (Boolean)
Initializes a new StackFrame object, optionally capturing source information.
StackFrame (Int32)
Initializes a new StackFrame object that corresponds to a frame above the current stack frame.
StackFrame (Int32, Boolean)
Initializes a new StackFrame object that corresponds to a frame above the current stack frame, optionally capturing source information.
StackFrame (String, Int32)
Initializes a new StackFrame object that only contains the given file name and line number.
StackFrame (String, Int32, Int32)
Initializes a new StackFrame object that only contains the given file name, line number, and column number.

Important Methods
GetFileColumnNumber
Gets the column number in the file that contains the code that is executing. This information is typically extracted from the debugging symbols for the executable.
GetFileLineNumber
Gets the line number in the file that contains the code that is executing. This information is typically extracted from the debugging symbols for the executable.
GetFileName
Gets the file name that contains the code that is executing. This information is typically extracted from the debugging symbols for the executable.
GetMethod
Gets the method in which the frame is executing.

StackTrace
Represents a stack trace, which is an ordered collection of one or more stack frames. StackTrace might not report as many method calls as expected, due to code transformations that occur during optimization.

StackTrace st = new StackTrace(true);
for(int i =0; i< st.FrameCount; i++ )
{
  // Note that high up the call stack, there is only
  // one stack frame.
  StackFrame sf = st.GetFrame(i);
  Console.WriteLine();
  Console.WriteLine("High up the call stack, Method: {0}", sf.GetMethod());

  Console.WriteLine("High up the call stack, Line Number: {0}",
  sf.GetFileLineNumber());
}

Methods:
GetFrame
Gets the specified stack frame.
GetFrames
Returns a copy of all stack frames in the current stack trace.

Comments

Popular posts from this blog

Culture Information and Localization in .NET

Namespace: System.Globalization CultureInfo Class:                 It provides information like the Format of numbers and dates, Culture’s Calendar, Culture’s language and sublanguage (if applicable), Country and region of the culture. The Basic use of CultureInfo class is shown here: • How string Comparisons are performed • How Number Comparison & Formats are performed • Date Comparison and Formats. • How resources are retrieved and used. Cultures are grouped into three categories: Invariant Culture : It’s Culture Insensitive. It can be used to build some trial application. It can be also used to build an application with hard-coded expiry date that ignores cultures. But using it for every comparison will be incorrect and inappropriate. Neutral Culture : English(en), Frensh(fr), and Spanish(sp). A neutral culture is related to language but it’s not related to specific regi...

Concept of App Domain in .Net

Creating Application Domains: Application domain is just like process, provides separate memory space, and isolates from other code. But it’s quite light weight. It also provides the following advantages: 1-       Reliability : If a domain crashes, it can be unloaded. Hence doesn’t affect the other assemblies. 2-       Efficiency : Loading all assemblies in one domain can be cumbersome and can make the process heavy but Appdomains are efficient in this manner. Important properties of AppDomain: ApplicationIdentity , ApplicationTrust , BaseDirectory , CurrentDomain , DomainManager , DomainDirectory , Evidence , FriendlyName , ID , RelativeSearchPath , SetupInformation , ShadowCopyFiles . Important methods of AppDomain: ApplyPolicy , CreateCOMInstanceFrom , CreateDomain , CreateInstance (Assembly). To create an AppDomain: AppDomain adomain = AppDomain.CreateDomain(“D”); To execute an assembly:...

Asynchronous Execution in ASP.NET

Asynchronous Execution: Two ways either implement IHTTPAsyncHandler interface or in ASP.NET 2.0 set <%@ Page Async=”true” %>. The second option implements IHTTPAsyncHandler interface automatically for the page when parsed or compiled. AddOnPreRenderCompleteAsync ( new BeginEventHandler(BeginTask), new EndEventHandler(EndTask)); AddOnPreRenderCompleteAsync() shoud be called in Page_load. The BeginEventHandler and EndEventHandler are delegates defined as follows: IAsyncResult BeginEventHandler( object sender, EventArgs e, AsyncCallback cb, object state) void EndEventHandler( IAsyncResult ar) AsyncProcess starts and completes between PreRender and PreRenderComplete. Other way to perform Async Task is using AsyncPageTask structure. It also allows multiple tasks to execute simultaneously. void Page_Load (object sender, EventArgs e) { PageAsyncTask task = new PageAsyncTask( new BeginEventHandler(BeginTask), new EndEventH...