Esempio n. 1
0
        //------------------------------------------------------------------------------
        //
        // Method: CreatePerformanceCounters
        //
        //------------------------------------------------------------------------------
        /// <summary>
        /// Creates Windows performance counters for the registered metrics and defined aggregates on the local computer.
        /// </summary>
        public void CreatePerformanceCounters()
        {
            if (counterCreationDataCollection == null)
            {
                counterCreationDataCollection = new CounterCreationDataCollection();
            }

            // Create performance counters for metrics
            foreach (MetricBase currentMetric in registeredMetrics.Values)
            {
                ValidateMetricName(currentMetric);
                ICounterCreationData counterCreationData = counterCreationDataFactory.Create(currentMetric.Name, currentMetric.Description, System.Diagnostics.PerformanceCounterType.NumberOfItems64);
                counterCreationDataCollection.Add(counterCreationData);
            }

            // Create performance counters for various types of aggregates
            foreach (MetricAggregateContainer <CountMetric> currentAggregate in countOverTimeUnitAggregateDefinitions)
            {
                // Create the basic counter as type NumberOfItems64.  The value of the aggregate will be calculated by this class, and will show the average count of items per time unit of the entire duration of running instances of this class (i.e. from when Start() was called).
                ICounterCreationData counterCreationData = counterCreationDataFactory.Create(currentAggregate.Name, currentAggregate.Description, System.Diagnostics.PerformanceCounterType.NumberOfItems64);
                counterCreationDataCollection.Add(counterCreationData);
                // Create a second 'instantaneous' instance of the same metric aggregate
                // If the time unit of the aggregate is second use the RateOfCountsPerSecond64 counter time, for other time units use the where the AverageCount64 type
                //   As per http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecountertype%28v=vs.110%29.aspx AverageCount64 requires an accompanying base counter added directly after it
                if (currentAggregate.DenominatorTimeUnit == TimeUnit.Second)
                {
                    ICounterCreationData instantaneousCounterCreationData = counterCreationDataFactory.Create(CounterNameAppendInstantaneous(currentAggregate.Name), CounterDescriptionAppendInstantaneous(currentAggregate.Description), System.Diagnostics.PerformanceCounterType.RateOfCountsPerSecond64);
                    counterCreationDataCollection.Add(instantaneousCounterCreationData);
                }
                else
                {
                    ICounterCreationData instantaneousCounterCreationData = counterCreationDataFactory.Create(CounterNameAppendInstantaneous(currentAggregate.Name), CounterDescriptionAppendInstantaneous(currentAggregate.Description), System.Diagnostics.PerformanceCounterType.AverageCount64);
                    counterCreationDataCollection.Add(instantaneousCounterCreationData);
                    ICounterCreationData instantaneousBaseCounterCreationData = counterCreationDataFactory.Create(CounterNameAppendInstantaneousBase(currentAggregate.Name), CounterDescriptionAppendInstantaneousBase(currentAggregate.Description), System.Diagnostics.PerformanceCounterType.AverageBase);
                    counterCreationDataCollection.Add(instantaneousBaseCounterCreationData);
                }
            }

            foreach (MetricAggregateContainer <AmountMetric, CountMetric> currentAggregate in amountOverCountAggregateDefinitions)
            {
                ICounterCreationData counterCreationData = counterCreationDataFactory.Create(currentAggregate.Name, currentAggregate.Description, System.Diagnostics.PerformanceCounterType.NumberOfItems64);
                counterCreationDataCollection.Add(counterCreationData);
                // Create a second 'instantaneous' instance of the same metric aggregate
                ICounterCreationData instantaneousCounterCreationData = counterCreationDataFactory.Create(CounterNameAppendInstantaneous(currentAggregate.Name), CounterDescriptionAppendInstantaneous(currentAggregate.Description), System.Diagnostics.PerformanceCounterType.AverageCount64);
                counterCreationDataCollection.Add(instantaneousCounterCreationData);
                ICounterCreationData instantaneousBaseCounterCreationData = counterCreationDataFactory.Create(CounterNameAppendInstantaneousBase(currentAggregate.Name), CounterDescriptionAppendInstantaneousBase(currentAggregate.Description), System.Diagnostics.PerformanceCounterType.AverageBase);
                counterCreationDataCollection.Add(instantaneousBaseCounterCreationData);
            }

            foreach (MetricAggregateContainer <AmountMetric> currentAggregate in amountOverTimeUnitAggregateDefinitions)
            {
                // Create the basic counter as type NumberOfItems64.  The value of the aggregate will be calculated by this class, and will show the average amount per time unit of the entire duration of running instances of this class (i.e. from when Start() was called).
                ICounterCreationData counterCreationData = counterCreationDataFactory.Create(currentAggregate.Name, currentAggregate.Description, System.Diagnostics.PerformanceCounterType.NumberOfItems64);
                counterCreationDataCollection.Add(counterCreationData);
                // Create a second 'instantaneous' instance of the same metric aggregate
                // If the time unit of the aggregate is second use the RateOfCountsPerSecond64 counter time, for other time units use the where the AverageCount64 type
                //   As per http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecountertype%28v=vs.110%29.aspx AverageCount64 requires an accompanying base counter added directly after it
                if (currentAggregate.DenominatorTimeUnit == TimeUnit.Second)
                {
                    ICounterCreationData instantaneousCounterCreationData = counterCreationDataFactory.Create(CounterNameAppendInstantaneous(currentAggregate.Name), CounterDescriptionAppendInstantaneous(currentAggregate.Description), System.Diagnostics.PerformanceCounterType.RateOfCountsPerSecond64);
                    counterCreationDataCollection.Add(instantaneousCounterCreationData);
                }
                else
                {
                    ICounterCreationData instantaneousCounterCreationData = counterCreationDataFactory.Create(CounterNameAppendInstantaneous(currentAggregate.Name), CounterDescriptionAppendInstantaneous(currentAggregate.Description), System.Diagnostics.PerformanceCounterType.AverageCount64);
                    counterCreationDataCollection.Add(instantaneousCounterCreationData);
                    ICounterCreationData instantaneousBaseCounterCreationData = counterCreationDataFactory.Create(CounterNameAppendInstantaneousBase(currentAggregate.Name), CounterDescriptionAppendInstantaneousBase(currentAggregate.Description), System.Diagnostics.PerformanceCounterType.AverageBase);
                    counterCreationDataCollection.Add(instantaneousBaseCounterCreationData);
                }
            }

            foreach (MetricAggregateContainer <AmountMetric, AmountMetric> currentAggregate in amountOverAmountAggregateDefinitions)
            {
                // Create counter as type RawFraction
                ICounterCreationData counterCreationData = counterCreationDataFactory.Create(currentAggregate.Name, currentAggregate.Description, System.Diagnostics.PerformanceCounterType.RawFraction);
                counterCreationDataCollection.Add(counterCreationData);
                ICounterCreationData baseCounterCreationData = counterCreationDataFactory.Create(CounterNameAppendBase(currentAggregate.Name), CounterDescriptionAppendBase(currentAggregate.Description), System.Diagnostics.PerformanceCounterType.RawBase);
                counterCreationDataCollection.Add(baseCounterCreationData);
            }

            foreach (MetricAggregateContainer <IntervalMetric, CountMetric> currentAggregate in intervalOverAmountAggregateDefinitions)
            {
                ICounterCreationData counterCreationData = counterCreationDataFactory.Create(currentAggregate.Name, currentAggregate.Description, System.Diagnostics.PerformanceCounterType.NumberOfItems64);
                counterCreationDataCollection.Add(counterCreationData);
                // Create a second 'instantaneous' instance of the same metric aggregate
                ICounterCreationData instantaneousCounterCreationData = counterCreationDataFactory.Create(CounterNameAppendInstantaneous(currentAggregate.Name), CounterDescriptionAppendInstantaneous(currentAggregate.Description), System.Diagnostics.PerformanceCounterType.AverageCount64);
                counterCreationDataCollection.Add(instantaneousCounterCreationData);
                ICounterCreationData instantaneousBaseCounterCreationData = counterCreationDataFactory.Create(CounterNameAppendInstantaneousBase(currentAggregate.Name), CounterDescriptionAppendInstantaneousBase(currentAggregate.Description), System.Diagnostics.PerformanceCounterType.AverageBase);
                counterCreationDataCollection.Add(instantaneousBaseCounterCreationData);
            }

            foreach (MetricAggregateContainer <IntervalMetric> currentAggregate in intervalOverTotalRunTimeAggregateDefinitions)
            {
                ICounterCreationData counterCreationData = counterCreationDataFactory.Create(currentAggregate.Name, currentAggregate.Description, System.Diagnostics.PerformanceCounterType.RawFraction);
                counterCreationDataCollection.Add(counterCreationData);
                ICounterCreationData baseCounterCreationData = counterCreationDataFactory.Create(CounterNameAppendBase(currentAggregate.Name), CounterDescriptionAppendBase(currentAggregate.Description), System.Diagnostics.PerformanceCounterType.RawBase);
                counterCreationDataCollection.Add(baseCounterCreationData);
            }

            try
            {
                if (performanceCounterCategory.Exists(metricCategoryName) == true)
                {
                    performanceCounterCategory.Delete(metricCategoryName);
                }
                performanceCounterCategory.Create(metricCategoryName, metricCategoryDescription, System.Diagnostics.PerformanceCounterCategoryType.SingleInstance, counterCreationDataCollection);
            }
            catch (Exception e)
            {
                throw new Exception("Failed to create performance counter category.", e);
            }
        }