예제 #1
0
        /// <summary>
        /// Monitor average response time using a counter having the specified instance name
        /// </summary>
        /// <param name="counterInstanceName">Performance counter instance name</param>
        /// <param name="starttime">the start of the interval for a single sample or response time being averaged</param>
        /// <param name="lockType">some type used internally for locking</param>
        /// <param name="isMonitoringActive">caller can forward external monitoring control</param>
        /// <returns>Current time used as end time for previous sample. Can be used as starttime for next sample</returns>
        public static DateTime MonitorAverageResponse(string counterInstanceName, DateTime starttime, System.Type lockType, bool isMonitoringActive)
        {
            try
            {
                // access counter in hashlist if it is there, else null
                CounterBase counter = (CounterBase)PerformanceCounterFacade.Counters[counterInstanceName];
                // allocate counter if needed
                AverageCounter rtCounter = (AverageCounter)PerformanceCounterFacade.SetCounter(lockType, ref counter, "Average", counterInstanceName, isMonitoringActive);

                if (rtCounter != null)
                {
                    // calculate milliseconds
                    TimeSpan ts = DateTime.Now - starttime;
                    long     ms = (long)ts.TotalMilliseconds;
                    // adjust counters
                    rtCounter.AddSampleValue(ms);
                    rtCounter.IncrementSampleCount();;
                }
                // persist static, threadsafe counter (null if perf monitoring off) for reuse by validator instances
                PerformanceCounterFacade.Counters[counterInstanceName] = rtCounter;
            }
            catch (InvalidOperationException ex)
            {
            }
            return(DateTime.Now);
        }
예제 #2
0
        /// <summary>
        /// Monitor an average value with a counter having the specified instance name
        /// </summary>
        /// <param name="counterInstanceName">Performance counter instance name</param>
        /// <param name="count">the value for a single sample being averaged</param>
        /// <param name="lockType">some type used internally for locking</param>
        /// <param name="isMonitoringActive">caller can forward external monitoring control</param>
        public static void MonitorAverageCount(string counterInstanceName, long count, System.Type lockType, bool isMonitoringActive)
        {
            try
            {
                // access counter in hashlist if it is there, else null
                CounterBase counter = (CounterBase)PerformanceCounterFacade.Counters[counterInstanceName];
                // allocate counter if needed
                AverageCounter avgCounter = (AverageCounter)PerformanceCounterFacade.SetCounter(lockType, ref counter, "Average", counterInstanceName, isMonitoringActive);

                if (avgCounter != null)
                {
                    // adjust counters
                    avgCounter.AddSampleValue(count);
                    avgCounter.IncrementSampleCount();;
                }
                // persist static, threadsafe counter (null if perf monitoring off) for reuse by validator instances
                PerformanceCounterFacade.Counters[counterInstanceName] = avgCounter;
            }
            catch (Exception ex)
            {
                NameValueCollection nvc = new NameValueCollection(2);
                nvc.Add("Method", "MonitorAverageCount");
                nvc.Add("Probable cause", "PerfCounters not registered or wrong category in config file");
                new LoggableException(ex, nvc);
            }
        }
예제 #3
0
        /// <summary>
        /// Synchonize creation and destruction of performance counter references
        /// </summary>
        /// <param name="pc">reference to perfcounter variable from caller</param>
        /// <param name="counterType">underlying perf counter type for which new instance might be created</param>
        /// <param name="instancename">instance name to be assigned for new counter instance</param>
        /// <param name="si">sampling interval for new instance</param>
        /// <param name="estsamplesize">estimated initial sample size</param>
        /// <returns>true if perf counter ref is non null and monitoring should be done</returns>
        /// <remarks>This was done in order to have monitoring respond to config setting and dynamically allocate
        /// and deallocate performance counters according to the setting. It also keeps a lot of
        /// potentially messy code in one place</remarks>
        public static CounterBase SetCounter(Type senderType, ref CounterBase pc, string counterType, string instancename, bool isMonitoringOn)
        {
            // if they should continue not monitoring then exit
            if (!isMonitoringOn && pc == null)
            {
                return(null);
            }
            // if they should continue monitoring then exit
            if (isMonitoringOn && pc != null)
            {
                return(pc);
            }

            // either they are monitoring and should stop or they aren't and they should start

            CounterBase newpc = null;


            if (isMonitoringOn)
            {
                if (pc == null)
                {
                    switch (counterType)
                    {
                    case "SimpleCounter":
                        newpc = new SimpleCounter(instancename);
                        break;

                    case "Average":
                        newpc = new AverageCounter(instancename);
                        break;

                    case "Percent":
                        newpc = new PercentCounter(instancename);
                        break;

                    case "AverageResponse":
                        newpc = new AverageResponseTimeCounter(instancename);
                        break;

                    case "Rate":
                        newpc = new RateCounter(instancename);
                        break;

                    default:
                        break;
                    }
                }
            }

            // since perf counters are static shared objects we must lock
            lock (senderType)
            {
                // change their monitoring state from on to off or from off to on
                if (newpc == null)
                {
                    pc.theCounter.RemoveInstance();
                }
                pc = newpc;
            }

            return(newpc);
        }