CodeTimerPerformance EasyPerformanceCounterHelper .NET 4.5

Microshaoft發表於2013-11-10

//#define NET35
namespace TestConsoleApplication
{
    using System;
    using System.Diagnostics;
    using System.Threading;
    using System.Threading.Tasks;
    using Microshaoft;
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Begin ...");
            Random r = new Random();
            int sleep = 100;
            int iterations = 2000;
            int maxDegreeOfParallelism = 8; // Environment.ProcessorCount;
            var performanceCountersCategoryName = "Microshaoft EasyPerformanceCounters Category";
            var performanceCountersCategoryInstanceName = string.Format
                                                                    (
                                                                        "{2}{0}{3}{1}{4}"
                                                                        , ": "
                                                                        , " @ "
                                                                        , ""
                                                                        , ""
                                                                        , Process.GetCurrentProcess().ProcessName
                                                                    );
            //EasyPerformanceCountersHelper 呼叫示例
            //呼叫 EasyPerformanceCountersHelper.AttachPerformanceCountersCategoryInstance 可載入效能計數器
            EasyPerformanceCountersHelper.AttachPerformanceCountersCategoryInstance
                                (
                                    performanceCountersCategoryName
                                    , performanceCountersCategoryInstanceName + "-1"
                                );
            var enableCounters = MultiPerformanceCountersTypeFlags.ProcessCounter
                                    | MultiPerformanceCountersTypeFlags.ProcessedAverageTimerCounter
                                    | MultiPerformanceCountersTypeFlags.ProcessedCounter
                                    | MultiPerformanceCountersTypeFlags.ProcessedRateOfCountsPerSecondCounter
                                    | MultiPerformanceCountersTypeFlags.ProcessingCounter;
            //EasyPerformanceCountersHelper 可以直接使用 比如 用於 ASP.NET page_load 程式中程式碼中
            EasyPerformanceCountersHelper.CountPerformance
                                (
                                    enableCounters
                                    , performanceCountersCategoryName
                                    , performanceCountersCategoryInstanceName + "-1"
                                    , null
                                    , () =>
                                    {
                                        //需要效能計數器的程式碼段
                                        //begin ==============================================
                                        var x = r.Next(0, 10) * sleep;
                                        Thread.Sleep(x);
                                        //end ================================================
                                    }
                                    , null
                                );
            Parallel.For
                        (
                            0
                            , 99
                            , (x) =>
                            {
                                Stopwatch stopwatch =
                                    EasyPerformanceCountersHelper.CountPerformanceBegin
                                                (
                                                    enableCounters
                                                    , performanceCountersCategoryName
                                                    , performanceCountersCategoryInstanceName + "-1"
                                                );
                                //需要效能計數器的程式碼段
                                //begin ==============================================
                                var xx = r.Next(0, 10) * sleep;
                                Thread.Sleep(xx);
                                //end ================================================
                                if (stopwatch != null)
                                {
                                    EasyPerformanceCountersHelper.CountPerformanceEnd
                                                (
                                                    enableCounters
                                                    , performanceCountersCategoryName
                                                    , performanceCountersCategoryInstanceName + "-1"
                                                    , stopwatch
                                                );
                                    stopwatch = null;
                                }
                            }
                        );
            //CodeTimerPerformance 呼叫示例
            //CodeTimerPerformance.AttachPerformanceCountersCategoryInstance 可載入效能計數器
            CodeTimerPerformance.AttachPerformanceCountersCategoryInstance
                                (
                                    performanceCountersCategoryName
                                    , performanceCountersCategoryInstanceName + "-2"
                                );
            enableCounters =
                                MultiPerformanceCountersTypeFlags.ProcessCounter
                                | MultiPerformanceCountersTypeFlags.ProcessingCounter
                                | MultiPerformanceCountersTypeFlags.ProcessedCounter
                                | MultiPerformanceCountersTypeFlags.ProcessedAverageTimerCounter
                                | MultiPerformanceCountersTypeFlags.ProcessedRateOfCountsPerSecondCounter;
            //enableCounters = MultiPerformanceCountersTypeFlags.None;
            CodeTimerPerformance.ParallelTime
                        (
                            "ParallelTime1"
                            , iterations
                            , () =>
                            {
                                //需要效能計數器的程式碼段
                                //begin ==============================================
                                var x = r.Next(0, 10) * sleep;
                                Thread.Sleep(x);
                                //end ================================================
                            }
                            , maxDegreeOfParallelism
                            , enableCounters
                            , performanceCountersCategoryName
                            , performanceCountersCategoryInstanceName + "-1"
                        );
            CodeTimerPerformance.Time
                        (
                            "Time2"
                            , iterations
                            , () =>
                            {
                                //需要效能計數器的程式碼段
                                //begin ==============================================
                                var x = r.Next(0, 10) * sleep;
                                Thread.Sleep(x);
                                //end ================================================
                            }
                //, maxDegreeOfParallelism
                            , enableCounters
                            , performanceCountersCategoryName
                            , performanceCountersCategoryInstanceName + "-2"
                        );
            Console.WriteLine("End ...");
            Console.ReadLine();
        }
    }
}
namespace Microshaoft
{
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Threading.Tasks;
    public static class CodeTimerPerformance
    {
        public static void Initialize()
        {
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            Time("", 1, () => { }, MultiPerformanceCountersTypeFlags.None, string.Empty, string.Empty);
        }
        public static void AttachPerformanceCountersCategoryInstance
                    (
                        string performanceCountersCategoryName
                        , string performanceCountersCategoryInstanceName
                    )
        {
            EasyPerformanceCountersHelper.AttachPerformanceCountersCategoryInstance
                    (
                        performanceCountersCategoryName
                        , performanceCountersCategoryInstanceName
                    );
        }
        public static void ParallelTime
                                (
                                    string name
                                    , int iterations
                                    , Action actionOnce
                                    , int maxDegreeOfParallelism //= 1
                                    , MultiPerformanceCountersTypeFlags enablePerformanceCounters //= false
                                    , string performanceCountersCategoryName
                                    , string performanceCountersCategoryInstanceName
                                )
        {
            // 1.
            ConsoleColor 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);
            }
            IntPtr threadID = GetCurrentThreadId();
            Stopwatch watch = Stopwatch.StartNew();
            ulong cycleCount = GetCurrentThreadCycleCount();
            Parallel.For
                        (
                            0
                            , iterations
                            , new ParallelOptions()
                            {
                                MaxDegreeOfParallelism = maxDegreeOfParallelism
                                //, TaskScheduler = null
                            }
                            , (x) =>
                            {
                                EasyPerformanceCountersHelper.CountPerformance
                                                (
                                                    enablePerformanceCounters
                                                    , performanceCountersCategoryName
                                                    , performanceCountersCategoryInstanceName
                                                    , null
                                                    , actionOnce
                                                    , null
                                                );
                            }
                        );
            ulong cpuCycles = GetCurrentThreadCycleCount() - cycleCount;
            watch.Stop();
            //watch = null;
            // 4.
            Console.ForegroundColor = currentForeColor;
            Console.WriteLine
                            (
                                "{0}Time Elapsed:{0}{1}ms"
                                , "\t"
                                , watch.ElapsedMilliseconds.ToString("N0")
                            );
            Console.WriteLine
                            (
                                "{0}CPU Cycles:{0}{1}"
                                , "\t"
                                , cpuCycles.ToString("N0")
                            );
            // 5.
            for (int i = 0; i <= GC.MaxGeneration; i++)
            {
                int count = GC.CollectionCount(i) - gcCounts[i];
                Console.WriteLine
                            (
                                "{0}Gen{1}:{0}{0}{2}"
                                , "\t"
                                , i
                                , count
                            );
            }
            Console.WriteLine();
        }
        public static void Time
                            (
                                string name
                                , int iterations
                                , Action actionOnce
                                , MultiPerformanceCountersTypeFlags enablePerformanceCounters //= false
                                , string performanceCountersCategoryName
                                , string performanceCountersCategoryInstanceName
                            )
        {
            ParallelTime
                        (
                            name
                            , iterations
                            , actionOnce
                            , 1
                            , enablePerformanceCounters
                            , performanceCountersCategoryName
                            , performanceCountersCategoryInstanceName
                        );
        }
        private static ulong GetThreadCycleCount(IntPtr threadID)
        {
            ulong cycleCount = 0;
            QueryThreadCycleTime(threadID, ref cycleCount);
            return cycleCount;
        }
        private static ulong GetCurrentThreadCycleCount()
        {
            IntPtr threadID = GetCurrentThread();
            return GetThreadCycleCount(threadID);
        }
        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool QueryThreadCycleTime(IntPtr threadHandle, ref ulong cycleTime);
        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentThread();
        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentThreadId();
    }
}
namespace Microshaoft
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    //using System.Collections.Concurrent;
    public static class EasyPerformanceCountersHelper
    {
        private static Dictionary<string, CommonPerformanceCountersContainer> _dictionary = new Dictionary<string, CommonPerformanceCountersContainer>();
        public static void AttachPerformanceCountersCategoryInstance
                            (
                                string performanceCountersCategoryName
                                , string performanceCountersCategoryInstanceName
                            )
        {
            string key = string.Format
                                    (
                                        "{1}{0}{2}"
                                        , "-"
                                        , performanceCountersCategoryName
                                        , performanceCountersCategoryInstanceName
                                    );
            CommonPerformanceCountersContainer container = null;
            if (!_dictionary.TryGetValue(key, out container))
            {
                container = new CommonPerformanceCountersContainer();
                _dictionary.Add
                            (
                                key
                                , container //new CommonPerformanceCountersContainer()
                            );
                container.AttachPerformanceCountersToProperties(performanceCountersCategoryInstanceName, performanceCountersCategoryName);
            }
        }
        private static object _lockerObject = new object();
        public static Stopwatch CountPerformanceBegin
                                    (
                                        MultiPerformanceCountersTypeFlags enabledPerformanceCounters
                                        , string performanceCountersCategoryName
                                        , string performanceCountersCategoryInstanceName
                                    )
        {
            Stopwatch r = null;
            if (enabledPerformanceCounters != MultiPerformanceCountersTypeFlags.None)
            {
                string key = string.Format
                                        (
                                            "{1}{0}{2}"
                                            , "-"
                                            , performanceCountersCategoryName
                                            , performanceCountersCategoryInstanceName
                                        );
                CommonPerformanceCountersContainer container = null;
                if (!_dictionary.TryGetValue(key, out container))
                {
                    lock (_lockerObject)
                    {
                        container = new CommonPerformanceCountersContainer();
                        _dictionary.Add
                                    (
                                        key
                                        , new CommonPerformanceCountersContainer()
                                    );
                        container.AttachPerformanceCountersToProperties
                                            (
                                                performanceCountersCategoryInstanceName
                                                , performanceCountersCategoryName
                                            );
                    }
                }
                var enableProcessCounter =
                                            (
                                                (enabledPerformanceCounters & MultiPerformanceCountersTypeFlags.ProcessCounter)
                                                != MultiPerformanceCountersTypeFlags.None
                                            );
                if (enableProcessCounter)
                {
                    container.PrcocessPerformanceCounter.Increment();
                }
                var enableProcessingCounter =
                                            (
                                                (enabledPerformanceCounters & MultiPerformanceCountersTypeFlags.ProcessingCounter)
                                                != MultiPerformanceCountersTypeFlags.None
                                            );
                if (enableProcessingCounter)
                {
                    container.ProcessingPerformanceCounter.Increment();
                }
                var enableProcessedAverageTimerCounter =
                                            (
                                                (enabledPerformanceCounters & MultiPerformanceCountersTypeFlags.ProcessedAverageTimerCounter)
                                                != MultiPerformanceCountersTypeFlags.None
                                            );
                if (enableProcessedAverageTimerCounter)
                {
                    r = Stopwatch.StartNew();
                }
            }
            return r;
        }
        public static void CountPerformanceEnd
                                    (
                                        MultiPerformanceCountersTypeFlags enabledPerformanceCounters
                                        , string performanceCountersCategoryName
                                        , string performanceCountersCategoryInstanceName
                                        , Stopwatch stopwatch
                                    )
        {
            string key = string.Format
                        (
                            "{1}{0}{2}"
                            , "-"
                            , performanceCountersCategoryName
                            , performanceCountersCategoryInstanceName
                        );
            CommonPerformanceCountersContainer container = null;
            if (!_dictionary.TryGetValue(key, out container))
            {
                return;
            }
            var enableProcessedAverageTimerCounter =
                                        (
                                            (enabledPerformanceCounters & MultiPerformanceCountersTypeFlags.ProcessedAverageTimerCounter)
                                            != MultiPerformanceCountersTypeFlags.None
                                        );
            if (enableProcessedAverageTimerCounter)
            {
                if (stopwatch != null)
                {
                    PerformanceCounter performanceCounter = container.ProcessedAverageTimerPerformanceCounter;
                    PerformanceCounter basePerformanceCounter = container.ProcessedAverageBasePerformanceCounter;
                    stopwatch.Stop();
                    performanceCounter.IncrementBy(stopwatch.ElapsedTicks);
                    basePerformanceCounter.Increment();
                    //stopwatch = null;
                }
            }
            var enableProcessingCounter =
                                        (
                                            (enabledPerformanceCounters & MultiPerformanceCountersTypeFlags.ProcessingCounter)
                                            != MultiPerformanceCountersTypeFlags.None
                                        );
            if (enableProcessingCounter)
            {
                container.ProcessingPerformanceCounter.Decrement();
            }
            var enableProcessedPerformanceCounter =
                                    (
                                        (enabledPerformanceCounters & MultiPerformanceCountersTypeFlags.ProcessedCounter)
                                        != MultiPerformanceCountersTypeFlags.None
                                    );
            if (enableProcessedPerformanceCounter)
            {
                container.ProcessedPerformanceCounter.Increment();
            }
            var enableProcessedRateOfCountsPerSecondPerformanceCounter =
                        (
                            (enabledPerformanceCounters & MultiPerformanceCountersTypeFlags.ProcessedRateOfCountsPerSecondCounter)
                            != MultiPerformanceCountersTypeFlags.None
                        );
            if (enableProcessedRateOfCountsPerSecondPerformanceCounter)
            {
                container.ProcessedRateOfCountsPerSecondPerformanceCounter.Increment();
            }
        }
        public static void CountPerformance
                                    (
                                        MultiPerformanceCountersTypeFlags enabledPerformanceCounters
                                        , string performanceCountersCategoryName
                                        , string performanceCountersCategoryInstanceName
                                        , Action onBeforeCountPerformanceInnerProcessAction
                                        , Action onCountPerformanceInnerProcessAction
                                        , Action onAfterCountPerformanceInnerProcessAction
                                    )
        {
            if (enabledPerformanceCounters != MultiPerformanceCountersTypeFlags.None)
            {
                if (onCountPerformanceInnerProcessAction != null)
                {
                    string key = string.Format
                                            (
                                                "{1}{0}{2}"
                                                , "-"
                                                , performanceCountersCategoryName
                                                , performanceCountersCategoryInstanceName
                                            );
                    CommonPerformanceCountersContainer container = null;
                    if (!_dictionary.TryGetValue(key, out container))
                    {
                        lock (_lockerObject)
                        {
                            container = new CommonPerformanceCountersContainer();
                            _dictionary.Add
                                        (
                                            key
                                            , new CommonPerformanceCountersContainer()
                                        );
                            container.AttachPerformanceCountersToProperties
                                                (
                                                    performanceCountersCategoryInstanceName
                                                    , performanceCountersCategoryName
                                                );
                        }
                    }
                    var enableProcessCounter =
                                                (
                                                    (enabledPerformanceCounters & MultiPerformanceCountersTypeFlags.ProcessCounter)
                                                    != MultiPerformanceCountersTypeFlags.None
                                                );
                    if (enableProcessCounter)
                    {
                        container.PrcocessPerformanceCounter.Increment();
                    }
                    var enableProcessingCounter =
                                                (
                                                    (enabledPerformanceCounters & MultiPerformanceCountersTypeFlags.ProcessingCounter)
                                                    != MultiPerformanceCountersTypeFlags.None
                                                );
                    if (enableProcessingCounter)
                    {
                        container.ProcessingPerformanceCounter.Increment();
                    }
                    var enableProcessedAverageTimerCounter =
                                                (
                                                    (enabledPerformanceCounters & MultiPerformanceCountersTypeFlags.ProcessedAverageTimerCounter)
                                                    != MultiPerformanceCountersTypeFlags.None
                                                );
                    try
                    {
                        container.ProcessedAverageTimerPerformanceCounter.ChangeAverageTimerCounterValueWithTryCatchExceptionFinally
                                                                (
                                                                    enableProcessedAverageTimerCounter
                                                                    , container.ProcessedAverageBasePerformanceCounter
                                                                    , () =>
                                                                    {
                                                                        if (onCountPerformanceInnerProcessAction != null)
                                                                        {
                                                                            if (onBeforeCountPerformanceInnerProcessAction != null)
                                                                            {
                                                                                onBeforeCountPerformanceInnerProcessAction();
                                                                            }
                                                                            onCountPerformanceInnerProcessAction();
                                                                            if (onAfterCountPerformanceInnerProcessAction != null)
                                                                            {
                                                                                onAfterCountPerformanceInnerProcessAction();
                                                                            }
                                                                        }
                                                                    }
                                                                    , null
                                                                    , null
                                                                );
                    }
                    catch (Exception e)
                    {
                        throw new Exception("ReThrow Exception On Caught Excepion", e);
                    }
                    finally
                    {
                        if (enableProcessingCounter)
                        {
                            container.ProcessingPerformanceCounter.Decrement();
                        }
                        var enableProcessedPerformanceCounter =
                                                (
                                                    (enabledPerformanceCounters & MultiPerformanceCountersTypeFlags.ProcessedCounter)
                                                    != MultiPerformanceCountersTypeFlags.None
                                                );
                        if (enableProcessedPerformanceCounter)
                        {
                            container.ProcessedPerformanceCounter.Increment();
                        }
                        var enableProcessedRateOfCountsPerSecondPerformanceCounter =
                                                (
                                                    (enabledPerformanceCounters & MultiPerformanceCountersTypeFlags.ProcessedRateOfCountsPerSecondCounter)
                                                    != MultiPerformanceCountersTypeFlags.None
                                                );
                        if (enableProcessedRateOfCountsPerSecondPerformanceCounter)
                        {
                            container.ProcessedRateOfCountsPerSecondPerformanceCounter.Increment();
                        }
                    }
                }
            }
            else
            {
                if (onCountPerformanceInnerProcessAction != null)
                {
                    onCountPerformanceInnerProcessAction();
                }
            }
        }
    }
}
//=========================================================================================
//=========================================================================================
namespace Microshaoft
{
    using System;
    using System.Diagnostics;
    public class CommonPerformanceCountersContainer
    {
        #region PerformanceCounters
        private PerformanceCounter _processPerformanceCounter;
        [PerformanceCounterDefinitionAttribute(CounterType = PerformanceCounterType.NumberOfItems64, CounterName = "01.接收處理筆數(筆)")]
        public PerformanceCounter PrcocessPerformanceCounter
        {
            private set
            {
                ReaderWriterLockSlimHelper.TryEnterWriterLockSlimWrite<PerformanceCounter>(ref _processPerformanceCounter, value, 2);
            }
            get
            {
                return _processPerformanceCounter;
            }
        }
        private PerformanceCounter _processingPerformanceCounter;
        [PerformanceCounterDefinitionAttribute(CounterType = PerformanceCounterType.NumberOfItems64, CounterName = "02.正在處理筆數(筆)")]
        public PerformanceCounter ProcessingPerformanceCounter
        {
            private set
            {
                ReaderWriterLockSlimHelper.TryEnterWriterLockSlimWrite<PerformanceCounter>(ref _processingPerformanceCounter, value, 2);
            }
            get
            {
                return _processingPerformanceCounter;
            }
        }
        private PerformanceCounter _processedPerformanceCounter;
        [PerformanceCounterDefinitionAttribute(CounterType = PerformanceCounterType.NumberOfItems64, CounterName = "03.完成處理筆數(筆)")]
        public PerformanceCounter ProcessedPerformanceCounter
        {
            private set
            {
                ReaderWriterLockSlimHelper.TryEnterWriterLockSlimWrite<PerformanceCounter>(ref _processedPerformanceCounter, value, 2);
            }
            get
            {
                return _processedPerformanceCounter;
            }
        }
        private PerformanceCounter _processedRateOfCountsPerSecondPerformanceCounter;
        [PerformanceCounterDefinitionAttribute(CounterType = PerformanceCounterType.RateOfCountsPerSecond64, CounterName = "04.每秒完成處理筆數(筆/秒)")]
        public PerformanceCounter ProcessedRateOfCountsPerSecondPerformanceCounter
        {
            private set
            {
                ReaderWriterLockSlimHelper.TryEnterWriterLockSlimWrite<PerformanceCounter>(ref _processedRateOfCountsPerSecondPerformanceCounter, value, 2);
            }
            get
            {
                return _processedRateOfCountsPerSecondPerformanceCounter;
            }
        }
        private PerformanceCounter _ProcessedAverageTimerPerformanceCounter;
        [PerformanceCounterDefinitionAttribute(CounterType = PerformanceCounterType.AverageTimer32, CounterName = "05.平均每筆處理耗時秒數(秒/筆)")]
        public PerformanceCounter ProcessedAverageTimerPerformanceCounter
        {
            private set
            {
                ReaderWriterLockSlimHelper.TryEnterWriterLockSlimWrite<PerformanceCounter>(ref _ProcessedAverageTimerPerformanceCounter, value, 2);
            }
            get
            {
                return _ProcessedAverageTimerPerformanceCounter;
            }
        }
        private PerformanceCounter _processedAverageBasePerformanceCounter;
        [PerformanceCounterDefinitionAttribute(CounterType = PerformanceCounterType.AverageBase)]
        public PerformanceCounter ProcessedAverageBasePerformanceCounter
        {
            private set
            {
                ReaderWriterLockSlimHelper.TryEnterWriterLockSlimWrite<PerformanceCounter>(ref _processedAverageBasePerformanceCounter, value, 2);
            }
            get
            {
                return _processedAverageBasePerformanceCounter;
            }
        }
        #endregion
        // indexer declaration
        public PerformanceCounter this[string name]
        {
            get
            {
                throw new NotImplementedException();
                //return null;
            }
        }
        //private bool _isAttachedPerformanceCounters = false;
        public void AttachPerformanceCountersToProperties
                            (
                                string instanceName
                                , string categoryName
                            )
        {
            var type = this.GetType();
            PerformanceCountersHelper.AttachPerformanceCountersToProperties<CommonPerformanceCountersContainer>(instanceName, categoryName, this);
        }
    }
}
namespace Microshaoft
{
    using System;
    using System.Diagnostics;
    public static class PerformanceCounterExtensionMethodsManager
    {
        public static T ChangeCounterValueWithTryCatchExceptionFinally<T>
                                (
                                    this PerformanceCounter performanceCounter
                                    , bool enabled
                                    , Func<PerformanceCounter, T> onCounterChangeProcessFunc //= null
                                    , Action<PerformanceCounter> onCounterChangedProcessAction //= null
                                    , Func<PerformanceCounter, Exception, bool> onCaughtExceptionProcessFunc //= null
                                    , Action<PerformanceCounter> onCaughtExceptionFinallyProcessAction //= null
                                )
        {
            T r = default(T);
            if (enabled)
            {
                if (onCounterChangeProcessFunc != null)
                {
                    var caughtException = false;
                    try
                    {
                        r = onCounterChangeProcessFunc(performanceCounter);
                    }
                    catch (Exception e)
                    {
                        caughtException = true;
                        var reThrow = true;
                        if (onCaughtExceptionProcessFunc != null)
                        {
                            reThrow = onCaughtExceptionProcessFunc(performanceCounter, e);
                        }
                        if (reThrow)
                        {
                            throw new Exception("ReThrow Exception On Caught Excepion", e);
                        }
                    }
                    finally
                    {
                        if (caughtException)
                        {
                            if (onCaughtExceptionFinallyProcessAction != null)
                            {
                                onCaughtExceptionFinallyProcessAction(performanceCounter);
                            }
                        }
                    }
                }
            }
            if (onCounterChangedProcessAction != null)
            {
                var caughtException = false;
                try
                {
                    onCounterChangedProcessAction(performanceCounter);
                }
                catch (Exception e)
                {
                    caughtException = true;
                    var reThrow = true;
                    if (onCaughtExceptionProcessFunc != null)
                    {
                        reThrow = onCaughtExceptionProcessFunc(performanceCounter, e);
                    }
                    if (reThrow)
                    {
                        throw new Exception("ReThrow Exception On Caught Excepion", e);
                    }
                }
                finally
                {
                    if (caughtException)
                    {
                        if (onCaughtExceptionFinallyProcessAction != null)
                        {
                            onCaughtExceptionFinallyProcessAction(performanceCounter);
                        }
                    }
                }
            }
            return r;
        }
        public static void ChangeAverageTimerCounterValueWithTryCatchExceptionFinally
                                (
                                    this PerformanceCounter performanceCounter
                                    , bool enabled
                                    , PerformanceCounter basePerformanceCounter
                                    , Action onCountPerformanceInnerProcessAction //= null
                                    , Func<PerformanceCounter, Exception, bool> onCaughtExceptionProcessFunc //= null
                                    , Action<PerformanceCounter, PerformanceCounter> onCaughtExceptionFinallyProcessAction //= null
                                )
        {
            if (enabled)
            {
                var stopwatch = Stopwatch.StartNew();
                if (onCountPerformanceInnerProcessAction != null)
                {
                    var caughtException = false;
                    try
                    {
                        onCountPerformanceInnerProcessAction();
                    }
                    catch (Exception e)
                    {
                        caughtException = true;
                        var reThrow = true;
                        if (onCaughtExceptionProcessFunc != null)
                        {
                            reThrow = onCaughtExceptionProcessFunc(performanceCounter, e);
                        }
                        if (reThrow)
                        {
                            throw new Exception("ReThrow Exception On Caught Excepion", e);
                        }
                    }
                    finally
                    {
                        stopwatch.Stop();
                        performanceCounter.IncrementBy(stopwatch.ElapsedTicks);
                        stopwatch = null;
                        basePerformanceCounter.Increment();
                        if (caughtException)
                        {
                            if (onCaughtExceptionFinallyProcessAction != null)
                            {
                                onCaughtExceptionFinallyProcessAction(performanceCounter, basePerformanceCounter);
                            }
                        }
                    }
                }
            }
        }
    }
}
namespace Microshaoft
{
    using System;
    using System.Diagnostics;
    [FlagsAttribute]
    public enum MultiPerformanceCountersTypeFlags : ushort
    {
        None = 0,
        ProcessCounter = 1,
        ProcessingCounter = 2,
        ProcessedCounter = 4,
        ProcessedAverageTimerCounter = 8,
        ProcessedRateOfCountsPerSecondCounter = 16
    };
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
    public class PerformanceCounterDefinitionAttribute : Attribute
    {
        public PerformanceCounterType CounterType;
        public string CounterName;
    }
}
namespace Microshaoft
{
    using System.Diagnostics;
    using System.Linq;
    public static class PerformanceCountersHelper
    {
        public static void AttachPerformanceCountersToProperties<T>
                                    (
                                        string performanceCounterInstanceName
                                        , string category
                                        , T target //= default(T)
                                    )
        {
            var type = typeof(T);
            var propertiesList = type.GetProperties().ToList();
            propertiesList = propertiesList.Where
                                                (
                                                    (pi) =>
                                                    {
                                                        var parameters = pi.GetIndexParameters();
                                                        return
                                                            (
                                                                pi.PropertyType == typeof(PerformanceCounter)
                                                                && (parameters == null ? 0 : parameters.Length) <= 0
                                                            );
                                                    }
                                                ).ToList();
            if (PerformanceCounterCategory.Exists(category))
            {
                propertiesList.ForEach
                                    (
                                        (pi) =>
                                        {
                                            if (PerformanceCounterCategory.CounterExists(pi.Name, category))
                                            {
                                                if (PerformanceCounterCategory.InstanceExists(performanceCounterInstanceName, category))
                                                {
                                                    //var pc = new PerformanceCounter(category, pi.Name, instanceName, false);
                                                    //pc.InstanceName = instanceName;
                                                    //pc.RemoveInstance();
                                                }
                                            }
                                        }
                                    );
                //PerformanceCounterCategory.Delete(category);
            }
            if (!PerformanceCounterCategory.Exists(category))
            {
                var ccdc = new CounterCreationDataCollection();
                propertiesList.ForEach
                                (
                                    (pi) =>
                                    {
                                        var propertyName = pi.Name;
                                        var performanceCounterType = PerformanceCounterType.NumberOfItems64;
                                        var performanceCounterName = propertyName;
                                        var attribute = pi.GetCustomAttributes(false).FirstOrDefault
                                                                                    (
                                                                                        (x) =>
                                                                                        {
                                                                                            return x as PerformanceCounterDefinitionAttribute != null;
                                                                                        }
                                                                                    ) as PerformanceCounterDefinitionAttribute;
                                        if (attribute != null)
                                        {
                                            var counterName = attribute.CounterName;
                                            if (!string.IsNullOrEmpty(counterName))
                                            {
                                                performanceCounterName = counterName;
                                            }
                                            var counterType = attribute.CounterType;
                                            //if (counterType != null)
                                            {
                                                performanceCounterType = counterType;
                                            }
                                        }
                                        var ccd = PerformanceCountersHelper.GetCounterCreationData
                                                                                (
                                                                                    performanceCounterName
                                                                                    , performanceCounterType
                                                                                );
                                        ccdc.Add(ccd);
                                    }
                                );
                PerformanceCounterCategory.Create
                                (
                                    category,
                                    string.Format("{0} Category Help.", category),
                                    PerformanceCounterCategoryType.MultiInstance,
                                    ccdc
                                );
            }
            propertiesList.ForEach
                            (
                                (pi) =>
                                {
                                    var propertyName = pi.Name;
                                    var performanceCounterType = PerformanceCounterType.NumberOfItems64;
                                    var performanceCounterName = propertyName;
                                    var attribute = pi.GetCustomAttributes(false).FirstOrDefault
                                                                                (
                                                                                    (x) =>
                                                                                    {
                                                                                        return x as PerformanceCounterDefinitionAttribute != null;
                                                                                    }
                                                                                ) as PerformanceCounterDefinitionAttribute;
                                    if (attribute != null)
                                    {
                                        var counterName = attribute.CounterName;
                                        if (!string.IsNullOrEmpty(counterName))
                                        {
                                            performanceCounterName = counterName;
                                        }
                                        var counterType = attribute.CounterType;
                                        //if (counterType != null)
                                        {
                                            performanceCounterType = counterType;
                                        }
                                    }
                                    var pc = new PerformanceCounter()
                                    {
                                        CategoryName = category
                                        ,
                                        CounterName = performanceCounterName
                                        ,
                                        InstanceLifetime = PerformanceCounterInstanceLifetime.Process
                                        ,
                                        InstanceName = performanceCounterInstanceName
                                        ,
                                        ReadOnly = false
                                        ,
                                        RawValue = 0
                                    };
                                    if (pi.GetGetMethod().IsStatic)
                                    {
                                        var setter = DynamicPropertyAccessor.CreateSetStaticPropertyValueAction<PerformanceCounter>(type, propertyName);
                                        setter(pc);
                                    }
                                    else
                                    {
                                        if (target != null)
                                        {
                                            var setter = DynamicPropertyAccessor.CreateSetPropertyValueAction<PerformanceCounter>(type, propertyName);
                                            setter(target, pc);
                                        }
                                    }
                                }
                            );
        }
        public static CounterCreationData GetCounterCreationData(string counterName, PerformanceCounterType performanceCounterType)
        {
            return new CounterCreationData()
            {
                CounterName = counterName
                ,
                CounterHelp = string.Format("{0} Help", counterName)
                ,
                CounterType = performanceCounterType
            };
        }
    }
}
namespace Microshaoft
{
    using System;
    using System.Threading;
    public static class ReaderWriterLockSlimHelper
    {
        public static bool TryEnterWriterLockSlimWrite<T>
                                                (
                                                     ref T target
                                                    , T newValue
                                                    , int enterTimeOutSeconds
                                                )
                                                    where T : class
        {
            bool r = false;
            var rwls = new ReaderWriterLockSlim();
            int timeOut = Timeout.Infinite;
            if (enterTimeOutSeconds >= 0)
            {
                timeOut = enterTimeOutSeconds * 1000;
            }
            try
            {
                r = (rwls.TryEnterWriteLock(timeOut));
                if (r)
                {
                    Interlocked.Exchange<T>(ref target, newValue);
                    r = true;
                }
            }
            finally
            {
                if (r)
                {
                    rwls.ExitWriteLock();
                }
            }
            return r;
        }
        public static bool TryEnterWriterLockSlim
                                (
                                    Action action
                                    , int enterTimeOutSeconds
                                )
        {
            bool r = false;
            if (action != null)
            {
                var rwls = new ReaderWriterLockSlim();
                int timeOut = Timeout.Infinite;
                if (enterTimeOutSeconds >= 0)
                {
                    timeOut = enterTimeOutSeconds * 1000;
                }
                try
                {
                    r = (rwls.TryEnterWriteLock(timeOut));
                    if (r)
                    {
                        action();
                        r = true;
                    }
                }
                finally
                {
                    if (r)
                    {
                        rwls.ExitWriteLock();
                    }
                }
            }
            return r;
        }
    }
}
namespace Microshaoft
{
    using System;
    using System.Linq;
    using System.Linq.Expressions;
    using System.Reflection;
    public class DynamicPropertyAccessor
    {
        private static Assembly GetAssemblyByTypeName(string typeName)
        {
            return
                AppDomain
                    .CurrentDomain
                        .GetAssemblies()
                            .First
                                (
                                    (a) =>
                                    {
                                        return
                                            a
                                                .GetTypes()
                                                    .Any
                                                        (
                                                            (t) =>
                                                            {
                                                                return
                                                                    (
                                                                        t.FullName
                                                                        == typeName
                                                                    );
                                                            }
                                                        );
                                    }
                                );
        }
        public static Func<object, object> CreateGetPropertyValueFunc
                        (
                            string typeName
                            , string propertyName
                            , bool isTypeFromAssembly = false
                        )
        {
            Type type;
            if (isTypeFromAssembly)
            {
                var assembly = GetAssemblyByTypeName(typeName);
                type = assembly.GetType(typeName);
            }
            else
            {
                type = Type.GetType(typeName);
            }
            return CreateGetPropertyValueFunc(type, propertyName);
        }
        public static Func<object, object> CreateGetPropertyValueFunc
                        (
                            Type type
                            , string propertyName
                        )
        {
            var target = Expression.Parameter(typeof(object));
            var castTarget = Expression.Convert(target, type);
            var getPropertyValue = Expression.Property(castTarget, propertyName);
            var castPropertyValue = Expression.Convert(getPropertyValue, typeof(object));
            var lambda = Expression.Lambda<Func<object, object>>(castPropertyValue, target);
            return lambda.Compile();
        }
        public static Func<object, TProperty> CreateGetPropertyValueFunc<TProperty>
                        (
                            string typeName
                            , string propertyName
                            , bool isTypeFromAssembly = false
                        )
        {
            Type type;
            if (isTypeFromAssembly)
            {
                var assembly = GetAssemblyByTypeName(typeName);
                type = assembly.GetType(typeName);
            }
            else
            {
                type = Type.GetType(typeName);
            }
            return CreateGetPropertyValueFunc<TProperty>(type, propertyName);
        }
        public static Func<object, TProperty> CreateGetPropertyValueFunc<TProperty>
                        (
                            Type type
                            , string propertyName
                        )
        {
            var target = Expression.Parameter(typeof(object));
            var castTarget = Expression.Convert(target, type);
            var getPropertyValue = Expression.Property(castTarget, propertyName);
            var lambda = Expression.Lambda<Func<object, TProperty>>(getPropertyValue, target);
            return lambda.Compile();
        }
        public static Func<TProperty> CreateGetStaticPropertyValueFunc<TProperty>
                        (
                            string typeName
                            , string propertyName
                            , bool isTypeFromAssembly = false
                        )
        {
            Type type;
            if (isTypeFromAssembly)
            {
                var assembly = GetAssemblyByTypeName(typeName);
                type = assembly.GetType(typeName);
            }
            else
            {
                type = Type.GetType(typeName);
            }
            return CreateGetStaticPropertyValueFunc<TProperty>(type, propertyName);
        }
        public static Func<TProperty> CreateGetStaticPropertyValueFunc<TProperty>
                        (
                            Type type
                            , string propertyName
                        )
        {
            Func<TProperty> func = null;
            var property = type.GetProperty(propertyName, typeof(TProperty));
            if (property == null)
            {
                property =
                            type
                                .GetProperties()
                                    .ToList()
                                        .FirstOrDefault
                                            (
                                                (x) =>
                                                {
                                                    return
                                                        (
                                                            x.Name.ToLower()
                                                            == propertyName.ToLower()
                                                        );
                                                }
                                            );
            }
            if (property != null)
            {
                var getPropertyValue = Expression.Property(null, property);
                var lambda = Expression.Lambda<Func<TProperty>>(getPropertyValue, null);
                func = lambda.Compile();
            }
            return func;
        }
        public static Func<object> CreateGetStaticPropertyValueFunc
                        (
                            Type type
                            , string propertyName
                        )
        {
            Func<object> func = null;
            var property = type.GetProperty(propertyName);
            if (property == null)
            {
                property =
                            type
                                .GetProperties()
                                    .ToList()
                                        .FirstOrDefault
                                            (
                                                (x) =>
                                                {
                                                    return
                                                        (
                                                            x.Name.ToLower()
                                                            == propertyName.ToLower()
                                                        );
                                                }
                                            );
            }
            if (property != null)
            {
                var getPropertyValue = Expression.Property(null, property);
                var castPropertyValue = Expression.Convert(getPropertyValue, typeof(object));
                var lambda = Expression.Lambda<Func<object>>(castPropertyValue, null);
                func = lambda.Compile();
            }
            return func;
        }
        public static Func<object> CreateGetStaticPropertyValueFunc
                        (
                            string typeName
                            , string propertyName
                            , bool isTypeFromAssembly = false
                        )
        {
            Type type;
            if (isTypeFromAssembly)
            {
                var assembly = GetAssemblyByTypeName(typeName);
                type = assembly.GetType(typeName);
            }
            else
            {
                type = Type.GetType(typeName);
            }
            return CreateGetStaticPropertyValueFunc(type, propertyName);
        }
        public static Action<object, object> CreateSetPropertyValueAction
                        (
                            Type type
                            , string propertyName
                        )
        {
            Action<object, object> action = null;
            var property = type.GetProperty(propertyName);
            if (property == null)
            {
                property =
                            type
                                .GetProperties()
                                    .ToList()
                                        .FirstOrDefault
                                            (
                                                (x) =>
                                                {
                                                    return
                                                        (
                                                            x.Name.ToLower()
                                                            == propertyName.ToLower()
                                                        );
                                                }
                                            );
            }
            if (property != null)
            {
                var target = Expression.Parameter(typeof(object));
                var propertyValue = Expression.Parameter(typeof(object));
                var castTarget = Expression.Convert(target, type);
                var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType);
                var getSetMethod = property.GetSetMethod();
                if (getSetMethod == null)
                {
                    getSetMethod = property.GetSetMethod(true);
                }
                var call = Expression.Call(castTarget, getSetMethod, castPropertyValue);
                var lambda = Expression.Lambda<Action<object, object>>(call, target, propertyValue);
                action = lambda.Compile();
            }
            return action;
        }
        public static Action<object, object> CreateSetPropertyValueAction
                        (
                            string typeName
                            , string propertyName
                            , bool isTypeFromAssembly = false
                        )
        {
            Type type;
            if (isTypeFromAssembly)
            {
                var assembly = GetAssemblyByTypeName(typeName);
                type = assembly.GetType(typeName);
            }
            else
            {
                type = Type.GetType(typeName);
            }
            return CreateSetPropertyValueAction(type, propertyName);
        }
        public static Action<object, TProperty> CreateSetPropertyValueAction<TProperty>
                        (
                            Type type
                            , string propertyName
                        )
        {
            Action<object, TProperty> action = null;
            var property = type.GetProperty(propertyName);
            if (property == null)
            {
                property =
                            type
                                .GetProperties()
                                    .ToList()
                                        .FirstOrDefault
                                            (
                                                (x) =>
                                                {
                                                    return
                                                        (
                                                            x.Name.ToLower()
                                                            == propertyName.ToLower()
                                                        );
                                                }
                                            );
            }
            if (property != null)
            {
                var target = Expression.Parameter(typeof(object));
                var propertyValue = Expression.Parameter(typeof(TProperty));
                var castTarget = Expression.Convert(target, type);
                var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType);
                var getSetMethod = property.GetSetMethod();
                if (getSetMethod == null)
                {
                    getSetMethod = property.GetSetMethod(true);
                }
                var call = Expression.Call(castTarget, getSetMethod, castPropertyValue);
                var lambda = Expression.Lambda<Action<object, TProperty>>(call, target, propertyValue);
                action = lambda.Compile();
            }
            return action; 
        }
        public static Action<object, TProperty> CreateSetPropertyValueAction<TProperty>
                        (
                            string typeName
                            , string propertyName
                            , bool isTypeFromAssembly = false
                        )
        {
            Type type;
            if (isTypeFromAssembly)
            {
                var assembly = GetAssemblyByTypeName(typeName);
                type = assembly.GetType(typeName);
            }
            else
            {
                type = Type.GetType(typeName);
            }
            return CreateSetPropertyValueAction<TProperty>(type, propertyName);
        }
        public static Action<object> CreateSetStaticPropertyValueAction
                        (
                            Type type
                            , string propertyName
                        )
        {
            Action<object> action = null;
            var property = type.GetProperty(propertyName);
            if (property == null)
            {
                property =
                            type
                                .GetProperties()
                                    .ToList()
                                        .FirstOrDefault
                                            (
                                                (x) =>
                                                {
                                                    return
                                                        (
                                                            x.Name.ToLower()
                                                            == propertyName.ToLower()
                                                        );
                                                }
                                            );
            }
            if (property != null)
            {
                var propertyValue = Expression.Parameter(typeof(object));
                var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType);
                var getSetMethod = property.GetSetMethod();
                if (getSetMethod == null)
                {
                    getSetMethod = property.GetSetMethod(true);
                }
                var call = Expression.Call(null, getSetMethod, castPropertyValue);
                var lambda = Expression.Lambda<Action<object>>(call, propertyValue);
                action = lambda.Compile();
            }
            return action;
        }
        public static Action<object> CreateSetStaticPropertyValueAction
                        (
                            string typeName
                            , string propertyName
                            , bool isTypeFromAssembly = false
                        )
        {
            Type type;
            if (isTypeFromAssembly)
            {
                var assembly = GetAssemblyByTypeName(typeName);
                type = assembly.GetType(typeName);
            }
            else
            {
                type = Type.GetType(typeName);
            }
            return CreateSetStaticPropertyValueAction(type, propertyName);
        }
        public static Action<TProperty> CreateSetStaticPropertyValueAction<TProperty>
                        (
                            Type type
                            , string propertyName
                        )
        {
            Action<TProperty> action = null;
            var property = type.GetProperty(propertyName);
            if (property == null)
            {
                property =
                            type
                                .GetProperties()
                                    .ToList()
                                        .FirstOrDefault
                                            (
                                                (x) =>
                                                {
                                                    return
                                                        (
                                                            x.Name.ToLower()
                                                            == propertyName.ToLower()
                                                        );
                                                }
                                            );
            }
            if (property != null)
            {
                var propertyValue = Expression.Parameter(typeof(TProperty));
                //var castPropertyValue = Expression.Convert(propertyValue, property.PropertyType);
                var getSetMethod = property.GetSetMethod();
                if (getSetMethod == null)
                {
                    getSetMethod = property.GetSetMethod(true);
                }
                var call = Expression.Call(null, getSetMethod, propertyValue);
                var lambda = Expression.Lambda<Action<TProperty>>(call, propertyValue);
                action = lambda.Compile();
            }
            return action;
        }
        public static Action<TProperty> CreateSetStaticPropertyValueAction<TProperty>
                        (
                            string typeName
                            , string propertyName
                            , bool isTypeFromAssembly = false
                        )
        {
            Type type;
            if (isTypeFromAssembly)
            {
                var assembly = GetAssemblyByTypeName(typeName);
                type = assembly.GetType(typeName);
            }
            else
            {
                type = Type.GetType(typeName);
            }
            return CreateSetStaticPropertyValueAction<TProperty>(type, propertyName);
        }
    }
}
//以下 only for .NET 3.5 =============================
#if NET35
namespace System.Threading.Tasks
{
    using System;
    using System.Threading;
    public class ParallelOptions
    {
        public int MaxDegreeOfParallelism
        {
            set;
            get;
        }
    }
    public static class Parallel
    {
        public static void For
                        (
                            int from
                            , int to
                            , ParallelOptions options
                            , Action<int> action
                        )
        {
            var iterations = to - from;
            var threads = options.MaxDegreeOfParallelism;
            var ares = new AutoResetEvent[threads];
            var i = 0;
            var count = 0;
            Array.ForEach
                    (
                        ares
                        , (x) =>
                        {
                            var are = new AutoResetEvent(false);
                            ares[i++] = are;
                            new Thread
                                (
                                    new ThreadStart
                                            (
                                                () =>
                                                {
                                                    while (true)
                                                    {
                                                        int j = Interlocked.Increment(ref from);
                                                        if (j <= to)
                                                        {
                                                            Interlocked.Increment(ref count);
                                                            action(j);
                                                        }
                                                        else
                                                        {
                                                            break;
                                                        }
                                                    }
                                                    are.Set();
                                                }
                                            )
                                ).Start();
                        }
                    );
            WaitHandle.WaitAll(ares);
            Console.WriteLine(count);
        }
    }
}
#endif

相關文章