private void CountPerformanceEnd
 (
     HttpActionExecutedContext actionExecutedContext
     , string stopwatchKey
     , string performanceCountersCategoryNameKey
     , string performanceCountersInstanceNameKey
 )
 {
     if (actionExecutedContext.Request.Properties.ContainsKey(stopwatchKey))
     {
         var stopwatch = actionExecutedContext
                         .Request
                         .Properties[stopwatchKey] as Stopwatch;
         if (stopwatch != null)
         {
             var enableCountPerformance = false;
             if (OnCheckEnabledCountPerformanceProcessFunc != null)
             {
                 enableCountPerformance = OnCheckEnabledCountPerformanceProcessFunc();
             }
             if (enableCountPerformance)
             {
                 var performanceCountersCategoryName
                     = actionExecutedContext
                       .Request
                       .Properties[performanceCountersCategoryNameKey] as string;
                 var performanceCountersInstanceName = actionExecutedContext
                                                       .Request
                                                       .Properties[performanceCountersInstanceNameKey] as string;
                 if
                 (
                     !string.IsNullOrEmpty(performanceCountersCategoryName)
                     &&
                     !string.IsNullOrEmpty(performanceCountersInstanceName)
                 )
                 {
                     EasyPerformanceCountersHelper <CommonPerformanceCountersContainer>
                     .CountPerformanceEnd
                     (
                         EnabledCounters
                         , performanceCountersCategoryName
                         , performanceCountersInstanceName
                         , stopwatch
                     );
                 }
             }
             if (stopwatch != null)
             {
                 stopwatch = null;
             }
         }
     }
 }
コード例 #2
0
 public static void AttachPerformanceCountersCategoryInstance
 (
     string performanceCountersCategoryName
     , string performanceCountersCategoryInstanceName
 )
 {
     EasyPerformanceCountersHelper <SessionsPerformanceCountersContainer> .AttachPerformanceCountersCategoryInstance
     (
         performanceCountersCategoryName
         , performanceCountersCategoryInstanceName
     );
 }
コード例 #3
0
        public void AttachPerformanceCountersCategoryInstance
        (
            string performanceCountersCategoryName
            , string performanceCountersCategoryInstanceNamePrefix
            , MultiPerformanceCountersTypeFlags enablePerformanceCounters           = MultiPerformanceCountersTypeFlags.ProcessNonTimeBasedCounters
            , PerformanceCounterInstanceLifetime performanceCounterInstanceLifetime = PerformanceCounterInstanceLifetime.Process
        )
        {
            if (!_isAttachPerformanceCounters)
            {
                _isAttachPerformanceCounters = true;
            }
            else
            {
                CommonPerformanceCountersContainer container = null;
                EasyPerformanceCountersHelper <CommonPerformanceCountersContainer>
                .AttachPerformanceCountersCategoryInstance
                (
                    performanceCountersCategoryName
                    , string.Format
                    (
                        "{1}{0}{2}"
                        , "-"
                        , "Non-Pooled Objects"
                        , performanceCountersCategoryInstanceNamePrefix
                    )
                    , out container
                    , PerformanceCounterInstanceLifetime.Process
                    , initializePerformanceCounterInstanceRawValue : 1009
                );

                EasyPerformanceCountersHelper <CommonPerformanceCountersContainer>
                .AttachPerformanceCountersCategoryInstance
                (
                    performanceCountersCategoryName
                    , string.Format
                    (
                        "{1}{0}{2}"
                        , "-"
                        , "Pooled Objects"
                        , performanceCountersCategoryInstanceNamePrefix
                    )
                    , out container
                    , PerformanceCounterInstanceLifetime.Process
                    , initializePerformanceCounterInstanceRawValue : 1009
                );
            }
        }
        private void CountPerformanceBegin
        (
            HttpActionContext actionContext
            , string performanceCountersCategoryName
            , string performanceCountersInstanceName
            , string stopwatchKey
        )
        {
            var enableCountPerformance = false;

            if (OnCheckEnabledCountPerformanceProcessFunc != null)
            {
                enableCountPerformance = OnCheckEnabledCountPerformanceProcessFunc();
            }
            if (enableCountPerformance)
            {
                Stopwatch stopwatch =
                    EasyPerformanceCountersHelper <CommonPerformanceCountersContainer>
                    .CountPerformanceBegin
                    (
                        EnabledCounters
                        , performanceCountersCategoryName
                        , performanceCountersInstanceName
                        , () =>
                {
                    return(enableCountPerformance);
                }

                    );

                if (stopwatch != null)
                {
                    actionContext
                    .Request
                    .Properties[stopwatchKey] = stopwatch;
                }
            }
        }
