internal void PerformanceCollectorRefreshCountersTest(IPerformanceCollector collector)
        {
            var counters = new PerformanceCounter[]
            {
                new PerformanceCounter("Processor", "% Processor Time", "_Total123blabla"),
                new PerformanceCounter("Processor", "% Processor Time", "_Total"),
                new PerformanceCounter("Processor", "% Processor Time", "_Total123afadfdsdf"),
            };

            foreach (var pc in counters)
            {
                try
                {
                    string error = null;
                    collector.RegisterCounter(
                        PerformanceCounterUtility.FormatPerformanceCounter(pc),
                        null,
                        true,
                        out error,
                        false);
                }
                catch (Exception)
                {
                }
            }

            collector.RefreshCounters();

            // All bad state counters are removed and added later through register counter, and as a result, the order of the performance coutners is changed.
            Assert.AreEqual(collector.PerformanceCounters.First().PerformanceCounter.InstanceName, "_Total");
            Assert.AreEqual(collector.PerformanceCounters.Last().PerformanceCounter.InstanceName, "_Total123afadfdsdf");
        }
        internal void PerformanceCollectorBadStateTest(IPerformanceCollector collector)
        {
            var counters = new PerformanceCounter[]
            {
                new PerformanceCounter("Processor", "% Processor Time", "_Total123blabla"),
                new PerformanceCounter("Processor", "% Processor Time", "_Total")
            };

            foreach (var pc in counters)
            {
                try
                {
                    string error = null;
                    collector.RegisterCounter(
                        PerformanceCounterUtility.FormatPerformanceCounter(pc),
                        null,
                        true,
                        out error,
                        false);
                }
                catch (Exception)
                {
                }
            }

            Assert.IsTrue(collector.PerformanceCounters.First().IsInBadState);
            Assert.IsFalse(collector.PerformanceCounters.Last().IsInBadState);
        }
        internal void PerformanceCollectorAddRemoveCountersTest(StandardPerformanceCollector collector)
        {
            var counters = new[]
            {
                new PerformanceCounter("Processor", "% Processor Time", "_Total"),
                new PerformanceCounter("Memory", "Available Bytes", string.Empty)
            };

            foreach (var pc in counters)
            {
                string error;
                collector.RegisterCounter(
                    PerformanceCounterUtility.FormatPerformanceCounter(pc),
                    pc.GetHashCode().ToString(CultureInfo.InvariantCulture),
                    true,
                    out error,
                    false);
            }

            var twoCounters = collector.PerformanceCounters.ToArray();

            collector.RemoveCounter(@"\PROCESSOR(_Total)\% Processor Time", counters[0].GetHashCode().ToString(CultureInfo.InvariantCulture));

            var oneCounter = collector.PerformanceCounters.ToArray();

            Assert.AreEqual(2, twoCounters.Count());
            Assert.AreEqual(@"\Processor(_Total)\% Processor Time", twoCounters[0].OriginalString);
            Assert.AreEqual(@"\Memory\Available Bytes", twoCounters[1].OriginalString);

            Assert.AreEqual(@"\Memory\Available Bytes", oneCounter.Single().OriginalString);
        }
        internal void PerformanceCollectorAddRemoveCountersForWebAppTest(WebAppPerformanceCollector collector)
        {
            var counters = new[]
            {
                new PerformanceCounter("ASP.NET Applications", "Request Execution Time", "??APP_W3SVC_PROC??"),
                new PerformanceCounter("ASP.NET Applications", "Requests In Application Queue", "??APP_W3SVC_PROC??")
            };

            foreach (var pc in counters)
            {
                string error;
                collector.RegisterCounter(
                    PerformanceCounterUtility.FormatPerformanceCounter(pc),
                    pc.GetHashCode().ToString(CultureInfo.InvariantCulture),
                    true,
                    out error,
                    false);
            }

            var twoCounters = collector.PerformanceCounters.ToArray();

            collector.RemoveCounter(
                @"\ASP.NET APPLICATIONS(??APP_W3SVC_PROC??)\Request Execution Time",
                counters[0].GetHashCode().ToString(CultureInfo.InvariantCulture));

            var oneCounter = collector.PerformanceCounters.ToArray();

            Assert.AreEqual(2, twoCounters.Count());
            Assert.AreEqual(@"\ASP.NET Applications(??APP_W3SVC_PROC??)\Request Execution Time", twoCounters[0].OriginalString);
            Assert.AreEqual(@"\ASP.NET Applications(??APP_W3SVC_PROC??)\Requests In Application Queue", twoCounters[1].OriginalString);

            Assert.AreEqual(@"\ASP.NET Applications(??APP_W3SVC_PROC??)\Requests In Application Queue", oneCounter.Single().OriginalString);
        }
        private void RefreshCounter(
            PerformanceCounterData pcd,
            IEnumerable <string> win32Instances,
            IEnumerable <string> clrInstances)
        {
            string dummy;

            bool usesInstanceNamePlaceholder;
            var  pc = this.CreateCounter(
                pcd.OriginalString,
                win32Instances,
                clrInstances,
                out usesInstanceNamePlaceholder,
                out dummy);

            try
            {
                this.collector.RefreshPerformanceCounter(pcd, pc);

                PerformanceCollectorEventSource.Log.CounterRegisteredEvent(
                    PerformanceCounterUtility.FormatPerformanceCounter(pc));
            }
            catch (InvalidOperationException e)
            {
                PerformanceCollectorEventSource.Log.CounterRegistrationFailedEvent(
                    e.Message,
                    PerformanceCounterUtility.FormatPerformanceCounter(pc));
            }
        }
