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
                );
            }
        }
示例#2
0
 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
     );
 }
        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 CountPerformance(
										MultiPerformanceCountersTypeFlags enabledPerformanceCounters
										, string performanceCountersCategoryName
										, string performanceCountersCategoryInstanceName
										, Action beforeCountPerformanceInnerProcessAction
										, Action countPerformanceInnerProcessAction
										, Action afterCountPerformanceInnerProcessAction
									)
        {
            if (enabledPerformanceCounters != MultiPerformanceCountersTypeFlags.None)
            {
                if (countPerformanceInnerProcessAction != null)
                {
                    string key = string.Format
                                            (
                                                "{1}{0}{2}"
                                                , "-"
                                                , performanceCountersCategoryName
                                                , performanceCountersCategoryInstanceName
                                            );
                    PerformanceCountersContainer container = null;
                    if (!_dictionary.TryGetValue(key, out container))
                    {
                        lock (_lockerObject)
                        {
                            container = new PerformanceCountersContainer();
                            _dictionary.Add
                                        (
                                            key
                                            , new PerformanceCountersContainer()
                                        );
                            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
                                                );
                    container.ProcessedAverageTimerPerformanceCounter.ChangeAverageTimerCounterValueWithTryCatchExceptionFinally
                                                            (
                                                                enableProcessedAverageTimerCounter
                                                                , container.ProcessedAverageBasePerformanceCounter
                                                                , () =>
                                                                {
                                                                    if (countPerformanceInnerProcessAction != null)
                                                                    {
                                                                        if (beforeCountPerformanceInnerProcessAction != null)
                                                                        {
                                                                            beforeCountPerformanceInnerProcessAction();
                                                                        }
                                                                        countPerformanceInnerProcessAction();
                                                                        if (afterCountPerformanceInnerProcessAction != null)
                                                                        {
                                                                            afterCountPerformanceInnerProcessAction();
                                                                        }
                                                                    }
                                                                }
                                                                , null
                                                                , null
                                                            );
                    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 (countPerformanceInnerProcessAction != null)
                {
                    countPerformanceInnerProcessAction();
                }
            }
        }
        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
                        );
        }
示例#6
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();
        }
示例#7
0
        public static Stopwatch CountPerformanceBegin
        (
            MultiPerformanceCountersTypeFlags enabledPerformanceCounters
            , string performanceCountersCategoryName
            , string performanceCountersCategoryInstanceName
            , Func <bool> onEnabledCountPerformanceProcessFunc = null
            , PerformanceCounterInstanceLifetime
            performanceCounterInstanceLifetime
            = PerformanceCounterInstanceLifetime.Global
            , long?initializePerformanceCounterInstanceRawValue = null
        )
        {
            Stopwatch r = null;
            var       enabledCountPerformance = true;

            {
                if (onEnabledCountPerformanceProcessFunc != null)
                {
                    enabledCountPerformance = onEnabledCountPerformanceProcessFunc.Invoke();
                }
            }
            if
            (
                enabledCountPerformance
                &&
                enabledPerformanceCounters != MultiPerformanceCountersTypeFlags.None
            )
            {
                string key = string
                             .Format
                             (
                    "{1}{0}{2}"
                    , "-"
                    , performanceCountersCategoryName
                    , performanceCountersCategoryInstanceName
                             );
                TPerformanceCountersContainer container = null;
                if (!_dictionary.TryGetValue(key, out container))
                {
                    lock (_lockerObject)
                    {
                        container = new TPerformanceCountersContainer();
                        _dictionary.Add
                        (
                            key
                            , container
                        );
                        container
                        .AttachPerformanceCountersToMembers
                        (
                            performanceCountersCategoryName
                            , performanceCountersCategoryInstanceName
                            , performanceCounterInstanceLifetime
                            , initializePerformanceCounterInstanceRawValue
                        );
                    }
                }
                var enableProcessCounter = enabledPerformanceCounters
                                           .HasFlag
                                           (
                    MultiPerformanceCountersTypeFlags
                    .ProcessCounter
                                           );
                if (enableProcessCounter)
                {
                    container
                    .PrcocessPerformanceCounter
                    .Increment();
                }
                var enableProcessingCounter = enabledPerformanceCounters
                                              .HasFlag
                                              (
                    MultiPerformanceCountersTypeFlags
                    .ProcessingCounter
                                              );
                if (enableProcessingCounter)
                {
                    container.ProcessingPerformanceCounter.Increment();
                }
                var enableProcessedAverageTimerCounter
                    = enabledPerformanceCounters
                      .HasFlag
                      (
                          MultiPerformanceCountersTypeFlags
                          .ProcessedAverageTimerCounter
                      );
                if (enableProcessedAverageTimerCounter)
                {
                    _stopwatchsPool.TryGet(out r); //Stopwatch.StartNew();
                    r.Restart();
                }
            }
            return(r);
        }