コード例 #5
0
        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();

#if NET45
//#endif
            Parallel.For
            (
                0
                , iterations
                , new ParallelOptions()
            {
                MaxDegreeOfParallelism = maxDegreeOfParallelism
                                         //, TaskScheduler = null
            }
                , (x) =>
            {
                EasyPerformanceCountersHelper <SessionsPerformanceCountersContainer> .CountPerformance
                (
                    enablePerformanceCounters
                    , performanceCountersCategoryName
                    , performanceCountersCategoryInstanceName
                    , null
                    , actionOnce
                    , null
                    , null
                    , null
                );
            }
            );
//#if NET45
#endif
            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();
        }
コード例 #6
0
        public void AttachPerformanceCountersCategoryInstance
        (
            string performanceCountersCategoryNamePrefix
            , string performanceCountersCategoryInstanceNamePrefix
            , Func <bool> onGetEnabledCountPerformanceProcessFunc = null
            , PerformanceCounterInstanceLifetime
            performanceCounterInstanceLifetime
            = PerformanceCounterInstanceLifetime.Process
        )
        {
            var process            = Process.GetCurrentProcess();
            var processName        = process.ProcessName;
            var instanceNamePrefix = string
                                     .Format
                                     (
                "{0}-{1}"
                , processName
                , performanceCountersCategoryInstanceNamePrefix
                                     );

            instanceNamePrefix = performanceCountersCategoryInstanceNamePrefix;
            var suffix = "-Queue";

            _performanceCountersCategoryNameForQueueProcess         = performanceCountersCategoryNamePrefix + suffix;
            _performanceCountersCategoryInstanceNameForQueueProcess = instanceNamePrefix + suffix;



            var qpcc = _queuePerformanceCountersContainer;

            qpcc
            .AttachPerformanceCountersToMembers
            (
                _performanceCountersCategoryNameForQueueProcess
                , _performanceCountersCategoryInstanceNameForQueueProcess
                , performanceCounterInstanceLifetime
            );
            qpcc
            .RegisterCountersUsage();

            suffix = "-BatchProcess";

            _performanceCountersCategoryNameForBatchProcess         = performanceCountersCategoryNamePrefix + suffix;
            _performanceCountersCategoryInstanceNameForBatchProcess = instanceNamePrefix + suffix;
            CommonPerformanceCountersContainer container = null;

            EasyPerformanceCountersHelper <CommonPerformanceCountersContainer>
            .AttachPerformanceCountersCategoryInstance
            (
                _performanceCountersCategoryNameForBatchProcess
                , _performanceCountersCategoryInstanceNameForBatchProcess
                , out container
                , performanceCounterInstanceLifetime
            );

            if (_timerCounters == null)
            {
                _timerCounters = new WriteableTuple
                                 <
                    bool
                    , Stopwatch
                    , PerformanceCounter
                    , PerformanceCounter
                                 >[]
                {
                    WriteableTuple
                    .Create
                    <
                        bool
                        , Stopwatch
                        , PerformanceCounter
                        , PerformanceCounter
                    >
                    (
                        false
                        , null
                        , _queuePerformanceCountersContainer
                        .QueuedWaitAverageTimerPerformanceCounter
                        , _queuePerformanceCountersContainer
                        .QueuedWaitAverageBasePerformanceCounter
                    )
                    , WriteableTuple
                    .Create
                    <
                        bool
                        , Stopwatch
                        , PerformanceCounter
                        , PerformanceCounter
                    >
                    (
                        true
                        , null
                        , _queuePerformanceCountersContainer
                        .DequeueProcessedAverageTimerPerformanceCounter
                        , _queuePerformanceCountersContainer
                        .DequeueProcessedAverageBasePerformanceCounter
                    )
                };
            }


            _isAttachedPerformanceCounters          = true;
            OnGetEnabledCountPerformanceProcessFunc = onGetEnabledCountPerformanceProcessFunc;
        }
