Exemple #1
0
        public static void PerformanceCounter_Decrement_DecrementReadOnly()
        {
            var name = nameof(PerformanceCounter_Decrement_DecrementReadOnly) + "_Counter";

            using (PerformanceCounter counterSample = CreateCounterWithCategory(name, true, PerformanceCounterCategoryType.SingleInstance))
            {
                Assert.Throws <InvalidOperationException>(() => counterSample.Decrement());
                Helpers.DeleteCategory(name);
            }
        }
        public static void PerformanceCounter_Increment_IncrementReadOnly()
        {
            string categoryName = nameof(PerformanceCounter_Increment_IncrementReadOnly) + "_Category";

            using (PerformanceCounter counterSample = CreateCounterWithCategory(categoryName, readOnly: true, PerformanceCounterCategoryType.SingleInstance))
            {
                Assert.Throws <InvalidOperationException>(() => counterSample.Increment());
            }

            Helpers.DeleteCategory(categoryName);
        }
Exemple #3
0
        public static void PerformanceCounterCategory_Create_Obsolete()
        {
            var name     = nameof(PerformanceCounterCategory_Create_Obsolete) + "_Counter";
            var category = name + "_Category";

            Helpers.DeleteCategory(category);

            PerformanceCounterCategory.Create(category, "category help", name, "counter help");

            Assert.True(PerformanceCounterCategory.Exists(category));
            PerformanceCounterCategory.Delete(category);
        }
Exemple #4
0
        public static void PerformanceCounter_CreateCounter_Count0()
        {
            var name = nameof(PerformanceCounter_CreateCounter_Count0) + "_Counter";

            using (PerformanceCounter counterSample = CreateCounterWithCategory(name, false, PerformanceCounterCategoryType.SingleInstance))
            {
                counterSample.RawValue = 0;

                Assert.Equal(0, counterSample.RawValue);
                Helpers.DeleteCategory(name);
            }
        }
Exemple #5
0
        public static void PerformanceCounterCategory_Create_Obsolete()
        {
            string categoryName = nameof(PerformanceCounterCategory_Create_Obsolete) + "_Category";
            string counterName  = nameof(PerformanceCounterCategory_Create_Obsolete) + "_Counter";

            Helpers.DeleteCategory(categoryName);

            PerformanceCounterCategory.Create(categoryName, "category help", counterName, "counter help");

            Assert.True(PerformanceCounterCategory.Exists(categoryName));
            PerformanceCounterCategory.Delete(categoryName);
        }
        public static void PerformanceCounter_CreateCounter_Count0()
        {
            string categoryName = nameof(PerformanceCounter_CreateCounter_Count0) + "_Category";

            using (PerformanceCounter counterSample = CreateCounterWithCategory(categoryName, readOnly: false, PerformanceCounterCategoryType.SingleInstance))
            {
                counterSample.RawValue = 0;

                Assert.Equal(0, counterSample.RawValue);
            }

            Helpers.DeleteCategory(categoryName);
        }
Exemple #7
0
        public static void PerformanceCounter_IncrementBy_IncrementBy2()
        {
            var name = nameof(PerformanceCounter_IncrementBy_IncrementBy2) + "_Counter";

            using (PerformanceCounter counterSample = CreateCounterWithCategory(name, false, PerformanceCounterCategoryType.SingleInstance))
            {
                counterSample.RawValue = 10;
                Helpers.RetryOnAllPlatforms(() => counterSample.IncrementBy(2));

                Assert.Equal(12, counterSample.RawValue);
                Helpers.DeleteCategory(name);
            }
        }
Exemple #8
0
        public static void PerformanceCounter_RemoveInstance()
        {
            var name = nameof(PerformanceCounter_RemoveInstance) + "_Counter";

            using (PerformanceCounter counterSample = CreateCounterWithCategory(name, false, PerformanceCounterCategoryType.SingleInstance))
            {
                counterSample.RawValue = 100;
                counterSample.RemoveInstance();
                counterSample.Close();

                Assert.NotNull(counterSample);
                Helpers.DeleteCategory(name);
            }
        }
        public static void PerformanceCounter_IncrementBy_IncrementBy2()
        {
            string categoryName = nameof(PerformanceCounter_IncrementBy_IncrementBy2) + "_Category";

            using (PerformanceCounter counterSample = CreateCounterWithCategory(categoryName, readOnly: false, PerformanceCounterCategoryType.SingleInstance))
            {
                counterSample.RawValue = 10;
                Helpers.RetryOnAllPlatforms(() => counterSample.IncrementBy(2));

                Assert.Equal(12, Helpers.RetryOnAllPlatforms(() => counterSample.NextSample().RawValue));
            }

            Helpers.DeleteCategory(categoryName);
        }