Exemplo n.º 6
0
        private void RegisterCounter(
            string originalString,
            string reportAs,
            PerformanceCounterStructure pc,
            bool isCustomCounter,
            bool usesInstanceNamePlaceholder,
            out string error)
        {
            error = null;

            try
            {
                this.RegisterPerformanceCounter(
                    originalString,
                    reportAs,
                    pc.CategoryName,
                    pc.CounterName,
                    pc.InstanceName,
                    usesInstanceNamePlaceholder,
                    isCustomCounter);

                PerformanceCollectorEventSource.Log.CounterRegisteredEvent(
                    PerformanceCounterUtility.FormatPerformanceCounter(pc));
            }
            catch (InvalidOperationException e)
            {
                PerformanceCollectorEventSource.Log.CounterRegistrationFailedEvent(
                    e.Message,
                    PerformanceCounterUtility.FormatPerformanceCounter(pc));
                error = e.Message;
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Performs collection for all registered counters.
        /// </summary>
        /// <param name="onReadingFailure">Invoked when an individual counter fails to be read.</param>
        public IEnumerable <Tuple <PerformanceCounterData, double> > Collect(
            Action <string, Exception> onReadingFailure = null)
        {
            return(this.performanceCounters.Where(pc => !pc.Item1.IsInBadState).SelectMany(
                       pc =>
            {
                double value;

                try
                {
                    value = CollectCounter(pc.Item2);
                }
                catch (InvalidOperationException e)
                {
                    if (onReadingFailure != null)
                    {
                        onReadingFailure(
                            PerformanceCounterUtility.FormatPerformanceCounter(pc.Item2),
                            e);
                    }

                    return new Tuple <PerformanceCounterData, double>[] { };
                }

                return new[] { Tuple.Create(pc.Item1, value) };
            }));
        }
        /// <summary>
        /// Rebinds performance counters to Windows resources.
        /// </summary>
        public void RefreshPerformanceCounter(PerformanceCounterData pcd)
        {
            Tuple <PerformanceCounterData, ICounterValue> tupleToRemove = this.performanceCounters.FirstOrDefault(t => t.Item1 == pcd);

            if (tupleToRemove != null)
            {
                this.performanceCounters.Remove(tupleToRemove);
            }

            try
            {
                this.RegisterPerformanceCounter(
                    pcd.OriginalString,
                    pcd.ReportAs,
                    pcd.PerformanceCounter.CategoryName,
                    pcd.PerformanceCounter.CounterName,
                    pcd.PerformanceCounter.InstanceName,
                    pcd.UsesInstanceNamePlaceholder);
            }
            catch (InvalidOperationException e)
            {
                PerformanceCollectorEventSource.Log.CounterRegistrationFailedEvent(
                    e.Message,
                    PerformanceCounterUtility.FormatPerformanceCounter(pcd.PerformanceCounter));
            }
        }
        public void PerformanceCollectorBadStateTest()
        {
            var counters = new PerformanceCounter[]
            {
                new PerformanceCounter("Processor", "% Processor Time", "_Total123blabla"),
                new PerformanceCounter("Processor", "% Processor Time", "_Total")
            };

            IPerformanceCollector collector = new PerformanceCollector();

            foreach (var pc in counters)
            {
                try
                {
                    collector.RegisterPerformanceCounter(
                        PerformanceCounterUtility.FormatPerformanceCounter(pc),
                        null,
                        pc.CategoryName,
                        pc.CounterName,
                        pc.InstanceName,
                        false,
                        true);
                }
                catch (Exception)
                {
                }
            }

            Assert.IsTrue(collector.PerformanceCounters.First().IsInBadState);
            Assert.IsFalse(collector.PerformanceCounters.Last().IsInBadState);
        }
        public void PerformanceCollectorRefreshTest()
        {
            var counters = new PerformanceCounter[]
            {
                new PerformanceCounter("Processor", "% Processor Time", "_Total"),
                new PerformanceCounter("Processor", "% Processor Time", "_Total")
            };

            var newCounter = new PerformanceCounter("Memory", "Available Bytes", string.Empty);

            IPerformanceCollector collector = new PerformanceCollector();

            foreach (var pc in counters)
            {
                collector.RegisterPerformanceCounter(
                    PerformanceCounterUtility.FormatPerformanceCounter(pc),
                    null,
                    pc.CategoryName,
                    pc.CounterName,
                    pc.InstanceName,
                    false,
                    true);
            }

            collector.RefreshPerformanceCounter(collector.PerformanceCounters.Last(), newCounter);

            Assert.IsTrue(collector.PerformanceCounters.Last().PerformanceCounter.CategoryName == newCounter.CategoryName);
            Assert.IsTrue(collector.PerformanceCounters.Last().PerformanceCounter.CounterName == newCounter.CounterName);
            Assert.IsTrue(collector.PerformanceCounters.Last().PerformanceCounter.InstanceName == newCounter.InstanceName);
        }
 /// <summary>
 /// Returns the current value of the counter as a <c ref="MetricTelemetry"/>.
 /// </summary>
 /// <returns>Value of the counter.</returns>
 public double Collect()
 {
     try
     {
         return(this.performanceCounter.NextValue());
     }
     catch (Exception e)
     {
         throw new InvalidOperationException(
                   string.Format(
                       CultureInfo.CurrentCulture,
                       Resources.PerformanceCounterReadFailed,
                       PerformanceCounterUtility.FormatPerformanceCounter(this.performanceCounter)),
                   e);
     }
 }
Exemplo n.º 12
0
        /// <summary>
        /// Returns the current value of the counter as a <c ref="MetricTelemetry"/>.
        /// </summary>
        /// <returns>Value of the counter.</returns>
        public double Collect()
        {
            if (!this.isInitialized)
            {
                return(0);
            }

            try
            {
                return(this.performanceCounter.NextValue() / this.processorsCount);
            }
            catch (Exception e)
            {
                throw new InvalidOperationException(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              "Failed to perform a read for performance counter {0}",
                              PerformanceCounterUtility.FormatPerformanceCounter(this.performanceCounter)),
                          e);
            }
        }
        /// <summary>
        /// Performs collection for all registered counters.
        /// </summary>
        /// <param name="onReadingFailure">Invoked when an individual counter fails to be read.</param>
        public IEnumerable <Tuple <PerformanceCounterData, double> > Collect(Action <string, Exception> onReadingFailure = null)
        {
            return(this.performanceCounters.Where(pc => !pc.Item1.IsInBadState).SelectMany(
                       pc =>
            {
                double value;

                try
                {
                    value = pc.Item2.Collect();
                }
                catch (InvalidOperationException e)
                {
                    onReadingFailure?.Invoke(PerformanceCounterUtility.FormatPerformanceCounter(pc.Item1.PerformanceCounter), e);
#if NETSTANDARD2_0
                    return Array.Empty <Tuple <PerformanceCounterData, double> >();
#else
                    return new Tuple <PerformanceCounterData, double>[] { };
#endif
                }

                return new[] { Tuple.Create(pc.Item1, value) };
            }));
        }