Ejemplo n.º 1
0
        public void TryRecycle()
        {
            var nonpersistentAggregator = new NaiveDistinctCountMetricSeriesAggregator(
                new MetricSeriesConfigurationForNaiveDistinctCount(usePersistentAggregation: false),
                dataSeries: null,
                aggregationCycleKind: CycleKind.Custom);

            var persistentAggregator = new NaiveDistinctCountMetricSeriesAggregator(
                new MetricSeriesConfigurationForNaiveDistinctCount(usePersistentAggregation: true),
                dataSeries: null,
                aggregationCycleKind: CycleKind.Custom);

            var startTS = new DateTimeOffset(2017, 9, 25, 17, 0, 0, TimeSpan.FromHours(-8));
            var endTS   = new DateTimeOffset(2017, 9, 25, 17, 1, 0, TimeSpan.FromHours(-8));

            long periodMillisDef   = (long)(endTS - default(DateTimeOffset)).TotalMilliseconds;
            long periodMillisStart = (long)(endTS - startTS).TotalMilliseconds;


            {
                nonpersistentAggregator.Reset(startTS, valueFilter: null);

                nonpersistentAggregator.TrackValue(10);
                nonpersistentAggregator.TrackValue(20);
                nonpersistentAggregator.TrackValue(10);

                MetricAggregate aggregate = nonpersistentAggregator.CreateAggregateUnsafe(endTS);
                ValidateNumericAggregateValues(aggregate, name: "null", count: 3, sum: 2, max: 0, min: 0, stdDev: 0, timestamp: startTS, periodMs: periodMillisStart);

                bool canRecycle = nonpersistentAggregator.TryRecycle();
                Assert.IsTrue(canRecycle);

                aggregate = nonpersistentAggregator.CreateAggregateUnsafe(endTS);
                ValidateNumericAggregateValues(aggregate, name: "null", count: 0, sum: 0, max: 0, min: 0, stdDev: 0, timestamp: default(DateTimeOffset), periodMs: periodMillisDef);

                canRecycle = nonpersistentAggregator.TryRecycle();
                Assert.IsTrue(canRecycle);

                aggregate = nonpersistentAggregator.CreateAggregateUnsafe(endTS);
                ValidateNumericAggregateValues(aggregate, name: "null", count: 0, sum: 0, max: 0, min: 0, stdDev: 0, timestamp: default(DateTimeOffset), periodMs: periodMillisDef);
            }
            {
                persistentAggregator.Reset(startTS, valueFilter: null);

                persistentAggregator.TrackValue(-10);
                persistentAggregator.TrackValue(-20);
                persistentAggregator.TrackValue(-10);

                MetricAggregate aggregate = persistentAggregator.CreateAggregateUnsafe(endTS);
                ValidateNumericAggregateValues(aggregate, name: "null", count: 3, sum: 2, max: 0, min: 0, stdDev: 0, timestamp: startTS, periodMs: periodMillisStart);

                bool canRecycle = persistentAggregator.TryRecycle();
                Assert.IsFalse(canRecycle);

                aggregate = persistentAggregator.CreateAggregateUnsafe(endTS);
                ValidateNumericAggregateValues(aggregate, name: "null", count: 3, sum: 2, max: 0, min: 0, stdDev: 0, timestamp: startTS, periodMs: periodMillisStart);
            }
        }