示例#8
0
        public static void TryCountPerformance
        (
            MultiPerformanceCountersTypeFlags enabledPerformanceCounters
            , string performanceCountersCategoryName
            , string performanceCountersCategoryInstanceName
            , Func <bool> onEnabledCountPerformanceProcessFunc  = null
            , Action onBeforeCountPerformanceInnerProcessAction = null
            , Action onCountPerformanceInnerProcessAction       = null
            , Action onAfterCountPerformanceInnerProcessAction  = null
            , Func <Exception, Exception, string, bool> onCaughtExceptionProcessFunc = null
            , Action <bool, Exception, Exception, string> onFinallyProcessAction     = null
        )
        {
            var enabledCountPerformance = true;

            {
                if (onEnabledCountPerformanceProcessFunc != null)
                {
                    enabledCountPerformance
                        = onEnabledCountPerformanceProcessFunc();
                }
            }
            if
            (
                enabledCountPerformance
                &&
                enabledPerformanceCounters != MultiPerformanceCountersTypeFlags.None
                &&
                onCountPerformanceInnerProcessAction != null
            )
            {
                if (onCountPerformanceInnerProcessAction != null)
                {
                    string key = string
                                 .Format
                                 (
                        "{1}{0}{2}"
                        , "-"
                        , performanceCountersCategoryName
                        , performanceCountersCategoryInstanceName
                                 );
                    TPerformanceCountersContainer container = null;
                    if (!_dictionary.TryGetValue(key, out container))
                    {
                        lock (_lockerObject)
                        {
                            container = new TPerformanceCountersContainer();
                            _dictionary
                            .Add
                            (
                                key
                                , container
                            );
                            container
                            .AttachPerformanceCountersToMembers
                            (
                                performanceCountersCategoryName
                                , performanceCountersCategoryInstanceName
                            );
                        }
                    }
                    if (enabledCountPerformance)
                    {
                        var enableProcessCounter = enabledPerformanceCounters
                                                   .HasFlag
                                                   (
                            MultiPerformanceCountersTypeFlags
                            .ProcessCounter
                                                   );

                        if (enableProcessCounter)
                        {
                            container
                            .PrcocessPerformanceCounter
                            .Increment();
                        }
                        var enableProcessingCounter =
                            enabledPerformanceCounters
                            .HasFlag
                            (
                                MultiPerformanceCountersTypeFlags
                                .ProcessingCounter
                            );
                        if (enableProcessingCounter)
                        {
                            container
                            .ProcessingPerformanceCounter
                            .Increment();
                        }
                        var enableProcessedAverageTimerCounter =
                            enabledPerformanceCounters
                            .HasFlag
                            (
                                MultiPerformanceCountersTypeFlags
                                .ProcessedAverageTimerCounter
                            );
                        var reThrowException = false;
                        _stopwatchsPool.TryGet(out var stopwatch);
                        //stopwatch.Reset();
                        container
                        .ProcessedAverageTimerPerformanceCounter
                        .TryChangeAverageTimerCounterValue
                        (

                            container
                            .ProcessedAverageBasePerformanceCounter
                            , stopwatch
                            , () =>
                        {
                            return(enableProcessedAverageTimerCounter);
                        }
                            , () =>
                        {
                            if (onCountPerformanceInnerProcessAction != null)
                            {
                                if (onBeforeCountPerformanceInnerProcessAction != null)
                                {
                                    onBeforeCountPerformanceInnerProcessAction();
                                }
                                onCountPerformanceInnerProcessAction();
                                if (onAfterCountPerformanceInnerProcessAction != null)
                                {
                                    onAfterCountPerformanceInnerProcessAction();
                                }
                            }
                        }
                            ,
                            (
                                senderPerformanceCounter
                                , exception
                                , newException
                                , caughtInnerExceptionMessage
                            ) =>                                //catch
                        {
                            container
                            .CaughtExceptionsPerformanceCounter
                            .Increment();
                            var r = reThrowException;
                            if (onCaughtExceptionProcessFunc != null)
                            {
                                r = onCaughtExceptionProcessFunc(exception, newException, caughtInnerExceptionMessage);
                            }
                            return(r);
                        }
                            ,
                            (
                                senderPerformanceCounter
                                , senderBasePerformanceCounter
                                , isCaughtException
                                , exception
                                , newException
                                , caughtInnerExceptionMessage
                            ) =>                                //Finally
                        {
                            if (enableProcessingCounter)
                            {
                                long l = container
                                         .ProcessingPerformanceCounter
                                         .Decrement();
                                if (l < 0)
                                {
                                    container
                                    .ProcessingPerformanceCounter
                                    .RawValue = 0;
                                }
                            }
                            var enableProcessedPerformanceCounter
                                = enabledPerformanceCounters
                                  .HasFlag
                                  (
                                      MultiPerformanceCountersTypeFlags
                                      .ProcessedCounter
                                  );
                            if (enableProcessedPerformanceCounter)
                            {
                                container
                                .ProcessedPerformanceCounter
                                .Increment();
                            }
                            var enableProcessedRateOfCountsPerSecondPerformanceCounter
                                = enabledPerformanceCounters
                                  .HasFlag
                                  (
                                      MultiPerformanceCountersTypeFlags
                                      .ProcessedRateOfCountsPerSecondCounter
                                  );
                            if (enableProcessedRateOfCountsPerSecondPerformanceCounter)
                            {
                                container
                                .ProcessedRateOfCountsPerSecondPerformanceCounter
                                .Increment();
                            }
                        }
                        );
                        stopwatch.Reset();
                        var rr = _stopwatchsPool.TryPut(stopwatch);
                        if (!rr)
                        {
                            stopwatch.Stop();
                            stopwatch = null;
                        }
                    }
                }
            }
            else
            {
                onCountPerformanceInnerProcessAction?.Invoke();
            }
        }