Exemple #10
0
        public static void PerformanceCounter_CreateCounter_SetReadOnly()
        {
            var name = nameof(PerformanceCounter_CreateCounter_SetReadOnly) + "_Counter";

            var category = Helpers.CreateCategory(name, PerformanceCounterCategoryType.SingleInstance);

            using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatforms(() => new PerformanceCounter(category, name)))
            {
                counterSample.ReadOnly = false;

                Assert.False(counterSample.ReadOnly);
            }

            Helpers.DeleteCategory(name);
        }
Exemple #11
0
        public static void PerformanceCounter_CreateCounter_SetReadOnly()
        {
            string categoryName = nameof(PerformanceCounter_CreateCounter_SetReadOnly) + "_Category";
            string counterName  = nameof(PerformanceCounter_CreateCounter_SetReadOnly) + "_Counter";

            Helpers.CreateCategory(categoryName, PerformanceCounterCategoryType.SingleInstance);

            using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatformsWithClosingResources(() => new PerformanceCounter(categoryName, counterName)))
            {
                counterSample.ReadOnly = false;

                Assert.False(counterSample.ReadOnly);
            }

            Helpers.DeleteCategory(categoryName);
        }
Exemple #12
0
        public static void PerformanceCounter_NextSample_MultiInstance()
        {
            var name     = nameof(PerformanceCounter_NextSample_MultiInstance) + "_Counter";
            var instance = name + "_Instance";

            var category = Helpers.CreateCategory(name, PerformanceCounterCategoryType.MultiInstance);

            using (PerformanceCounter counterSample = new PerformanceCounter(category, name, instance, false))
            {
                counterSample.RawValue = 10;
                Helpers.RetryOnAllPlatforms(() => counterSample.Decrement());

                Assert.Equal(9, counterSample.RawValue);
                Helpers.DeleteCategory(name);
            }
        }
Exemple #13
0
        public static void PerformanceCounterCategory_Create_Obsolete_CCD()
        {
            string categoryName = nameof(PerformanceCounterCategory_Create_Obsolete) + "_Category";

            CounterCreationData           ccd  = new CounterCreationData(categoryName, "counter help", PerformanceCounterType.NumberOfItems32);
            CounterCreationDataCollection ccdc = new CounterCreationDataCollection();

            ccdc.Add(ccd);

            Helpers.DeleteCategory(categoryName);

            PerformanceCounterCategory.Create(categoryName, "category help", ccdc);

            Assert.True(PerformanceCounterCategory.Exists(categoryName));
            PerformanceCounterCategory.Delete(categoryName);
        }
Exemple #14
0
        public static void PerformanceCounter_CreateCounter_MultiInstanceReadOnly()
        {
            var name     = nameof(PerformanceCounter_CreateCounter_MultiInstanceReadOnly) + "_Counter";
            var instance = name + "_Instance";

            var category = Helpers.CreateCategory(name, PerformanceCounterCategoryType.MultiInstance);

            using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatforms(() => new PerformanceCounter(category, name, instance)))
            {
                Assert.Equal(name, counterSample.CounterName);
                Assert.Equal(category, counterSample.CategoryName);
                Assert.Equal(instance, counterSample.InstanceName);
                Assert.Equal("counter description", Helpers.RetryOnAllPlatforms(() => counterSample.CounterHelp));
                Assert.True(counterSample.ReadOnly);
                Helpers.DeleteCategory(name);
            }
        }