Ejemplo n.º 2
0
        public void CreateAggregateUnsafe()
        {
            var aggregationManager = new MetricAggregationManager();
            var seriesConfig       = new MetricSeriesConfigurationForNaiveDistinctCount(usePersistentAggregation: false);
            var metric             = new MetricSeries(
                aggregationManager,
                new MetricIdentifier(null, "Distinct Cows Sold", "Dim 2", "Dim 3", "Dim 1"),
                new KeyValuePair <string, string>[] { new KeyValuePair <string, string>("Dim 1", "DV1"),
                                                      new KeyValuePair <string, string>("Dim 2", "DV2"),
                                                      new KeyValuePair <string, string>("Dim 3", "DV3"),
                                                      new KeyValuePair <string, string>("Dim 2", "DV2a") },
                seriesConfig);

            var aggregator = new NaiveDistinctCountMetricSeriesAggregator(
                (MetricSeriesConfigurationForNaiveDistinctCount)metric.GetConfiguration(),
                metric,
                CycleKind.Custom);

            var startTS = new DateTimeOffset(2017, 9, 25, 17, 0, 0, TimeSpan.FromHours(-8));
            var endTS   = new DateTimeOffset(2017, 9, 25, 17, 1, 0, TimeSpan.FromHours(-8));

            aggregator.Reset(startTS, valueFilter: null);

            aggregator.TrackValue("Foo");
            aggregator.TrackValue("Bar");
            aggregator.TrackValue("Foo");

            MetricAggregate aggregate = aggregator.CreateAggregateUnsafe(endTS);

            Assert.IsNotNull(aggregate);

            Assert.AreEqual("Distinct Cows Sold", aggregate.MetricId, "aggregate.MetricId mismatch");
            Assert.AreEqual(3, aggregate.Data["TotalCount"], "aggregate.Data[TotalCount] mismatch");
            Assert.AreEqual(2, aggregate.Data["DistinctCount"], "aggregate.Data[DistinctCount] mismatch");

            Assert.AreEqual(startTS, aggregate.AggregationPeriodStart, "metricAggregate.Timestamp mismatch");
            Assert.AreEqual(
                (endTS - startTS).TotalMilliseconds,
                aggregate.AggregationPeriodDuration.TotalMilliseconds,
                "aggregate.AggregationPeriodDuration mismatch");

            Assert.AreEqual(3, aggregate.Dimensions.Count);

            Assert.IsTrue(aggregate.Dimensions.ContainsKey("Dim 1"));
            Assert.AreEqual("DV1", aggregate.Dimensions["Dim 1"]);

            Assert.IsTrue(aggregate.Dimensions.ContainsKey("Dim 2"));
            Assert.AreEqual("DV2a", aggregate.Dimensions["Dim 2"]);

            Assert.IsTrue(aggregate.Dimensions.ContainsKey("Dim 3"));
            Assert.AreEqual("DV3", aggregate.Dimensions["Dim 3"]);
        }
