Пример #1
0
        public void TestCounterMetrics()
        {
            const string name = "countertest";
            const string desc = "countertestdesc";
            IMetricsFactory factory = TangFactory.GetTang().NewInjector().GetInstance<IMetricsFactory>();

            var longCounter = factory.CreateCounter(name, desc, 5);
            Assert.Equal(name, longCounter.Info.Name);
            Assert.Equal(desc, longCounter.Info.Description);

            MetricTestUtils.RecordBuilderForTests recordBuilder = new MetricTestUtils.RecordBuilderForTests();
            SnapshotRequest request = new SnapshotRequest(recordBuilder, false);

            longCounter.OnNext(request);
            recordBuilder.Validate(name, 5);
            recordBuilder.Reset();

            longCounter.Increment();
            longCounter.OnNext(request);
            recordBuilder.Validate(name, 6);
            recordBuilder.Reset();

            longCounter.OnNext(request);
            Assert.False(recordBuilder.LongKeyPresent(name), "Metric is not supposed to be recorded.");

            request = new SnapshotRequest(recordBuilder, true);
            longCounter.OnNext(request);
            recordBuilder.Validate(name, 6);
            recordBuilder.Reset();
        }
Пример #2
0
 private void GiveSnapshot(SnapshotRequest request)
 {
     lock (_lock)
     {
         if (request.FullSnapshot || Changed)
         {
             request.Builder.AddCounter(Info, _value);
             ClearChanged();
         }
     }
 }
Пример #3
0
        public void TestSnapshotRequest()
        {
            var request = new SnapshotRequest(new MetricsRecordBuilderTestImpl(), true);
            Assert.NotNull(request.Builder);
            Assert.Equal(request.FullSnapshot, true);

            request = new SnapshotRequest(new MetricsRecordBuilderTestImpl(), false);
            Assert.NotNull(request.Builder);
            Assert.Equal(request.FullSnapshot, false);

            request = new SnapshotRequest(new MetricsRecordBuilderTestImpl());
            Assert.NotNull(request.Builder);
            Assert.Equal(request.FullSnapshot, false);
        }
Пример #4
0
        private void GiveSnapshot(SnapshotRequest request)
        {
            bool all = request.FullSnapshot;
            var recordBuilder = request.Builder;
            lock (_lock)
            {
                var lastStat = Changed ? _intervalStat : _prevStat;
                if (all || Changed)
                {
                    recordBuilder.AddCounter(_numSamplesInfo, _runningStat.NumSamples)
                        .AddGauge(_currentMeanInfo, lastStat.Mean);

                    if (_showExtendedStats)
                    {
                        recordBuilder.AddGauge(_currentMaxInfo, _intervalStat.MinMaxModule.Max)
                            .AddGauge(_currentMinInfo, _intervalStat.MinMaxModule.Min)
                            .AddGauge(_currentStdInfo, _intervalStat.Std)
                            .AddGauge(_runningMaxInfo, _runningStat.MinMaxModule.Max)
                            .AddGauge(_runningMinInfo, _runningStat.MinMaxModule.Min)
                            .AddGauge(_runningMeanInfo, _runningStat.Mean)
                            .AddGauge(_runningStdInfo, _runningStat.Std);
                    }

                    if (Changed)
                    {
                        if (_runningStat.NumSamples > 0)
                        {
                            _prevStat.CopyFrom(_intervalStat);
                            _intervalStat.Reset();
                        }
                        ClearChanged();
                    }
                }
            }
        }
Пример #5
0
        public void TestGaugeMetrics()
        {
            const string name = "gaugetest";
            const string desc = "guagetestdesc";
            IMetricsFactory factory = TangFactory.GetTang().NewInjector().GetInstance<IMetricsFactory>();

            var doubleGauge = factory.CreateDoubleGauge(name, desc, 5);
            Assert.Equal(name, doubleGauge.Info.Name);
            Assert.Equal(desc, doubleGauge.Info.Description);

            MetricTestUtils.RecordBuilderForTests recordBuilder = new MetricTestUtils.RecordBuilderForTests();
            SnapshotRequest request = new SnapshotRequest(recordBuilder, false);

            doubleGauge.OnNext(request);
            recordBuilder.Validate(name, 5.0, 1e-10);
            recordBuilder.Reset();

            doubleGauge.Increment(2.2);
            doubleGauge.OnNext(request);
            recordBuilder.Validate(name, 7.2, 1e-10);
            recordBuilder.Reset();

            doubleGauge.Decrement(1);
            doubleGauge.OnNext(request);
            recordBuilder.Validate(name, 6.2, 1e-10);
            recordBuilder.Reset();

            doubleGauge.OnNext(request);
            Assert.False(recordBuilder.DoubleKeyPresent(name), "Metric is not supposed to be recorded.");

            request = new SnapshotRequest(recordBuilder, true);
            doubleGauge.OnNext(request);
            recordBuilder.Validate(name, 6.2, 1e-10);
            recordBuilder.Reset();

            request = new SnapshotRequest(recordBuilder, false);
            var longGauge = factory.CreateLongGauge(name, desc, 5);
            Assert.Equal(name, longGauge.Info.Name);
            Assert.Equal(desc, longGauge.Info.Description);

            longGauge.OnNext(request);
            recordBuilder.Validate(name, (long)5);
            recordBuilder.Reset();

            longGauge.Increment(2);
            longGauge.OnNext(request);
            recordBuilder.Validate(name, (long)7);
            recordBuilder.Reset();

            longGauge.Decrement(1);
            longGauge.OnNext(request);
            recordBuilder.Validate(name, (long)6);
            recordBuilder.Reset();

            longGauge.OnNext(request);
            Assert.False(recordBuilder.LongKeyPresent(name), "Metric is not supposed to be recorded.");

            request = new SnapshotRequest(recordBuilder, true);
            longGauge.OnNext(request);
            recordBuilder.Validate(name, (long)6);
            recordBuilder.Reset();
        }