コード例 #7
0
        private void DequeueProcess
        (
            Action <long, T> onOnceDequeueProcessAction = null
            , int sleepInMilliseconds = 100
            , Action <long, List <Tuple <long, T> > > onBatchDequeuesProcessAction = null
            , int waitOneBatchTimeOutInMilliseconds = 1000
            , int waitOneBatchMaxDequeuedTimes      = 100
            , Func <Exception, Exception, string, bool> onDequeueProcessCaughtExceptionProcessFunc       = null
            , Action <bool, Exception, Exception, string> onDequeueProcessFinallyProcessAction           = null
            , Func <Exception, Exception, string, bool> onDequeuesBatchProcessCaughtExceptionProcessFunc = null
            , Action <bool, Exception, Exception, string> onDequeuesBatchProcessFinallyProcessAction     = null
        )
        {
            if (_isStartedDequeueProcess)
            {
                return;
            }
            List <Tuple <long, T> > list = null;
            long      i         = 0;
            Stopwatch stopwatch = null;



            if (onBatchDequeuesProcessAction != null)
            {
                list      = new List <Tuple <long, T> >();
                stopwatch = new Stopwatch();
                stopwatch.Start();
            }
            while (true)
            {
                TryCatchFinallyProcessHelper
                .TryProcessCatchFinally
                (
                    true
                    , () =>
                {
                    if (!_queue.IsEmpty)
                    {
                        Tuple <Stopwatch, T> element = null;
                        if (_queue.TryDequeue(out element))
                        {
                            var enabledCountPerformance = true;
                            {
                                if (OnGetEnabledCountPerformanceProcessFunc != null)
                                {
                                    enabledCountPerformance = OnGetEnabledCountPerformanceProcessFunc();
                                }
                            }

                            var qpcc = _queuePerformanceCountersContainer;
                            Stopwatch stopwatchDequeue = null;
                            var stopwatchEnqueue       = element.Item1;
                            if (enabledCountPerformance)
                            {
                                stopwatchDequeue = _stopwatchsPool.Get();
                            }

                            _timerCounters[0].Item2 = stopwatchEnqueue;
                            _timerCounters[1].Item2 = stopwatchDequeue;

                            #region while queue.IsEmpty loop
                            var reThrowException = false;
                            PerformanceCountersHelper
                            .TryCountPerformance
                            (
                                () =>
                            {
                                return(enabledCountPerformance);
                            }
                                , reThrowException
                                , qpcc.IncrementCountersBeforeCountPerformanceForDequeue
                                , qpcc.DecrementCountersBeforeCountPerformanceForDequeue
                                , _timerCounters
                                , () =>                                     //try
                            {
                                if (onOnceDequeueProcessAction != null)
                                {
                                    var item = element.Item2;
                                    onOnceDequeueProcessAction
                                        (i, item);
                                }
                            }
                                , (x, y, z) =>                                  //catch
                            {
                                qpcc
                                .CaughtExceptionsPerformanceCounter
                                .Increment();

                                z = "OnDequeueProcessCaughtExceptionProcessFunc\r\n" + z;
                                if (onDequeueProcessCaughtExceptionProcessFunc != null)
                                {
                                    reThrowException = onDequeueProcessCaughtExceptionProcessFunc
                                                       (
                                        x
                                        , y
                                        , z
                                                       );
                                }
                                else if (OnCaughtException != null)
                                {
                                    reThrowException = OnCaughtException(this, x, y, z);
                                }
                                return(reThrowException);
                            }
                                , onDequeueProcessFinallyProcessAction                                      //finally
                                , null
                                , qpcc.IncrementCountersAfterCountPerformanceForDequeue
                            );
                            #endregion while queue.IsEmpty loop

                            //池化

                            if (stopwatchEnqueue != null)
                            {
                                stopwatchEnqueue.Reset();
                                var r = _stopwatchsPool.Put(stopwatchEnqueue);
                                if (!r)
                                {
                                    stopwatchEnqueue.Stop();
                                    stopwatchEnqueue = null;
                                }
                            }
                            if (stopwatchDequeue != null)
                            {
                                stopwatchDequeue.Reset();

                                var r = _stopwatchsPool.Put(stopwatchDequeue);
                                if (!r)
                                {
                                    stopwatchDequeue.Stop();
                                    stopwatchDequeue = null;
                                }
                            }
                            if (onBatchDequeuesProcessAction != null)
                            {
                                i++;
                                var item = element.Item2;
                                var tuple
                                    = Tuple
                                      .Create <long, T>
                                      (
                                          i
                                          , item
                                      );
                                list.Add(tuple);
                            }
                        }
                    }
                    else
                    {
                        if (sleepInMilliseconds > 0)
                        {
                            Thread.Sleep(sleepInMilliseconds);
                        }
                    }
                    if (onBatchDequeuesProcessAction != null)
                    {
                        if
                        (
                            i >= waitOneBatchMaxDequeuedTimes
                            ||
                            stopwatch.ElapsedMilliseconds > waitOneBatchTimeOutInMilliseconds
                        )
                        {
                            if (i > 0)
                            {
                                if (stopwatch != null)
                                {
                                    stopwatch.Stop();
                                }
                                EasyPerformanceCountersHelper <CommonPerformanceCountersContainer>
                                .TryCountPerformance
                                (
                                    PerformanceCounterProcessingFlagsType.All
                                    , _performanceCountersCategoryNameForBatchProcess
                                    , _performanceCountersCategoryInstanceNameForBatchProcess
                                    , () =>
                                {
                                    return(OnGetEnabledCountPerformanceProcessFunc());
                                }
                                    , () =>
                                {
                                    onBatchDequeuesProcessAction
                                    (
                                        i
                                        , list
                                    );
                                }
                                    , null
                                    , (xx, yy, zz) =>
                                {
                                    var rrr = false;
                                    zz      = "OnDequeueProcessCaughtExceptionProcessFunc\r\n" + zz;
                                    if (onDequeuesBatchProcessCaughtExceptionProcessFunc != null)
                                    {
                                        rrr = onDequeuesBatchProcessCaughtExceptionProcessFunc
                                              (
                                            xx, yy, zz
                                              );
                                    }
                                    else if (OnCaughtException != null)
                                    {
                                        rrr = OnCaughtException(this, xx, yy, zz);
                                    }
                                    return(rrr);
                                }
                                    , (xx, yy, zz, ww) =>
                                {
                                    i = 0;
                                    list.Clear();
                                    if (stopwatch != null)
                                    {
                                        stopwatch.Restart();
                                    }
                                    onDequeuesBatchProcessFinallyProcessAction?
                                    .Invoke(xx, yy, zz, ww);
                                }
                                );
                            }
                        }
                    }
                }
                    , false
                    , (x, y, z) =>
                {
                    var rr = false;
                    if (OnCaughtException != null)
                    {
                        rr = OnCaughtException
                             (
                            this, x, y, z
                             );
                    }
                    return(rr);
                }
                );
            }
        }
