Asp.net simple performance counters-improved by lazhao

Source: Internet
Author: User
Tags static class

 

The code is as follows: Copy code

Public static class CodeTimer
    {
Private static bool isQueryThreadCycleTime = false;

Public static void Initialize ()
        {
Process. GetCurrentProcess (). PriorityClass = ProcessPriorityClass. High;
Thread. CurrentThread. Priority = ThreadPriority. Highest;
If (Environment. OSVersion. Platform = PlatformID. Win32NT & Environment. OSVersion. Version. Major> = 6)
            {
IsQueryThreadCycleTime = true;
            }
Time ("", 1, () => {});
        }

Public static void Time (string name, int iteration, Action action)
        {
If (String. IsNullOrEmpty (name) return;

// Warm up
Action ();

// 1.
Lelecolor currentForeColor = Console. ForegroundColor;
Console. ForegroundColor = ConsoleColor. Yellow;
Console. WriteLine (name );

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

// 3.
Stopwatch watch = new Stopwatch ();
Watch. Start ();
Ulong cycleCount = GetCycleCount ();
For (int I = 0; I <iteration; I ++) action ();
Ulong cpuCycles = GetCycleCount ()-cycleCount;
Watch. Stop ();

// 4.
Console. ForegroundColor = currentForeColor;
Console. WriteLine ("tTime Elapsed: t" + watch. ElapsedMilliseconds. ToString ("N0") + "ms ");
Console. WriteLine ("tCPU Cycles: t" + cpuCycles. ToString ("N0 "));

// 5.
For (int I = 0; I <= GC. MaxGeneration; I ++)
            {
Int count = GC. CollectionCount (I)-gcCounts [I];
Console. WriteLine ("tGen" + I + ": tt" + count );
            }

Console. WriteLine ();
        }

Private static ulong GetCycleCount ()
        {
Ulong cycleCount = 0;
If (isQueryThreadCycleTime)
            {
QueryThreadCycleTime (GetCurrentThread (), ref cycleCount );
            }
Else
            {
Ulong l;
Ulong kernelTime, userTimer;
GetThreadTimes (GetCurrentThread (), out l, out l, out kernelTime,
Out userTimer );
CycleCount = kernelTime + userTimer;
            }

Return cycleCount;
        }

[DllImport ("kernel32.dll")]
[Return: financialas (UnmanagedType. Bool)]
Static extern bool QueryThreadCycleTime (IntPtr threadHandle, ref ulong cycleTime );

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

[DllImport ("kernel32.dll", SetLastError = true)]
Static extern bool GetThreadTimes (IntPtr hThread, out ulong lpCreationTime,
Out ulong lpExitTime, out ulong lpKernelTime, out ulong lpUserTime );
    }

After reading their code, the old Zhao version is perfect in win7, and the eaglet version is ~~~ In his own continuation, he also said that there was a deviation in the actual running time of the GetThreadTimes obtaining thread. I did not perform many tests stably.

There is no way to improve it .. Windows 7 is in the nest, and the company's computer is hard-pressed xp .. That is to say, I can't share the same code in two places. There is no way to improve it.

It's disgusting to despise companies with framework2.0.

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.