asp.net中CodeTimer .net下的效能計數器執行個體

來源:互聯網
上載者:User

因為這個是winform的,所以我稍微改了下,改成WEB輸出了,執行結果如下:

完整代碼如下:

 代碼如下 複製代碼

 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
            //ConsoleColor 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("全部耗時:tt" +watch.ElapsedMilliseconds.ToString("N0") + "ms<br />");
           HttpContext.Current.Response.Write("執行一次耗時:" +(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 /><br />");

        }

        public static void Time(string name, int iteration, IAction action)
        {
            if (String.IsNullOrEmpty(name))
            {
                return;
            }

            if (action == null)
            {
                return;
            }

            //1. Print name
           // ConsoleColor 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 />");
            }

       

        }
    }
}

使用如下(GetUser和GetUserBySql是兩個方法):

 代碼如下 複製代碼

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.