public void AddsProcessCpuUsageUsesMEtric()
        {
            var eventListenerMock = new Mock <ISimpleEventListener>();
            ConcurrentDictionary <string, MetricsData> metrics = new ConcurrentDictionary <string, MetricsData>();
            const double cpuValue = 1.1;
            MetricsData  cpuUsage = new MetricsData()
            {
                Name         = "cpu-usage",
                Measurements = new List <Measurement>()
                {
                    new Measurement()
                    {
                        Value = cpuValue
                    }
                },
            };

            metrics.TryAdd("cpu-usage", cpuUsage);
            eventListenerMock.Setup(x => x.Metrics).Returns(metrics);

            var systemStatisticsProviderMock = new Mock <ISystemStatisticsProvider>();

            var basicMetricsProvider = new BasicMetricsProvider(loggerMock, eventListenerMock.Object, systemStatisticsProviderMock.Object);

            basicMetricsProvider.GetMetricNames().Should().Contain("process.cpu.usage");
            var metric = basicMetricsProvider.GetMetricByName("process.cpu.usage");

            metric.Measurements.First().Value.Should().Be(cpuValue);
        }
        public void AddsJvmThreadsDaemon()
        {
            var eventListenerMock = new Mock <ISimpleEventListener>();
            ConcurrentDictionary <string, MetricsData> metrics = new ConcurrentDictionary <string, MetricsData>();

            eventListenerMock.Setup(x => x.Metrics).Returns(metrics);

            var systemStatisticsProviderMock = new Mock <ISystemStatisticsProvider>();
            var basicMetricsProvider         = new BasicMetricsProvider(loggerMock, eventListenerMock.Object, systemStatisticsProviderMock.Object);

            basicMetricsProvider.GetMetricNames().Should().Contain("jvm.threads.daemon");
            var metric = basicMetricsProvider.GetMetricByName("jvm.threads.daemon");

            metric.Measurements.First().Value.Should().Be(0);
        }
        public void AddsSystemCpuCount()
        {
            var eventListenerMock = new Mock <ISimpleEventListener>();
            ConcurrentDictionary <string, MetricsData> metrics = new ConcurrentDictionary <string, MetricsData>();

            eventListenerMock.Setup(x => x.Metrics).Returns(metrics);

            var systemStatisticsProviderMock = new Mock <ISystemStatisticsProvider>();
            var basicMetricsProvider         = new BasicMetricsProvider(loggerMock, eventListenerMock.Object, systemStatisticsProviderMock.Object);

            basicMetricsProvider.GetMetricNames().Should().Contain("system.cpu.count");
            var metric = basicMetricsProvider.GetMetricByName("system.cpu.count");

            metric.Measurements.First().Value.Should().Be(System.Environment.ProcessorCount);
        }
        public void AddsProcessCpuUsageEvenIfNoMetric()
        {
            var eventListenerMock = new Mock <ISimpleEventListener>();
            ConcurrentDictionary <string, MetricsData> metrics = new ConcurrentDictionary <string, MetricsData>();

            eventListenerMock.Setup(x => x.Metrics).Returns(metrics);

            var systemStatisticsProviderMock = new Mock <ISystemStatisticsProvider>();

            var basicMetricsProvider = new BasicMetricsProvider(loggerMock, eventListenerMock.Object, systemStatisticsProviderMock.Object);

            basicMetricsProvider.GetMetricNames().Should().Contain("process.cpu.usage");
            var metric = basicMetricsProvider.GetMetricByName("process.cpu.usage");

            metric.Measurements.First().Value.Should().BeGreaterOrEqualTo(0.0001);
        }
        public void UsesMetricsBySimpleEventListener()
        {
            var eventListenerMock = new Mock <ISimpleEventListener>();
            ConcurrentDictionary <string, MetricsData> metrics = new ConcurrentDictionary <string, MetricsData>();
            MetricsData metric = new MetricsData();

            metrics.TryAdd("test", metric);
            eventListenerMock.Setup(x => x.Metrics).Returns(metrics);

            var systemStatisticsProviderMock = new Mock <ISystemStatisticsProvider>();

            var basicMetricsProvider = new BasicMetricsProvider(loggerMock, eventListenerMock.Object, systemStatisticsProviderMock.Object);

            basicMetricsProvider.GetMetricNames().Should().Contain("test");
            basicMetricsProvider.GetMetricByName("test").Should().Be(metric);
        }
        public void AddsJvmThreadsPeak()
        {
            var eventListenerMock = new Mock <ISimpleEventListener>();
            ConcurrentDictionary <string, MetricsData> metrics = new ConcurrentDictionary <string, MetricsData>();

            eventListenerMock.Setup(x => x.Metrics).Returns(metrics);

            var systemStatisticsProviderMock = new Mock <ISystemStatisticsProvider>();
            var basicMetricsProvider         = new BasicMetricsProvider(loggerMock, eventListenerMock.Object, systemStatisticsProviderMock.Object);

            basicMetricsProvider.GetMetricNames().Should().Contain("jvm.threads.peak");
            var metric      = basicMetricsProvider.GetMetricByName("jvm.threads.peak");
            int threadCount = Process.GetCurrentProcess().Threads.Count;

            metric.Measurements.First().Value.Should().Be(threadCount);

            // todo move this logic to get the threadcoun to systemstatsprovider, for easier and better test
        }
        public void AddsSystemCpuUsage()
        {
            var eventListenerMock = new Mock <ISimpleEventListener>();
            ConcurrentDictionary <string, MetricsData> metrics = new ConcurrentDictionary <string, MetricsData>();

            eventListenerMock.Setup(x => x.Metrics).Returns(metrics);

            var          systemStatisticsProviderMock = new Mock <ISystemStatisticsProvider>();
            const double systemCpuUsage = 2.2;

            systemStatisticsProviderMock.Setup(x => x.Metric).Returns(systemCpuUsage);

            var basicMetricsProvider = new BasicMetricsProvider(loggerMock, eventListenerMock.Object, systemStatisticsProviderMock.Object);

            basicMetricsProvider.GetMetricNames().Should().Contain("system.cpu.usage");
            var metric = basicMetricsProvider.GetMetricByName("system.cpu.usage");

            metric.Measurements.First().Value.Should().Be(systemCpuUsage);
        }
        public void AddsProcessUptime()
        {
            var eventListenerMock = new Mock <ISimpleEventListener>();
            ConcurrentDictionary <string, MetricsData> metrics = new ConcurrentDictionary <string, MetricsData>();

            eventListenerMock.Setup(x => x.Metrics).Returns(metrics);

            var systemStatisticsProviderMock = new Mock <ISystemStatisticsProvider>();

            TimeSpan uptime = new TimeSpan(0, 1, 0);

            systemStatisticsProviderMock.Setup(x => x.GetProcessUptime()).Returns(uptime);

            var basicMetricsProvider = new BasicMetricsProvider(loggerMock, eventListenerMock.Object, systemStatisticsProviderMock.Object);

            basicMetricsProvider.GetMetricNames().Should().Contain("process.uptime");
            var metric = basicMetricsProvider.GetMetricByName("process.uptime");

            metric.Measurements.First().Value.Should().Be(uptime.TotalSeconds);
        }
        public void AddsJvmGcPause()
        {
            var eventListenerMock = new Mock <ISimpleEventListener>();
            ConcurrentDictionary <string, MetricsData> metrics = new ConcurrentDictionary <string, MetricsData>();

            eventListenerMock.Setup(x => x.Metrics).Returns(metrics);

            var firstEvent = new MetricsData()
            {
                Name         = "% Time in GC since last GC",
                Measurements = new List <Measurement>()
                {
                    new Measurement()
                    {
                        Statistic = "VALUE", Value = 50
                    }
                },
                AvailableTags = new List <AvailableTag>()
                {
                    new AvailableTag()
                    {
                        Tag = "StandardDeviation", Values = new Dictionary <string, double> {
                            { "StandardDeviation", 0.0 }
                        },
                    },
                    new AvailableTag()
                    {
                        Tag = "Count", Values = new Dictionary <string, double> {
                            { "Count", 1.0 }
                        },
                    },
                    new AvailableTag()
                    {
                        Tag = "Min", Values = new Dictionary <string, double> {
                            { "Min", 0.0 }
                        },
                    },
                    new AvailableTag()
                    {
                        Tag = "Max", Values = new Dictionary <string, double> {
                            { "Max", 0.0 }
                        },
                    },
                    new AvailableTag()
                    {
                        Tag = "IntervalSec", Values = new Dictionary <string, double> {
                            { "IntervalSec", 1.0 }
                        },
                    },
                    new AvailableTag()
                    {
                        Tag = "Series", Values = new Dictionary <string, double> {
                            { "Series", 1000.0 }
                        },
                    },
                },
            };

            var secondEvent = new MetricsData()
            {
                Name         = "% Time in GC since last GC",
                Measurements = new List <Measurement>()
                {
                    new Measurement()
                    {
                        Statistic = "VALUE", Value = 25
                    }
                },
                AvailableTags = new List <AvailableTag>()
                {
                    new AvailableTag()
                    {
                        Tag = "StandardDeviation", Values = new Dictionary <string, double> {
                            { "StandardDeviation", 0.0 }
                        },
                    },
                    new AvailableTag()
                    {
                        Tag = "Count", Values = new Dictionary <string, double> {
                            { "Count", 1.0 }
                        },
                    },
                    new AvailableTag()
                    {
                        Tag = "Min", Values = new Dictionary <string, double> {
                            { "Min", 0.0 }
                        },
                    },
                    new AvailableTag()
                    {
                        Tag = "Max", Values = new Dictionary <string, double> {
                            { "Max", 0.0 }
                        },
                    },
                    new AvailableTag()
                    {
                        Tag = "IntervalSec", Values = new Dictionary <string, double> {
                            { "IntervalSec", 1.0 }
                        },
                    },
                    new AvailableTag()
                    {
                        Tag = "Series", Values = new Dictionary <string, double> {
                            { "Series", 1000.0 }
                        },
                    },
                },
            };

            var systemStatisticsProviderMock = new Mock <ISystemStatisticsProvider>();

            systemStatisticsProviderMock.Setup(x => x.GetGCCount()).Returns(2);
            var basicMetricsProvider = new BasicMetricsProvider(loggerMock, eventListenerMock.Object, systemStatisticsProviderMock.Object);

            eventListenerMock.Raise(x => x.GCCollectionEvent += null, new GcTotalTimeEventArgs(firstEvent));
            eventListenerMock.Raise(x => x.GCCollectionEvent += null, new GcTotalTimeEventArgs(secondEvent));

            basicMetricsProvider.GetMetricNames().Should().Contain("jvm.gc.pause");
            var metric = basicMetricsProvider.GetMetricByName("jvm.gc.pause");

            metric.Measurements.Should().HaveCount(3);

            metric.Measurements.First(x => x.Statistic == "COUNT").Value.Should().Be(2);
            metric.Measurements.First(x => x.Statistic == "TOTAL_TIME").Value.Should().Be(0.75);
            metric.Measurements.First(x => x.Statistic == "MAX").Value.Should().Be(0.5);
        }