Пример #6
0
        public void TestStatMetrics()
        {
            const string name = "stattest";
            const string desc = "stattestdesc";
            const string valueName = "statValName";
            const double delta = 1e-10;
            IMetricsFactory factory = TangFactory.GetTang().NewInjector().GetInstance<IMetricsFactory>();

            double[] array1 = new double[3];
            double[] array2 = new double[3];
            Random randGen = new Random();

            array1 = array1.Select(x => randGen.NextDouble()).ToArray();
            array2 = array2.Select(x => randGen.NextDouble()).ToArray();

            var stat = factory.CreateStatMetric(name, desc, valueName);
            MetricTestUtils.RecordBuilderForTests recordBuilder = new MetricTestUtils.RecordBuilderForTests();
            var request = new SnapshotRequest(recordBuilder, false);

            foreach (var entry in array1)
            {
                stat.Sample(entry);
            }

            double expectedMean = array1.Sum() / 3;
            double expectedStd = Math.Sqrt(array1.Select(x => Math.Pow(x - expectedMean, 2)).Sum() / 2);

            stat.OnNext(request);
            recordBuilder.Validate(name + "-Num", (long)3);
            recordBuilder.Validate(name + "-RunningAvg", expectedMean, delta);
            recordBuilder.Validate(name + "-RunningStdev", expectedStd, delta);
            recordBuilder.Validate(name + "-IntervalAvg", expectedMean, delta);
            recordBuilder.Validate(name + "-IntervalStdev", expectedStd, delta);
            recordBuilder.Validate(name + "-RunningMin", array1.Min(), delta);
            recordBuilder.Validate(name + "-IntervalMin", array1.Min(), delta);
            recordBuilder.Validate(name + "-RunningMax", array1.Max(), delta);
            recordBuilder.Validate(name + "-IntervalMax", array1.Max(), delta);

            recordBuilder.Reset();
            foreach (var entry in array2)
            {
                stat.Sample(entry);
            }

            double expectedIntervalMean = array2.Sum() / 3;
            double expectedIntervalStd = Math.Sqrt(array2.Select(x => Math.Pow(x - expectedIntervalMean, 2)).Sum() / 2);
            double expectedIntervalMin = array2.Min();
            double expectedIntervalMax = array2.Max();
            double expectedRunningMean = (array1.Sum() + array2.Sum()) / 6;
            double expectedRunningStd =
                Math.Sqrt((array1.Select(x => Math.Pow(x - expectedRunningMean, 2)).Sum() +
                           array2.Select(x => Math.Pow(x - expectedRunningMean, 2)).Sum()) / 5);
            double expectedRunningMin = Math.Min(array1.Min(), array2.Min());
            double expectedRunningMax = Math.Max(array1.Max(), array2.Max());

            stat.OnNext(request);
            recordBuilder.Validate(name + "-Num", (long)6);
            recordBuilder.Validate(name + "-RunningAvg", expectedRunningMean, delta);
            recordBuilder.Validate(name + "-RunningStdev", expectedRunningStd, delta);
            recordBuilder.Validate(name + "-IntervalAvg", expectedIntervalMean, delta);
            recordBuilder.Validate(name + "-IntervalStdev", expectedIntervalStd, delta);
            recordBuilder.Validate(name + "-RunningMin", expectedRunningMin, delta);
            recordBuilder.Validate(name + "-IntervalMin", expectedIntervalMin, delta);
            recordBuilder.Validate(name + "-RunningMax", expectedRunningMax, delta);
            recordBuilder.Validate(name + "-IntervalMax", expectedIntervalMax, delta);

            recordBuilder.Reset();
            stat.OnNext(request);
            Assert.False(recordBuilder.LongKeyPresent(name + "-Num"), "Metric is not supposed to be recorded.");

            request = new SnapshotRequest(recordBuilder, true);
            stat.OnNext(request);
            Assert.True(recordBuilder.LongKeyPresent(name + "-Num"), "Metric is supposed to be recorded.");
        }
Пример #7
0
        /// <summary>
        /// Gets metrics from the source.
        /// </summary>
        /// <param name="collector">Collector that stores the resulting metrics snapshot as records.</param>
        /// <param name="all">If true, gets metric values even if they are unchanged.</param>
        public void GetMetrics(IMetricsCollector collector, bool all)
        {
            lock (_lock)
            {
                var rb = collector.CreateRecord(_recordName)
                    .SetContext(_sourceContext)
                    .AddTag("TaskOrContextName", _contextOrTaskName)
                    .AddTag("EvaluatorId", _evaluatorId)
                    .AddTag("SourceType", "DefaultSource");
                var request = new SnapshotRequest(rb, all);
                foreach (var entry in _observers)
                {
                    entry.OnNext(request);
                }

                foreach (var entry in _tags)
                {
                    rb.Add(entry.Value);
                }
            }
        }
Пример #8
0
 /// <summary>
 /// Simply calls the OnNext() of registered observer.
 /// </summary>
 /// <param name="value">Snapshot request instance</param>
 public void OnNext(SnapshotRequest value)
 {
     _observer.OnNext(value);
 }