Ejemplo n.º 3
0
        public void CompleteAggregation()
        {
            var aggregationManager = new MetricAggregationManager();

            var nonPersistentConfig = new MetricSeriesConfigurationForNaiveDistinctCount(usePersistentAggregation: false);
            var nonPersistentMetric = new MetricSeries(aggregationManager, new MetricIdentifier("Unique Cows Sold"), null, nonPersistentConfig);

            var nonPersistentAggregator = new NaiveDistinctCountMetricSeriesAggregator(
                (MetricSeriesConfigurationForNaiveDistinctCount)nonPersistentMetric.GetConfiguration(),
                nonPersistentMetric,
                CycleKind.Custom);

            var persistentConfig = new MetricSeriesConfigurationForNaiveDistinctCount(usePersistentAggregation: true);
            var persistentMetric = new MetricSeries(aggregationManager, new MetricIdentifier("Unique Cows Sold"), null, persistentConfig);

            var persistentAggregator = new NaiveDistinctCountMetricSeriesAggregator(
                (MetricSeriesConfigurationForNaiveDistinctCount)persistentMetric.GetConfiguration(),
                persistentMetric,
                CycleKind.Custom);

            var  startTS      = new DateTimeOffset(2017, 9, 25, 17, 0, 0, TimeSpan.FromHours(-8));
            var  endTS        = new DateTimeOffset(2017, 9, 25, 17, 1, 0, TimeSpan.FromHours(-8));
            long periodMillis = (long)(endTS - startTS).TotalMilliseconds;

            int filterDoubleInvocationsCount = 0;
            int filterObjectInvocationsCount = 0;

            nonPersistentAggregator.Reset(
                startTS,
                new CommonSimpleDataSeriesAggregatorTests.CustomDoubleValueFilter(
                    filterFunctionDouble: (s, v) => { filterDoubleInvocationsCount++; return(true); },
                    filterFunctionObject: (s, v) => { filterObjectInvocationsCount++; return(true); }));

            Assert.AreEqual(0, filterDoubleInvocationsCount);
            Assert.AreEqual(0, filterObjectInvocationsCount);

            nonPersistentAggregator.TrackValue(1);
            nonPersistentAggregator.TrackValue("2");
            nonPersistentAggregator.TrackValue(2);

            MetricAggregate aggregate = nonPersistentAggregator.CompleteAggregation(endTS);

            ValidateNumericAggregateValues(aggregate, name: "Unique Cows Sold", count: 3, sum: 2, max: 0, min: 0, stdDev: 0, timestamp: startTS, periodMs: periodMillis);
            Assert.AreEqual(2, filterDoubleInvocationsCount);
            Assert.AreEqual(1, filterObjectInvocationsCount);

            nonPersistentAggregator.TrackValue("3");
            nonPersistentAggregator.TrackValue(4);

            aggregate = nonPersistentAggregator.CompleteAggregation(endTS);

            //// We had this originally when completed agregators did not take any more values when they were non-persistent. This complexity has no benefit.
            //ValidateNumericAggregateValues(aggregate, name: "Unique Cows Sold", count: 3, sum: 2, max: 0, min: 0, stdDev: 0, timestamp: startTS, periodMs: periodMillis);
            //Assert.AreEqual(2, filterDoubleInvocationsCount);
            //Assert.AreEqual(1, filterObjectInvocationsCount);
            ValidateNumericAggregateValues(aggregate, name: "Unique Cows Sold", count: 5, sum: 4, max: 0, min: 0, stdDev: 0, timestamp: startTS, periodMs: periodMillis);
            Assert.AreEqual(3, filterDoubleInvocationsCount);
            Assert.AreEqual(2, filterObjectInvocationsCount);

            filterDoubleInvocationsCount = 0;
            filterObjectInvocationsCount = 0;

            persistentAggregator.Reset(
                startTS,
                new CommonSimpleDataSeriesAggregatorTests.CustomDoubleValueFilter(
                    filterFunctionDouble: (s, v) => { filterDoubleInvocationsCount++; return(true); },
                    filterFunctionObject: (s, v) => { filterObjectInvocationsCount++; return(true); }));

            Assert.AreEqual(0, filterDoubleInvocationsCount);
            Assert.AreEqual(0, filterObjectInvocationsCount);

            persistentAggregator.TrackValue(1);
            persistentAggregator.TrackValue("2");
            persistentAggregator.TrackValue("1");

            aggregate = persistentAggregator.CompleteAggregation(endTS);
            ValidateNumericAggregateValues(aggregate, name: "Unique Cows Sold", count: 3, sum: 2, max: 0, min: 0, stdDev: 0, timestamp: startTS, periodMs: periodMillis);
            Assert.AreEqual(1, filterDoubleInvocationsCount);
            Assert.AreEqual(2, filterObjectInvocationsCount);

            persistentAggregator.TrackValue("3");
            persistentAggregator.TrackValue(4);

            aggregate = persistentAggregator.CompleteAggregation(endTS);
            ValidateNumericAggregateValues(aggregate, name: "Unique Cows Sold", count: 5, sum: 4, max: 0, min: 0, stdDev: 0, timestamp: startTS, periodMs: periodMillis);
            Assert.AreEqual(2, filterDoubleInvocationsCount);
            Assert.AreEqual(3, filterObjectInvocationsCount);
        }