Exemple #15
0
        public static PerformanceCounter CreateCounter(string categoryName, PerformanceCounterType counterType)
        {
            string counterName = categoryName + "_Counter";

            CounterCreationDataCollection ccdc = new CounterCreationDataCollection();
            CounterCreationData           ccd  = new CounterCreationData();

            ccd.CounterType = counterType;
            ccd.CounterName = counterName;
            ccdc.Add(ccd);

            Helpers.DeleteCategory(categoryName);
            PerformanceCounterCategory.Create(categoryName, "description", PerformanceCounterCategoryType.SingleInstance, ccdc);

            Helpers.VerifyPerformanceCounterCategoryCreated(categoryName);

            return(new PerformanceCounter(categoryName, counterName, readOnly: false));
        }
        public static void PerformanceCounter_NextSample_MultiInstance()
        {
            string categoryName = nameof(PerformanceCounter_NextSample_MultiInstance) + "_Category";
            string counterName  = nameof(PerformanceCounter_NextSample_MultiInstance) + "_Counter";
            string instanceName = nameof(PerformanceCounter_NextSample_MultiInstance) + "_Instance";

            Helpers.CreateCategory(categoryName, PerformanceCounterCategoryType.MultiInstance);

            using (PerformanceCounter counterSample = new PerformanceCounter(categoryName, counterName, instanceName, readOnly: false))
            {
                counterSample.RawValue = 10;
                Helpers.RetryOnAllPlatforms(() => counterSample.Decrement());

                Assert.Equal(9, counterSample.RawValue);
            }

            Helpers.DeleteCategory(categoryName);
        }
Exemple #17
0
        public static PerformanceCounter CreateCounter(string name, PerformanceCounterType counterType)
        {
            var category = name + "_Category";
            var instance = name + "_Instance";

            CounterCreationDataCollection ccdc = new CounterCreationDataCollection();
            CounterCreationData           ccd  = new CounterCreationData();

            ccd.CounterType = counterType;
            ccd.CounterName = name;
            ccdc.Add(ccd);

            Helpers.DeleteCategory(name);
            PerformanceCounterCategory.Create(category, "description", PerformanceCounterCategoryType.SingleInstance, ccdc);

            Assert.True(Helpers.PerformanceCounterCategoryCreated(category));

            return(new PerformanceCounter(category, name, false));
        }
Exemple #18
0
        public static void PerformanceCounter_CreateCounter_MultiInstanceReadOnly()
        {
            string categoryName = nameof(PerformanceCounter_CreateCounter_MultiInstanceReadOnly) + "_Category";
            string counterName  = nameof(PerformanceCounter_CreateCounter_MultiInstanceReadOnly) + "_Counter";
            string instanceName = nameof(PerformanceCounter_CreateCounter_MultiInstanceReadOnly) + "_Instance";

            Helpers.CreateCategory(categoryName, counterName, PerformanceCounterCategoryType.MultiInstance);

            using (PerformanceCounter counterSample = Helpers.RetryOnAllPlatformsWithClosingResources(() => new PerformanceCounter(categoryName, counterName, instanceName)))
            {
                Assert.Equal(counterName, counterSample.CounterName);
                Assert.Equal(categoryName, counterSample.CategoryName);
                Assert.Equal(instanceName, counterSample.InstanceName);
                Assert.Equal("counter description", Helpers.RetryOnAllPlatformsWithClosingResources(() => counterSample.CounterHelp));
                Assert.True(counterSample.ReadOnly);
            }

            Helpers.DeleteCategory(categoryName);
        }
Exemple #19
0
        public static void CounterSampleCalculator_ElapsedTime()
        {
            string categoryName = nameof(CounterSampleCalculator_ElapsedTime) + "_Category";

            PerformanceCounter counterSample = CreateCounter(categoryName, PerformanceCounterType.ElapsedTime);

            counterSample.RawValue = Stopwatch.GetTimestamp();
            DateTime Start = DateTime.Now;

            Helpers.RetryOnAllPlatforms(() => counterSample.NextValue());

            System.Threading.Thread.Sleep(500);

            var counterVal  = Helpers.RetryOnAllPlatforms(() => counterSample.NextValue());
            var dateTimeVal = DateTime.Now.Subtract(Start).TotalSeconds;

            Helpers.DeleteCategory(categoryName);
            Assert.True(Math.Abs(dateTimeVal - counterVal) < .3);
        }