Performance Counter instance under CodeTimer. net in asp.net

Source: Internet
Author: User

This article introduces the performance counter instance under CodeTimer. net in asp.net. If you need it, refer to the example.

Because this is winform, I changed it to WEB output. The execution result is as follows:

The complete code is as follows:

The Code is as follows: Copy code

Using System;
Using System. Web;
Using System. Collections. Generic;
Using System. Text;
Using System. Diagnostics;
Using System. Threading;
Using System. Runtime. InteropServices;

Namespace MYORM. COMMON
{
Public interface IAction
{
Void Action ();
}

Public static class CodeTimer
{
[DllImport ("kernel32.dll", SetLastError = true)]
Static extern bool GetThreadTimes (IntPtr hThread, out long lpCreationTime,
Out long lpExitTime, out long lpKernelTime, out long lpUserTime );

[DllImport ("kernel32.dll")]
Static extern IntPtr GetCurrentThread ();

Public delegate void ActionDelegate ();

Private static long GetCurrentThreadTimes ()
{
Long l;
Long kernelTime, userTimer;
GetThreadTimes (GetCurrentThread (), out l, out l, out kernelTime,
Out userTimer );
Return kernelTime + userTimer;
}

Static CodeTimer ()
{
Process. GetCurrentProcess (). PriorityClass = ProcessPriorityClass. High;
Thread. CurrentThread. Priority = ThreadPriority. Highest;

}

Public static void Time (string name, int iteration, ActionDelegate action)
{
If (String. IsNullOrEmpty (name ))
{
Return;
}

If (action = null)
{
Return;
}

// 1. Print name
// Lelecolor currentForeColor = Console. ForegroundColor;
// Console. ForegroundColor = ConsoleColor. Yellow;
// Console. WriteLine (name );
HttpContext. Current. Response. Write ("<font color =" blue ">" + name + "</font> <br/> ");

// 2. Record the latest GC counts
// GC. Collect (GC. MaxGeneration, GCCollectionMode. Forced );
GC. Collect (GC. MaxGeneration );
Int [] gcCounts = new int [GC. MaxGeneration + 1];
For (int I = 0; I <= GC. MaxGeneration; I ++)
{
GcCounts [I] = GC. CollectionCount (I );
}

// 3. Run action
Stopwatch watch = new Stopwatch ();
Watch. Start ();
Long ticksFst = GetCurrentThreadTimes (); // 100 nanosecond one tick

For (int I = 0; I <iteration; I ++) action ();
Long ticks = GetCurrentThreadTimes ()-ticksFst;
Watch. Stop ();

// 4. Print CPU
// Console. ForegroundColor = currentForeColor;
HttpContext. Current. Response. Write ("all time consumed: tt" + watch. ElapsedMilliseconds. ToString ("N0") + "ms <br/> ");
HttpContext. Current. Response. Write ("execution time:" + (watch. ElapsedMilliseconds/iteration). ToString ("N0") + "ms <br/> ");

// HttpContext. Current. Response. Write ("CPU time: tt" + (ticks * 100). ToString ("N0") + "ns <br/> ");
// HttpContext. current. response. write ("CPU time (one time): t" + (ticks * 100/iteration ). toString ("N0") + "ns <br/> ");

// 5. Print GC
// For (int I = 0; I <= GC. MaxGeneration; I ++)
//{
// Int count = GC. CollectionCount (I)-gcCounts [I];
// HttpContext. Current. Response. Write ("tGen" + I + ": ttt" + count + "<br/> ");
//}

HttpContext. Current. Response. Write ("<br/> ");

}

Public static void Time (string name, int iteration, IAction action)
{
If (String. IsNullOrEmpty (name ))
{
Return;
}

If (action = null)
{
Return;
}

// 1. Print name
// Lelecolor currentForeColor = Console. ForegroundColor;
// Console. ForegroundColor = ConsoleColor. Yellow;
HttpContext. Current. Response. Write (name + "<br/> ");


// 2. Record the latest GC counts
// GC. Collect (GC. MaxGeneration, GCCollectionMode. Forced );
GC. Collect (GC. MaxGeneration );
Int [] gcCounts = new int [GC. MaxGeneration + 1];
For (int I = 0; I <= GC. MaxGeneration; I ++)
{
GcCounts [I] = GC. CollectionCount (I );
}

// 3. Run action
Stopwatch watch = new Stopwatch ();
Watch. Start ();
Long ticksFst = GetCurrentThreadTimes (); // 100 nanosecond one tick

For (int I = 0; I <iteration; I ++) action. Action ();
Long ticks = GetCurrentThreadTimes ()-ticksFst;
Watch. Stop ();

// 4. Print CPU
// Console. ForegroundColor = currentForeColor;
HttpContext. Current. Response. Write ("tTime Elapsed: tt" +
Watch. ElapsedMilliseconds. ToString ("N0") + "ms <br/> ");
HttpContext. Current. Response. Write ("tTime Elapsed (one time):" +
(Watch. ElapsedMilliseconds/iteration). ToString ("N0") + "ms <br/> ");

HttpContext. Current. Response. Write ("tCPU time: tt" + (ticks * 100). ToString ("N0") + "ns <br/> ");
Console. WriteLine ("tCPU time (one time): t" + (ticks * 100/
Iteration). ToString ("N0") + "ns ");

// 5. Print GC
For (int I = 0; I <= GC. MaxGeneration; I ++)
{
Int count = GC. CollectionCount (I)-gcCounts [I];
HttpContext. Current. Response. Write ("tGen" + I + ": ttt" + count + "<br/> ");
}

}
}
}

Use the following (GetUser and GetUserBySql are two methods ):

The Code is as follows: Copy code

MYORM. COMMON. CodeTimer. Time ("MYORM", 10000, () =>{ GetUser ();});
MYORM. COMMON. CodeTimer. Time ("SQL", 10000, () =>{ GetUserBySql ();});

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.