public void BencharmSummaryWrite(int w)
        {
            var stopwatch = new Stopwatch();

            var summary = new Summary("test_summary", "helpless", new string[0]);
            var child = new Summary.Child();
            var now = new DateTime(2016, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            child.Init(summary, LabelValues.Empty, now);

            const int N = 1000;

            for (var obsNum = 0; obsNum < 1000000; obsNum++)
            {
                child.Observe(obsNum, now);
            }

            stopwatch.Start();
            var tasks = new Task[w];
            for (var taskNum = 0; taskNum < w; taskNum++)
            {
                var metric = new Metric();

                tasks[taskNum] = Task.Factory.StartNew(() =>
                {
                    for (var i = 0; i < N; i++)
                        child.Populate(metric, now);
                });
            }
            
            Task.WaitAll(tasks);
            stopwatch.Stop();

            TestContext.WriteLine($"{w} tasks doing {N} writes took {stopwatch.Elapsed.TotalMilliseconds} milliseconds");
        }
Example #2
0
        public void TestSummaryDecay()
        {
            var baseTime = new DateTime(2015, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            
            var sum = new Summary("test_summary", "helpless", new string[0], objectives: new List<QuantileEpsilonPair> {new QuantileEpsilonPair(0.1d, 0.001d)}, maxAge: TimeSpan.FromSeconds(100), ageBuckets: 10);
            var child = new Summary.Child();
            child.Init(sum, LabelValues.Empty, baseTime);
            
            Advanced.DataContracts.Summary m;
            var metric = new Metric();
            
            for (var i = 0; i < 1000; i++)
            {
                var now = baseTime.AddSeconds(i);
                child.Observe(i, now);
                
                if (i%10 == 0)
                {
                    child.Populate(metric, now);
                    m = metric.summary;
                    var got = m.quantile[0].value;
                    var want = Math.Max((double) i/10, (double) i - 90);

                    Assert.That(Math.Abs(got-want), Is.LessThanOrEqualTo(1), $"{i}. got {got} want {want}");
                }
            }

            // Wait for MaxAge without observations and make sure quantiles are NaN.
            child.Populate(metric, baseTime.AddSeconds(1000).AddSeconds(100));
            m = metric.summary;

            Assert.That(m.quantile[0].value, Is.NaN);
        }
            internal void Populate(Metric metric, DateTime now)
            {
                var summary   = new Advanced.DataContracts.Summary();
                var quantiles = new Quantile[_objectives.Count];

                lock (_bufLock)
                {
                    lock (_lock)
                    {
                        // Swap bufs even if hotBuf is empty to set new hotBufExpTime.
                        SwapBufs(now);
                        FlushColdBuf();
                        summary.sample_count = _count;
                        summary.sample_sum   = _sum;

                        for (var idx = 0; idx < _sortedObjectives.Length; idx++)
                        {
                            var rank = _sortedObjectives[idx];
                            var q    = _headStream.Count == 0 ? double.NaN : _headStream.Query(rank);

                            quantiles[idx] = new Quantile
                            {
                                quantile = rank,
                                value    = q
                            };
                        }
                    }
                }

                if (quantiles.Length > 0)
                {
                    Array.Sort(quantiles, _quantileComparer);
                }

                for (var i = 0; i < quantiles.Length; i++)
                {
                    summary.quantile.Add(quantiles[i]);
                }

                metric.summary = summary;
            }
            internal void Init(ICollector parent, LabelValues labelValues, DateTime now)
            {
                base.Init(parent, labelValues);

                _objectives = ((Summary)parent)._objectives;
                _maxAge     = ((Summary)parent)._maxAge;
                _ageBuckets = ((Summary)parent)._ageBuckets;
                _bufCap     = ((Summary)parent)._bufCap;

                _sortedObjectives  = new double[_objectives.Count];
                _hotBuf            = new SampleBuffer(_bufCap);
                _coldBuf           = new SampleBuffer(_bufCap);
                _streamDuration    = new TimeSpan(_maxAge.Ticks / _ageBuckets);
                _headStreamExpTime = now.Add(_streamDuration);
                _hotBufExpTime     = _headStreamExpTime;

                _streams = new QuantileStream[_ageBuckets];
                for (var i = 0; i < _ageBuckets; i++)
                {
                    _streams[i] = QuantileStream.NewTargeted(_objectives);
                }

                _headStream = _streams[0];

                for (var i = 0; i < _objectives.Count; i++)
                {
                    _sortedObjectives[i] = _objectives[i].Quantile;
                }

                Array.Sort(_sortedObjectives);

                _wireMetric = new Advanced.DataContracts.Summary();

                for (var i = 0; i < _objectives.Count; i++)
                {
                    _wireMetric.quantile.Add(new Quantile
                    {
                        quantile = _objectives[i].Quantile
                    });
                }
            }
        public void BenchmarkSummaryObserve(int w)
        {
            var stopwatch = new Stopwatch();
            
            const int N = 100000;
            var summary = new Summary("test_summary", "helpless", new string[0]);
            var tasks = new Task[w];

            stopwatch.Start();
            for (var i = 0; i < w; i++)
            {
                tasks[i] = Task.Factory.StartNew(() =>
                {
                    for (var j = 0; j < N; j++)
                        summary.Observe(j);
                });
            }

            Task.WaitAll(tasks);
            stopwatch.Stop();

            TestContext.WriteLine($"{w} tasks doing  {N} observations took {stopwatch.Elapsed.TotalMilliseconds} milliseconds");
        }
Example #6
0
        public void TestSummaryConcurrency([Range(0, 1000000, 10000)] int n)
        {
            var random = new Random(42);
            var mutations = n%10000 + 10000;
            var concLevel = n%5 + 1;
            var total = mutations * concLevel;
                
            var sum = new Summary("test_summary", "helpless", new string[0]);
            var allVars = new double[total];
            double sampleSum = 0;
            var tasks = new List<Task>();

            for (var i = 0; i < concLevel; i++)
            {
                var vals = new double[mutations];
                for (var j = 0; j < mutations; j++)
                {
                    var v = random.NormDouble();
                    vals[j] = v;
                    allVars[i * mutations + j] = v;
                    sampleSum += v;
                }

                tasks.Add(Task.Run(() =>
                {
                    foreach (var v in vals)
                        sum.Observe(v);
                }));
            }

            Task.WaitAll(tasks.ToArray());

            Array.Sort(allVars);

            var m = sum.Collect().metric.Single().summary;

            Assert.That(m.sample_count, Is.EqualTo(mutations * concLevel));

            var got = m.sample_sum;
            var want = sampleSum;
            Assert.That(Math.Abs(got-want)/want, Is.LessThanOrEqualTo(0.001));

            var objectives = Summary.DefObjectives.Select(_ => _.Quantile).ToArray();
            Array.Sort(objectives);

            for (var i = 0; i < objectives.Length; i++)
            {
                var wantQ = Summary.DefObjectives.ElementAt(i);
                var epsilon = wantQ.Epsilon;
                var gotQ = m.quantile[i].quantile;
                var gotV = m.quantile[i].value;
                var minMax = GetBounds(allVars, wantQ.Quantile, epsilon);

                Assert.That(gotQ, Is.Not.NaN);
                Assert.That(gotV, Is.Not.NaN);
                Assert.That(minMax.Item1, Is.Not.NaN);
                Assert.That(minMax.Item2, Is.Not.NaN);

                Assert.That(gotQ, Is.EqualTo(wantQ.Quantile));
                Assert.That(gotV, Is.GreaterThanOrEqualTo(minMax.Item1));
                Assert.That(gotV, Is.LessThanOrEqualTo(minMax.Item2));
            }
        }