示例#9
0
        public static void CountPerformanceEnd
        (
            MultiPerformanceCountersTypeFlags enabledPerformanceCounters
            , string performanceCountersCategoryName
            , string performanceCountersCategoryInstanceName
            , Stopwatch stopwatch
            , Func <bool> onEnabledCountPerformanceProcessFunc = null
        )
        {
            var enabledCountPerformance = true;

            {
                if (onEnabledCountPerformanceProcessFunc != null)
                {
                    enabledCountPerformance = onEnabledCountPerformanceProcessFunc();
                }
            }
            if
            (
                enabledCountPerformance
                &&
                enabledPerformanceCounters != MultiPerformanceCountersTypeFlags.None
            )
            {
                string key = string.Format
                             (
                    "{1}{0}{2}"
                    , "-"
                    , performanceCountersCategoryName
                    , performanceCountersCategoryInstanceName
                             );
                TPerformanceCountersContainer container = null;
                if (!_dictionary.TryGetValue(key, out container))
                {
                    return;
                }
                var enableProcessedAverageTimerCounter
                    = enabledPerformanceCounters
                      .HasFlag
                      (
                          MultiPerformanceCountersTypeFlags
                          .ProcessedAverageTimerCounter
                      );
                if (enableProcessedAverageTimerCounter)
                {
                    if (stopwatch != null)
                    {
                        var performanceCounter
                            = container
                              .ProcessedAverageTimerPerformanceCounter;
                        var basePerformanceCounter
                            = container
                              .ProcessedAverageBasePerformanceCounter;

                        performanceCounter
                        .IncrementBy(stopwatch.ElapsedTicks);
                        stopwatch.Reset();
                        var r = _stopwatchsPool.TryPut(stopwatch);
                        if (!r)
                        {
                            stopwatch.Stop();
                            stopwatch = null;
                        }
                        basePerformanceCounter.Increment();
                        //stopwatch = null;
                    }
                }
                var enableProcessingCounter
                    = enabledPerformanceCounters
                      .HasFlag
                      (
                          MultiPerformanceCountersTypeFlags
                          .ProcessingCounter
                      );
                if (enableProcessingCounter)
                {
                    long l = container
                             .ProcessingPerformanceCounter
                             .Decrement();
                    if (l < 0)
                    {
                        container
                        .ProcessingPerformanceCounter
                        .RawValue = 0;
                    }
                }
                var enableProcessedPerformanceCounter
                    = enabledPerformanceCounters
                      .HasFlag
                      (
                          MultiPerformanceCountersTypeFlags
                          .ProcessedCounter
                      );
                if (enableProcessedPerformanceCounter)
                {
                    container
                    .ProcessedPerformanceCounter
                    .Increment();
                }
                var enableProcessedRateOfCountsPerSecondPerformanceCounter
                    = enabledPerformanceCounters
                      .HasFlag
                      (
                          MultiPerformanceCountersTypeFlags
                          .ProcessedRateOfCountsPerSecondCounter
                      );
                if (enableProcessedRateOfCountsPerSecondPerformanceCounter)
                {
                    container
                    .ProcessedRateOfCountsPerSecondPerformanceCounter
                    .Increment();
                }
            }
        }
