Exemplo n.º 1
0
        public void WhenSamplerIsStopped_PerfCounterProxiesAreDisposed()
        {
            var disposeAttempts = 0;

            //Create a mock proxy that updates a counter
            //of the number of times it is disposed.
            var mockProxy = Mock.Create <IPerformanceCounterProxy>();

            Mock.Arrange(() => mockProxy.Dispose()).DoInstead(() => disposeAttempts++);

            Mock.Arrange(() => _perfCounterProxyFactory.CreatePerformanceCounterProxy(Arg.IsAny <string>(), Arg.IsAny <string>(), Arg.IsAny <string>()))
            .Returns(mockProxy);

            Mock.Arrange(() => _perfCounterProxyFactory.GetCurrentProcessInstanceNameForCategory(Arg.IsAny <string>(), Arg.IsAny <string>()))
            .Returns("Test Value");

            var gcSampleTransformer = Mock.Create <IGcSampleTransformer>();
            var sampler             = new GcSampler(_scheduler, gcSampleTransformer, _perfCounterProxyFactory);

            //Act
            sampler.Start();
            sampler.Sample();
            sampler.Dispose(); // calls sampler.Stop()

            //Assert
            Assert.AreEqual(_countSampleTypes, disposeAttempts, "Perf counter proxies were not disposed when the GcSampler was stopped.");
        }
Exemplo n.º 2
0
        public void SamplingStopsAfterExceedConsecutiveFailureLimit()
        {
            var countAttempts = 0;

            Mock.Arrange(() => _perfCounterProxyFactory.GetCurrentProcessInstanceNameForCategory(Arg.IsAny <string>(), Arg.IsAny <string>()))
            .Returns <string>((catName) =>
            {
                countAttempts++;
                if (countAttempts == 3)
                {
                    return("Test Instance Name");
                }

                throw new Exception();
            });

            //Intercept the stop call for the scheduler to see if the sampler was shut down.
            var stopWasCalled = false;

            Mock.Arrange(() => _scheduler.StopExecuting(Arg.IsAny <Action>(), Arg.IsAny <TimeSpan?>()))
            .DoInstead(() => { stopWasCalled = true; });

            var gcSampleTransformer = Mock.Create <IGcSampleTransformer>();
            var sampler             = new GcSampler(_scheduler, gcSampleTransformer, _perfCounterProxyFactory);

            //enabling the sampler will publish 3 configuration updated events which will in-turn,
            //perform the start actions.   Reset our test values prior to manually trying to start
            //the sampler
            countAttempts = 0;
            stopWasCalled = false;

            Assert.DoesNotThrow(sampler.Start);
            Assert.DoesNotThrow(() => _sampleAction());     //fail
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());     //fail
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());     //success
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());     //fail
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());     //fail
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());     //fail
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());     //fail
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());     //fail
            Assert.IsTrue(stopWasCalled);
        }
Exemplo n.º 3
0
        public void FailureOnSampleShutsDownSampler()
        {
            var countAttempts = 0;

            //Create a mock proxy that throws an exception and that updates a counter
            //of the number of times it was called.
            var mockProxy = Mock.Create <IPerformanceCounterProxy>();

            Mock.Arrange(() => mockProxy.NextValue())
            .Returns(() =>
            {
                countAttempts++;
                throw new Exception();
            });

            Mock.Arrange(() => _perfCounterProxyFactory.CreatePerformanceCounterProxy(Arg.IsAny <string>(), Arg.IsAny <string>(), Arg.IsAny <string>()))
            .Returns(mockProxy);

            Mock.Arrange(() => _perfCounterProxyFactory.GetCurrentProcessInstanceNameForCategory(Arg.IsAny <string>(), Arg.IsAny <string>()))
            .Returns("Test Value");

            var gcSampleTransformer = Mock.Create <IGcSampleTransformer>();
            var sampler             = new GcSampler(_scheduler, gcSampleTransformer, _perfCounterProxyFactory);

            //Intercept the stop call for the scheduler to see if the sampler was shut down.
            var stopWasCalled = false;

            Mock.Arrange(() => _scheduler.StopExecuting(Arg.IsAny <Action>(), Arg.IsAny <TimeSpan?>()))
            .DoInstead(() => { stopWasCalled = true; });


            //Act
            Assert.DoesNotThrow(sampler.Start);
            Assert.DoesNotThrow(() => _sampleAction());
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());

            //Assert
            Assert.Greater(countAttempts, 0);
            Assert.IsTrue(stopWasCalled);
        }
Exemplo n.º 4
0
        public void CreatePerfCounterSingleFailureContinuesToNext()
        {
            var countAttempts = 0;
            var countFails    = 0;
            var countSuccess  = 0;

            //Create a mock proxies, one that fails and one that succeeds
            var mockProxy = Mock.Create <IPerformanceCounterProxy>();

            Mock.Arrange(() => _perfCounterProxyFactory.CreatePerformanceCounterProxy(Arg.IsAny <string>(), Arg.IsAny <string>(), Arg.IsAny <string>()))
            .Returns <string, string>((catName, perfCounterName) =>
            {
                if ((++countAttempts) % 2 == 0)
                {
                    countSuccess++;
                    return(mockProxy);
                }
                else
                {
                    countFails++;
                    throw new Exception();
                }
            });

            Mock.Arrange(() => _perfCounterProxyFactory.GetCurrentProcessInstanceNameForCategory(Arg.IsAny <string>(), Arg.IsAny <string>()))
            .Returns("Test Value");

            var gcSampleTransformer = Mock.Create <IGcSampleTransformer>();
            var sampler             = new GcSampler(_scheduler, gcSampleTransformer, _perfCounterProxyFactory);

            //Intercept the stop call for the scheduler to see if the sampler was shut down.
            var stopWasCalled = false;

            Mock.Arrange(() => _scheduler.StopExecuting(Arg.IsAny <Action>(), Arg.IsAny <TimeSpan?>()))
            .DoInstead(() => { stopWasCalled = true; });

            //Act
            Assert.DoesNotThrow(sampler.Start);
            Assert.DoesNotThrow(sampler.Sample);

            //Assert
            Assert.AreEqual(_countSampleTypes, countAttempts);
            Assert.Greater(countFails, 0);
            Assert.Greater(countSuccess, 0);
            Assert.IsFalse(stopWasCalled);
        }