Ejemplo n.º 4
0
        public void Reset()
        {
            var startTS = new DateTimeOffset(2017, 9, 25, 17, 0, 0, TimeSpan.FromHours(-8));
            var endTS   = new DateTimeOffset(2017, 9, 25, 17, 1, 0, TimeSpan.FromHours(-8));

            long periodMillisDef   = (long)(endTS - default(DateTimeOffset)).TotalMilliseconds;
            long periodMillisStart = (long)(endTS - startTS).TotalMilliseconds;

            {
                // Measurement:
                var aggregator = new NaiveDistinctCountMetricSeriesAggregator(
                    new MetricSeriesConfigurationForNaiveDistinctCount(usePersistentAggregation: false),
                    dataSeries: null,
                    aggregationCycleKind: CycleKind.Custom);

                int filterDoubleInvocationsCount = 0;
                int filterObjectInvocationsCount = 0;

                aggregator.TrackValue("Cow 1");
                aggregator.TrackValue("Cow 2");
                aggregator.TrackValue("Cow 2");

                MetricAggregate aggregate = aggregator.CreateAggregateUnsafe(endTS);
                ValidateNumericAggregateValues(aggregate, name: "null", count: 3, sum: 2, max: 0, min: 0, stdDev: 0, timestamp: default(DateTimeOffset), periodMs: periodMillisDef);
                Assert.AreEqual(0, filterDoubleInvocationsCount);
                Assert.AreEqual(0, filterObjectInvocationsCount);

                aggregator.Reset(
                    startTS,
                    new CommonSimpleDataSeriesAggregatorTests.CustomDoubleValueFilter(
                        filterFunctionDouble: (s, v) => { filterDoubleInvocationsCount++; return(true); },
                        filterFunctionObject: (s, v) => { filterObjectInvocationsCount++; return(true); }));

                aggregate = aggregator.CreateAggregateUnsafe(endTS);
                ValidateNumericAggregateValues(aggregate, name: "null", count: 0, sum: 0, max: 0, min: 0, stdDev: 0, timestamp: startTS, periodMs: periodMillisStart);
                Assert.AreEqual(0, filterDoubleInvocationsCount);
                Assert.AreEqual(0, filterObjectInvocationsCount);

                aggregator.TrackValue("Cow 3");
                aggregator.TrackValue("Cow 3");
                aggregator.TrackValue("Cow 4");
                aggregator.TrackValue("Cow 4");

                aggregate = aggregator.CreateAggregateUnsafe(endTS);
                ValidateNumericAggregateValues(aggregate, name: "null", count: 4, sum: 2, max: 0, min: 0, stdDev: 0, timestamp: startTS, periodMs: periodMillisStart);
                Assert.AreEqual(0, filterDoubleInvocationsCount);
                Assert.AreEqual(4, filterObjectInvocationsCount);
            }
            {
                // Accumulator:
                var aggregator = new NaiveDistinctCountMetricSeriesAggregator(
                    new MetricSeriesConfigurationForNaiveDistinctCount(usePersistentAggregation: true),
                    dataSeries: null,
                    aggregationCycleKind: CycleKind.Custom);

                int filterDoubleInvocationsCount = 0;
                int filterObjectInvocationsCount = 0;

                aggregator.TrackValue("Cow 1");
                aggregator.TrackValue("Cow 2");
                aggregator.TrackValue("Cow 2");

                MetricAggregate aggregate = aggregator.CreateAggregateUnsafe(endTS);
                ValidateNumericAggregateValues(aggregate, name: "null", count: 3, sum: 2, max: 0, min: 0, stdDev: 0, timestamp: default(DateTimeOffset), periodMs: periodMillisDef);
                Assert.AreEqual(0, filterDoubleInvocationsCount);
                Assert.AreEqual(0, filterObjectInvocationsCount);

                aggregator.Reset(
                    startTS,
                    new CommonSimpleDataSeriesAggregatorTests.CustomDoubleValueFilter(
                        filterFunctionDouble: (s, v) => { filterDoubleInvocationsCount++; return(true); },
                        filterFunctionObject: (s, v) => { filterObjectInvocationsCount++; return(true); }));

                aggregate = aggregator.CreateAggregateUnsafe(endTS);
                ValidateNumericAggregateValues(aggregate, name: "null", count: 0, sum: 0, max: 0, min: 0, stdDev: 0, timestamp: startTS, periodMs: periodMillisStart);
                Assert.AreEqual(0, filterDoubleInvocationsCount);
                Assert.AreEqual(0, filterObjectInvocationsCount);

                aggregator.TrackValue("Cow 3");
                aggregator.TrackValue("Cow 3");
                aggregator.TrackValue("Cow 4");
                aggregator.TrackValue("Cow 4");

                aggregate = aggregator.CreateAggregateUnsafe(endTS);
                ValidateNumericAggregateValues(aggregate, name: "null", count: 4, sum: 2, max: 0, min: 0, stdDev: 0, timestamp: startTS, periodMs: periodMillisStart);
                Assert.AreEqual(0, filterDoubleInvocationsCount);
                Assert.AreEqual(4, filterObjectInvocationsCount);
            }
        }