示例#10
0
        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
                             );
                TPerformanceCountersContainer container = null;
                if (!_dictionary.TryGetValue(key, out container))
                {
                    lock (_lockerObject)
                    {
                        container = default(TPerformanceCountersContainer);
                        _dictionary.Add
                        (
                            key
                            , container
                        );
                        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 = _stopwatchsPool.Get(); //Stopwatch.StartNew();
                    r.Reset();
                    r.Start();
                }
            }
            return(r);
        }
示例#11
0
 public static void CountPerformance
 (
     MultiPerformanceCountersTypeFlags enabledPerformanceCounters
     , string performanceCountersCategoryName
     , string performanceCountersCategoryInstanceName
     , Action onBeforeCountPerformanceInnerProcessAction   = null
     , Action onCountPerformanceInnerProcessAction         = null
     , Action onAfterCountPerformanceInnerProcessAction    = null
     , Func <Exception, bool> onCaughtExceptionProcessFunc = null
     , Action <bool, Exception> onFinallyProcessAction     = null
 )
 {
     if (enabledPerformanceCounters != MultiPerformanceCountersTypeFlags.None)
     {
         if (onCountPerformanceInnerProcessAction != null)
         {
             string key = string.Format
                          (
                 "{1}{0}{2}"
                 , "-"
                 , performanceCountersCategoryName
                 , performanceCountersCategoryInstanceName
                          );
             TPerformanceCountersContainer container = null;
             if (!_dictionary.TryGetValue(key, out container))
             {
                 lock (_lockerObject)
                 {
                     container = default(TPerformanceCountersContainer);
                     _dictionary.Add
                     (
                         key
                         , container
                     );
                     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
                 );
             var reThrowException = false;
             var stopwatch        = _stopwatchsPool.Get();
             //stopwatch.Reset();
             container
             .ProcessedAverageTimerPerformanceCounter
             .ChangeAverageTimerCounterValueWithTryCatchExceptionFinally
             (
                 enableProcessedAverageTimerCounter
                 , container.ProcessedAverageBasePerformanceCounter
                 , stopwatch
                 , () =>
             {
                 if (onCountPerformanceInnerProcessAction != null)
                 {
                     if (onBeforeCountPerformanceInnerProcessAction != null)
                     {
                         onBeforeCountPerformanceInnerProcessAction();
                     }
                     onCountPerformanceInnerProcessAction();
                     if (onAfterCountPerformanceInnerProcessAction != null)
                     {
                         onAfterCountPerformanceInnerProcessAction();
                     }
                 }
             }
                 , (x, y) =>                            //catch
             {
                 container
                 .CaughtExceptionsPerformanceCounter
                 .Increment();
                 var r = reThrowException;
                 if (onCaughtExceptionProcessFunc != null)
                 {
                     r = onCaughtExceptionProcessFunc(y);
                 }
                 return(r);
             }
                 , (x, y, z, w) =>                            //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();
                 }
             }
             );
             stopwatch.Reset();
             _stopwatchsPool.Put(stopwatch);
         }
     }
     else
     {
         if (onCountPerformanceInnerProcessAction != null)
         {
             onCountPerformanceInnerProcessAction();
         }
     }
 }
示例#12
0
        public static void CountPerformanceEnd
        (
            MultiPerformanceCountersTypeFlags enabledPerformanceCounters
            , string performanceCountersCategoryName
            , string performanceCountersCategoryInstanceName
            , Stopwatch stopwatch
        )
        {
            string key = string.Format
                         (
                "{1}{0}{2}"
                , "-"
                , performanceCountersCategoryName
                , performanceCountersCategoryInstanceName
                         );
            TPerformanceCountersContainer 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;

                    performanceCounter.IncrementBy(stopwatch.ElapsedTicks);
                    stopwatch.Reset();
                    _stopwatchsPool.Put(stopwatch);
                    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();
            }
        }