Exemplo n.º 5
0
        public void CreatePerfCounterAllFailuresShutsDownSampler()
        {
            var countAttempts = 0;

            Mock.Arrange(() => _perfCounterProxyFactory.CreatePerformanceCounterProxy(Arg.IsAny <string>(), Arg.IsAny <string>(), Arg.IsAny <string>()))
            .Returns <string, string>((catName, perfCounterName) =>
            {
                countAttempts++;
                throw new Exception();
            });

            Mock.Arrange(() => _perfCounterProxyFactory.GetCurrentProcessInstanceNameForCategory(Arg.IsAny <string>(), Arg.IsAny <string>()))
            .Returns("Test Value");

            //Intercept the stop call for the scheduler to see if the sampler was shut down.
            var stopWasCalled = false;

            Mock.Arrange(() => _scheduler.StopExecuting(Arg.IsAny <Action>(), Arg.IsAny <TimeSpan?>()))
            .DoInstead(() => { stopWasCalled = true; });

            var gcSampleTransformer = Mock.Create <IGcSampleTransformer>();
            var sampler             = new GcSampler(_scheduler, gcSampleTransformer, _perfCounterProxyFactory);

            //enabling the sampler will publish 3 configuration updated events which will in-turn,
            //perform the start actions.   Reset our test values prior to manually trying to start
            //the sampler
            countAttempts = 0;
            stopWasCalled = false;

            //Act
            Assert.DoesNotThrow(sampler.Start);
            Assert.DoesNotThrow(() => _sampleAction());
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());
            Assert.IsFalse(stopWasCalled);
            Assert.DoesNotThrow(() => _sampleAction());

            //Assert
            Assert.AreEqual(_countSampleTypes * 5, countAttempts);
            Assert.IsTrue(stopWasCalled);
        }
Exemplo n.º 6
0
        public void SamplerCalculatesValuesCorrectly()
        {
            const int   expectedSampleCount  = 2;
            const float startVal             = 3f;
            const float deltaVal             = 10f;
            var         lastPerfCounterValue = 0f;
            var         testPerfCounterValue = startVal;

            //Create a mock proxy that allows us to return a specific value and ensure that the proxy factory returns this proxy.
            var mockProxy = Mock.Create <IPerformanceCounterProxy>();

            Mock.Arrange(() => mockProxy.NextValue())
            .Returns(() => { return(testPerfCounterValue); });

            Mock.Arrange(() => _perfCounterProxyFactory.CreatePerformanceCounterProxy(Arg.IsAny <string>(), Arg.IsAny <string>(), Arg.IsAny <string>()))
            .Returns(mockProxy);

            Mock.Arrange(() => _perfCounterProxyFactory.GetCurrentProcessInstanceNameForCategory(Arg.IsAny <string>(), Arg.IsAny <string>()))
            .Returns("Test Value");

            //Holds the results of the perf counter captures so that we may compare them as part of our assertions.
            List <Dictionary <GCSampleType, float> > perfCounterValues = new List <Dictionary <GCSampleType, float> >();


            //Intercept the transform method so that we can collect the values that were sampled from the performance counters
            var gcSampleTransformer = Mock.Create <IGcSampleTransformer>();

            Mock.Arrange(() => gcSampleTransformer.Transform(Arg.IsAny <Dictionary <GCSampleType, float> >()))
            .DoInstead <Dictionary <GCSampleType, float> >((sampleValues) =>
            {
                lastPerfCounterValue = testPerfCounterValue;
                perfCounterValues.Add(sampleValues);
                testPerfCounterValue += deltaVal;
            });

            var sampler = new GcSampler(_scheduler, gcSampleTransformer, _perfCounterProxyFactory);

            //Act
            sampler.Start();
            _sampleAction();    //Collect Sample 1
            _sampleAction();    //Collect Sample 2

            //Assert
            NrAssert.Multiple(
                () => Assert.AreEqual(expectedSampleCount, perfCounterValues.Count, $"There should have been {expectedSampleCount} samples collected"),
                () => Assert.AreEqual(_countSampleTypes, perfCounterValues[0].Count, $"There should be {_countSampleTypes} in the first sample"),
                () => Assert.AreEqual(_countSampleTypes, perfCounterValues[1].Count, $"There should be {_countSampleTypes} in the second sample")
                );

            //Validate the sampled(calculated) value for each counter type.
            foreach (var gcSampleType in perfCounterValues[0].Keys)
            {
                //For the first sample, all should have the same value
                Assert.AreEqual(startVal, perfCounterValues[0][gcSampleType]);

                //For the second sample...
                switch (gcSampleType)
                {
                //...delta values should represent the size of the delta
                case GCSampleType.Gen0CollectionCount:
                case GCSampleType.Gen1CollectionCount:
                case GCSampleType.Gen2CollectionCount:
                case GCSampleType.InducedCount:
                    Assert.AreEqual(deltaVal, perfCounterValues[1][gcSampleType]);
                    break;

                //...other measurements should be passed through with the perf counter value
                default:
                    Assert.AreEqual(lastPerfCounterValue, perfCounterValues[1][gcSampleType]);
                    break;
                }
            }
        }