//private bool _isAttachedPerformanceCounters = false; public void AttachPerformanceCountersToProperties ( string instanceName , string categoryName ) { var type = this.GetType(); PerformanceCountersHelper.AttachPerformanceCountersToProperties <SessionsPerformanceCountersContainer>(instanceName, categoryName, this); }
public void AttachPerformanceCountersToProperties ( string instanceName , string categoryName ) { if (!_isAttachedPerformanceCounters) { var type = this.GetType(); PerformanceCountersHelper .AttachPerformanceCountersToProperties <QueuePerformanceCountersContainer> (instanceName, categoryName, this); } _isAttachedPerformanceCounters = true; }
protected void AttachPerformanceCountersToMembers <TContainer> ( string categoryName , string instanceName , TContainer container , PerformanceCounterInstanceLifetime performanceCounterInstanceLifetime = PerformanceCounterInstanceLifetime.Global , long?initializePerformanceCounterInstanceRawValue = null ) { PerformanceCountersHelper .AttachPerformanceCountersToMembers <TContainer> ( categoryName , instanceName , container , performanceCounterInstanceLifetime , initializePerformanceCounterInstanceRawValue ); }
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); } ); } }
public bool Enqueue(T item) { var r = false; var reThrowException = false; var enabledCountPerformance = true; if (OnGetEnabledCountPerformanceProcessFunc != null) { enabledCountPerformance = OnGetEnabledCountPerformanceProcessFunc(); } var qpcc = _queuePerformanceCountersContainer; PerformanceCountersHelper .TryCountPerformance ( () => { return(enabledCountPerformance); } , reThrowException , qpcc .IncrementCountersBeforeCountPerformanceForEnqueue //incrementCountersBeforeCountPerformance , null , null , () => { Stopwatch stopwatchEnqueue = null; if (_isAttachedPerformanceCounters) { stopwatchEnqueue = _stopwatchsPool.Get(); } stopwatchEnqueue.Start(); var element = Tuple .Create ( stopwatchEnqueue , item ); _queue.Enqueue(element); //Thread.Sleep(100); r = true; } , (x, y, z) => { qpcc .CaughtExceptionsPerformanceCounter .Increment(); if (OnEnqueueProcessCaughtException != null) { reThrowException = OnEnqueueProcessCaughtException(this, x, y, z); } if (!reThrowException) { if (OnCaughtException != null) { reThrowException = OnCaughtException(this, x, y, z); } } return(reThrowException); } ); return(r); }
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); } ); } }
public bool Enqueue(T item) { var r = false; var reThrowException = false; var enabledCountPerformance = true; if (_onEnabledCountPerformanceProcessFunc != null) { enabledCountPerformance = _onEnabledCountPerformanceProcessFunc(); } var qpcc = PerformanceCountersContainer; PerformanceCountersHelper .TryCountPerformance ( () => { return(enabledCountPerformance); } , reThrowException , qpcc .IncrementCountersBeforeCountPerformanceForEnqueue //incrementCountersBeforeCountPerformance , null , null , () => { Stopwatch stopwatchEnqueue = null; if (_isAttachedPerformanceCounters) { _stopwatchsPool.TryGet(out stopwatchEnqueue); } stopwatchEnqueue.Start(); var element = Tuple .Create ( stopwatchEnqueue , item ); _queue.Enqueue(element); r = true; } , (x, y, z) => { qpcc .CaughtExceptionsPerformanceCounter .Increment(); if (OnEnqueueProcessCaughtException != null) { reThrowException = OnEnqueueProcessCaughtException(this, x, y, z); } if (!reThrowException) { if (OnCaughtException != null) { reThrowException = OnCaughtException(this, x, y, z); } } return(reThrowException); } , (x, y, z, w) => { if ( _waitingDequeueThreadsProcessorsStack != null && _waitingDequeueThreadsProcessorsStack.Count > 0 ) { ThreadProcessor processor = null; if ( _waitingDequeueThreadsProcessorsStack .TryPop(out processor) ) { processor.Waiter.Set(); processor = null; //Console.WriteLine("processor = null; {0}", _waitingDequeueThreadsProcessorsStack.Count); } } } ); return(r); }
public void ThreadProcess() { long l = 0; Interlocked.Increment(ref Sender._concurrentDequeueThreadsCount); QueuePerformanceCountersContainer qpcc = Sender.PerformanceCountersContainer; var queue = Sender.InternalQueue; var reThrowException = false; var enabledCountPerformance = true; { if (Sender._onEnabledCountPerformanceProcessFunc != null) { enabledCountPerformance = Sender._onEnabledCountPerformanceProcessFunc(); } } PerformanceCountersHelper .TryCountPerformance ( () => { return(enabledCountPerformance); } , reThrowException , qpcc.IncrementCountersBeforeCountPerformanceInThread //incrementCountersBeforeCountPerformanceInThread , null , null , () => { #region Try Process if (Sender.OnDequeueThreadStart != null) { l = Interlocked.Read(ref Sender._concurrentDequeueThreadsCount); Sender .OnDequeueThreadStart ( string .Format ( "{0} Threads Count {1},Queue Count {2},Current Thread: {3} at {4}" , "Threads ++ !" , l , queue.Count , Thread.CurrentThread.Name , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffff") ) ); } while (true) { #region while true loop if (Break) { break; } while (!queue.IsEmpty) { #region while queue.IsEmpty loop if (Break) { break; } Tuple <Stopwatch, T> item = null; if (queue.TryDequeue(out item)) { Stopwatch stopwatchDequeue = null; Stopwatch stopwatchEnqueue = item.Item1; if (enabledCountPerformance) { Sender ._stopwatchsPool .TryGet(out stopwatchDequeue); } _timerCounters[0].Item2 = stopwatchEnqueue; _timerCounters[1].Item2 = stopwatchDequeue; if (Sender._onEnabledCountPerformanceProcessFunc != null) { enabledCountPerformance = Sender._onEnabledCountPerformanceProcessFunc(); } PerformanceCountersHelper .TryCountPerformance ( () => { return(enabledCountPerformance); } , reThrowException , //incrementCountersBeforeCountPerformanceForDequeue qpcc.IncrementCountersBeforeCountPerformanceForDequeue , //decrementCountersBeforeCountPerformanceForDequeue qpcc.DecrementCountersBeforeCountPerformanceForDequeue , _timerCounters //qpcc.TimerCounters , () => //try { if (Sender.OnDequeue != null) { var element = item.Item2; item = null; Sender.OnDequeue(element); } } , (x, y, z) => //catch { qpcc .CaughtExceptionsPerformanceCounter .Increment(); if (Sender.OnDequeueProcessCaughtException != null) { reThrowException = Sender .OnDequeueProcessCaughtException ( Sender , x , y , z ); } if (!reThrowException) { if (Sender.OnCaughtException != null) { reThrowException = Sender.OnCaughtException(Sender, x, y, z); } } return(reThrowException); } , null //finally , null , //incrementCountersAfterCountPerformanceForDequeue qpcc.IncrementCountersAfterCountPerformanceForDequeue ); //池化 stopwatchEnqueue.Reset(); stopwatchDequeue.Reset(); var r = Sender._stopwatchsPool.TryPut(stopwatchDequeue); if (!r) { stopwatchDequeue.Stop(); stopwatchDequeue = null; } r = Sender._stopwatchsPool.TryPut(stopwatchEnqueue); if (!r) { stopwatchEnqueue.Stop(); stopwatchEnqueue = null; } } #endregion while queue.IsEmpty loop } #region wait Sender ._waitingDequeueThreadsProcessorsStack .TryPush(this); //Console.WriteLine("Enqueue(this), {0}", Sender._waitingDequeueThreadsProcessorsStack.Count); //Console.WriteLine("Enqueue(this), {0}", Sender._waitingDequeueThreadsProcessorsStack.Count); if (Break) { } if (!Waiter.WaitOne(Sender.DequeueIdleSleepSeconds * 1000)) { } #endregion wait #endregion while true loop } #endregion } , (x, y, z) => //catch { #region Catch Process if (Sender.OnCaughtException != null) { reThrowException = Sender.OnCaughtException(Sender, x, y, z); } return(reThrowException); #endregion } , (x, y, z, w) => //finally { #region Finally Process l = Interlocked.Decrement(ref Sender._concurrentDequeueThreadsCount); if (l < 0) { Interlocked.Exchange(ref Sender._concurrentDequeueThreadsCount, 0); } if (Sender.OnDequeueThreadEnd != null) { Sender .OnDequeueThreadEnd ( string.Format ( "{0} Threads Count {1},Queue Count {2},Current Thread: {3} at {4}" , "Threads--" , l , Sender .InternalQueue .Count , Thread .CurrentThread .Name , DateTime .Now .ToString("yyyy-MM-dd HH:mm:ss.fffff") ) ); } if (!Break) { Sender .StartIncreaseDequeueProcessThreads(1); } Break = false; #endregion } , //decrementCountersAfterCountPerformanceInThread qpcc.DecrementCountersAfterCountPerformanceInThread , //incrementCountersAfterCountPerformanceInThread qpcc.IncrementCountersAfterCountPerformanceInThread ); }
public void ThreadProcess() { long l = 0; Interlocked.Increment(ref Sender._concurrentDequeueThreadsCount); bool counterEnabled = Sender._isAttachedPerformanceCounters; QueuePerformanceCountersContainer qpcc = Sender.PerformanceCounters; var queue = Sender.InternalQueue; var reThrowException = false; PerformanceCounter[] incrementCountersBeforeCountPerformanceForThread = null; PerformanceCounter[] decrementCountersAfterCountPerformanceForThread = null; PerformanceCounter[] incrementCountersAfterCountPerformanceForThread = null; if (counterEnabled && qpcc != null) { incrementCountersBeforeCountPerformanceForThread = new PerformanceCounter[] { qpcc .DequeueThreadStartPerformanceCounter , qpcc .DequeueThreadsCountPerformanceCounter }; decrementCountersAfterCountPerformanceForThread = new PerformanceCounter[] { qpcc.DequeueThreadsCountPerformanceCounter }; incrementCountersAfterCountPerformanceForThread = new PerformanceCounter[] { qpcc.DequeueThreadEndPerformanceCounter }; } PerformanceCountersHelper .TryCountPerformance ( counterEnabled , reThrowException , incrementCountersBeforeCountPerformanceForThread , null , null , () => { #region Try Process if (Sender.OnDequeueThreadStart != null) { l = Interlocked.Read(ref Sender._concurrentDequeueThreadsCount); Sender .OnDequeueThreadStart ( string .Format ( "{0} Threads Count {1},Queue Count {2},Current Thread: {3} at {4}" , "Threads ++ !" , l , queue.Count , Thread.CurrentThread.Name , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffff") ) ); } while (true) { #region while true loop if (Break) { break; } while (!queue.IsEmpty) { #region while queue.IsEmpty loop if (Break) { break; } Tuple <Stopwatch, T> item = null; if (queue.TryDequeue(out item)) { Stopwatch stopwatchDequeue = QueuedObjectsPoolManager.StopwatchsPool.Get(); PerformanceCounter[] incrementCountersBeforeCountPerformanceForDequeue = null; PerformanceCounter[] decrementCountersBeforeCountPerformanceForDequeue = null; PerformanceCounter[] incrementCountersAfterCountPerformanceForDequeue = null; Tuple < bool , Stopwatch , PerformanceCounter , PerformanceCounter >[] timerCounters = null; if (counterEnabled && qpcc != null) { incrementCountersBeforeCountPerformanceForDequeue = new PerformanceCounter[] { qpcc .DequeuePerformanceCounter }; decrementCountersBeforeCountPerformanceForDequeue = new PerformanceCounter[] { qpcc .QueueLengthPerformanceCounter }; timerCounters = new Tuple < bool , Stopwatch , PerformanceCounter , PerformanceCounter >[] { Tuple.Create < bool //before 时是否需要启动 , Stopwatch , PerformanceCounter , PerformanceCounter //base > ( false , item.Item1 , qpcc .QueuedWaitAverageTimerPerformanceCounter , qpcc .QueuedWaitAverageBasePerformanceCounter ) , Tuple.Create < bool , Stopwatch , PerformanceCounter , PerformanceCounter > ( true , stopwatchDequeue , qpcc .DequeueProcessedAverageTimerPerformanceCounter , qpcc .DequeueProcessedAverageBasePerformanceCounter ) }; incrementCountersAfterCountPerformanceForDequeue = new PerformanceCounter[] { qpcc .DequeueProcessedPerformanceCounter , qpcc .DequeueProcessedRateOfCountsPerSecondPerformanceCounter }; } PerformanceCountersHelper .TryCountPerformance ( counterEnabled , reThrowException , incrementCountersBeforeCountPerformanceForDequeue , decrementCountersBeforeCountPerformanceForDequeue , timerCounters , () => //try { if (Sender.OnDequeue != null) { var element = item.Item2; item = null; Sender.OnDequeue(element); } } , (x) => //catch { reThrowException = false; return(reThrowException); } , null //finally , null , incrementCountersAfterCountPerformanceForDequeue ); //池化 stopwatchDequeue.Reset(); QueuedObjectsPoolManager.StopwatchsPool.Put(stopwatchDequeue); } #endregion while queue.IsEmpty loop } #region wait Sender ._dequeueThreadsProcessorsPool .Enqueue(this); if (Break) { } if (!Wait.WaitOne(Sender.DequeueIdleSleepSeconds * 1000)) { } #endregion wait #endregion while true loop } #endregion } , (x) => //catch { #region Catch Process if (Sender.OnCaughtException != null) { reThrowException = Sender.OnCaughtException(Sender, x); } return(reThrowException); #endregion } , (x, y) => //finally { #region Finally Process l = Interlocked.Decrement(ref Sender._concurrentDequeueThreadsCount); if (l < 0) { Interlocked.Exchange(ref Sender._concurrentDequeueThreadsCount, 0); } if (Sender.OnDequeueThreadEnd != null) { Sender .OnDequeueThreadEnd ( string.Format ( "{0} Threads Count {1},Queue Count {2},Current Thread: {3} at {4}" , "Threads--" , l , Sender.InternalQueue.Count , Thread.CurrentThread.Name , DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffff") ) ); } if (!Break) { Sender.StartIncreaseDequeueProcessThreads(1); } Break = false; #endregion } , decrementCountersAfterCountPerformanceForThread , incrementCountersAfterCountPerformanceForThread ); }
public bool Enqueue(T item) { var r = false; var reThrowException = false; var enableCount = _isAttachedPerformanceCounters; PerformanceCounter[] incrementCountersBeforeCountPerformance = null; var qpcc = PerformanceCounters; if (enableCount && qpcc != null) { incrementCountersBeforeCountPerformance = new PerformanceCounter[] { qpcc .EnqueuePerformanceCounter , qpcc .EnqueueRateOfCountsPerSecondPerformanceCounter , qpcc .QueueLengthPerformanceCounter }; } PerformanceCountersHelper .TryCountPerformance ( enableCount , reThrowException , incrementCountersBeforeCountPerformance , null , null , () => { Stopwatch stopwatch = null; if (_isAttachedPerformanceCounters) { stopwatch = QueuedObjectsPoolManager.StopwatchsPool.Get(); stopwatch = Stopwatch.StartNew(); } var element = Tuple.Create <Stopwatch, T>(stopwatch, item); _queue.Enqueue(element); r = true; } , (x) => { if (OnCaughtException != null) { reThrowException = OnCaughtException(this, x); } return(reThrowException); } , (x, y) => { if ( _dequeueThreadsProcessorsPool != null && !_dequeueThreadsProcessorsPool.IsEmpty ) { ThreadProcessor processor; if (_dequeueThreadsProcessorsPool.TryDequeue(out processor)) { processor.Wait.Set(); processor = null; //Console.WriteLine("processor = null;"); } } } ); return(r); }
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); } } } ); }