コード例 #8
0
        private void DequeueProcess <TBatchGroupByKey>
        (
            Func <long, JToken, bool> onOnceDequeueProcessFunc
            , int sleepInMilliseconds = 100
            , Func <JToken, TBatchGroupByKey> keySelector = null
            , Action <long, bool, TBatchGroupByKey, IEnumerable <JToken> > onBatchDequeuesProcessAction = null
            , int waitOneBatchTimeOutInMilliseconds = 1000
            , int waitOneBatchMaxDequeuedTimes      = 100
            , Func <Exception, Exception, string, bool> onDequeueProcessCaughtExceptionProcessFunc       = null
            , Action <bool, Exception, Exception, string> onDequeueProcessFinallyProcessAction           = null
            , Func <Exception, Exception, string, bool> onDequeuesBatchProcessCaughtExceptionProcessFunc = null
            , Action <bool, Exception, Exception, string> onDequeuesBatchProcessFinallyProcessAction     = null
        )
        {
            if (_isStartedDequeueProcess)
            {
                return;
            }
            List <JToken> list             = null;
            var           needBatchGroupBy = false;

            if (keySelector != null)
            {
                needBatchGroupBy = true;
            }
            long      i         = 0;
            Stopwatch stopwatch = null;

            if (onBatchDequeuesProcessAction != null)
            {
                list      = new List <JToken>();
                stopwatch = new Stopwatch();
                stopwatch.Start();
            }
            while (true)
            {
                TryCatchFinallyProcessHelper
                .TryProcessCatchFinally
                (
                    true
                    , () =>
                {
                    if (!_queue.IsEmpty)
                    {
                        Tuple <Stopwatch, JToken> element = null;
                        if (_queue.TryDequeue(out element))
                        {
                            var enabledCountPerformance = true;
                            {
                                if (OnGetEnabledCountPerformanceProcessFunc != null)
                                {
                                    enabledCountPerformance = OnGetEnabledCountPerformanceProcessFunc();
                                }
                            }

                            var qpcc = _queuePerformanceCountersContainer;
                            Stopwatch stopwatchDequeue = null;
                            var stopwatchEnqueue       = element.Item1;
                            if (enabledCountPerformance)
                            {
                                _stopwatchsPool.TryGet(out stopwatchDequeue);
                            }

                            _timerCounters[0].Item2 = stopwatchEnqueue;
                            _timerCounters[1].Item2 = stopwatchDequeue;

                            #region while queue.IsEmpty loop
                            var reThrowException = false;
                            JToken item          = null;
                            bool needAdd         = false;
                            PerformanceCountersHelper
                            .TryCountPerformance
                            (
                                () =>
                            {
                                return(enabledCountPerformance);
                            }
                                , reThrowException
                                , qpcc.IncrementCountersBeforeCountPerformanceForDequeue
                                , qpcc.DecrementCountersBeforeCountPerformanceForDequeue
                                , _timerCounters
                                , () =>                                     //try
                            {
                                if (onOnceDequeueProcessFunc != null)
                                {
                                    item    = element.Item2;
                                    needAdd = onOnceDequeueProcessFunc
                                                  (i, item);
                                }
                            }
                                , (x, y, z) =>                                  //catch
                            {
                                qpcc
                                .CaughtExceptionsPerformanceCounter
                                .Increment();

                                z = "OnDequeueProcessCaughtExceptionProcessFunc\r\n" + z;
                                if (onDequeueProcessCaughtExceptionProcessFunc != null)
                                {
                                    reThrowException = onDequeueProcessCaughtExceptionProcessFunc
                                                       (
                                        x
                                        , y
                                        , z
                                                       );
                                }
                                else if (OnCaughtException != null)
                                {
                                    reThrowException = OnCaughtException(this, x, y, z);
                                }
                                return(reThrowException);
                            }
                                , onDequeueProcessFinallyProcessAction                                      //finally
                                , null
                                , qpcc.IncrementCountersAfterCountPerformanceForDequeue
                            );
                            #endregion while queue.IsEmpty loop

                            //池化

                            if (stopwatchEnqueue != null)
                            {
                                stopwatchEnqueue.Reset();
                                var r = _stopwatchsPool.TryPut(stopwatchEnqueue);
                                if (!r)
                                {
                                    stopwatchEnqueue.Stop();
                                    stopwatchEnqueue = null;
                                }
                            }
                            if (stopwatchDequeue != null)
                            {
                                stopwatchDequeue.Reset();

                                var r = _stopwatchsPool.TryPut(stopwatchDequeue);
                                if (!r)
                                {
                                    stopwatchDequeue.Stop();
                                    stopwatchDequeue = null;
                                }
                            }
                            if (onBatchDequeuesProcessAction != null)
                            {
                                //var item = element.Item2;
                                if (needAdd)
                                {
                                    i++;
                                    list.Add(item);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (sleepInMilliseconds > 0)
                        {
                            Thread.Sleep(sleepInMilliseconds);
                        }
                    }
                    if (onBatchDequeuesProcessAction != null)
                    {
                        if
                        (
                            i >= waitOneBatchMaxDequeuedTimes
                            ||
                            stopwatch.ElapsedMilliseconds > waitOneBatchTimeOutInMilliseconds
                        )
                        {
                            if (i > 0)
                            {
                                if (stopwatch != null)
                                {
                                    stopwatch.Stop();
                                }
                                EasyPerformanceCountersHelper <CommonPerformanceCountersContainer>
                                .TryCountPerformance
                                (
                                    PerformanceCounterProcessingFlagsType.All
                                    , _performanceCountersCategoryNameForBatchProcess
                                    , _performanceCountersCategoryInstanceNameForBatchProcess
                                    , () =>
                                {
                                    return(OnGetEnabledCountPerformanceProcessFunc());
                                }
                                    , () =>
                                {
                                    if (needBatchGroupBy)
                                    {
                                        var groups = list
                                                     .GroupBy
                                                     (
                                            (x) =>
                                        {
                                            return(keySelector(x));
                                        }
                                                     );

                                        groups
                                        .AsParallel()
                                        .ForAll
                                        (
                                            (group) =>
                                        {
                                            onBatchDequeuesProcessAction
                                            (
                                                i
                                                , needBatchGroupBy
                                                , group.Key
                                                , group.AsEnumerable()
                                            );
                                        }
                                        );
                                        //foreach (var group in groups)
                                        //{

                                        //}
                                    }
                                    else
                                    {
                                        onBatchDequeuesProcessAction
                                        (
                                            i
                                            , needBatchGroupBy
                                            , default(TBatchGroupByKey)
                                            , list
                                        );
                                    }
                                }
                                    , null
                                    , (xx, yy, zz) =>
                                {
                                    var rrr = false;
                                    zz      = "OnDequeueProcessCaughtExceptionProcessFunc\r\n" + zz;
                                    if (onDequeuesBatchProcessCaughtExceptionProcessFunc != null)
                                    {
                                        rrr = onDequeuesBatchProcessCaughtExceptionProcessFunc
                                              (
                                            xx, yy, zz
                                              );
                                    }
                                    else if (OnCaughtException != null)
                                    {
                                        rrr = OnCaughtException(this, xx, yy, zz);
                                    }
                                    return(rrr);
                                }
                                    , (xx, yy, zz, ww) =>
                                {
                                    i = 0;
                                    list.Clear();
                                    if (stopwatch != null)
                                    {
                                        stopwatch.Restart();
                                    }
                                    onDequeuesBatchProcessFinallyProcessAction?
                                    .Invoke(xx, yy, zz, ww);
                                }
                                );
                            }
                        }
                    }
                }
                    , false
                    , (x, y, z) =>
                {
                    var rr = false;
                    if (OnCaughtException != null)
                    {
                        rr = OnCaughtException
                             (
                            this, x, y, z
                             );
                    }
                    return(rr);
                }
                );
            }
        }