예제 #1
0
        /// <summary>Calculates the top N users over a time interval.</summary>
        /// <param name="time">the current time</param>
        /// <param name="metricName">Name of metric</param>
        /// <returns/>
        private RollingWindowManager.TopN GetTopUsersForMetric(long time, string metricName
                                                               , RollingWindowManager.RollingWindowMap rollingWindows)
        {
            RollingWindowManager.TopN topN = new RollingWindowManager.TopN(topUsersCnt);
            IEnumerator <KeyValuePair <string, RollingWindow> > iterator = rollingWindows.GetEnumerator
                                                                               ();

            while (iterator.HasNext())
            {
                KeyValuePair <string, RollingWindow> entry = iterator.Next();
                string        userName  = entry.Key;
                RollingWindow aWindow   = entry.Value;
                long          windowSum = aWindow.GetSum(time);
                // do the gc here
                if (windowSum == 0)
                {
                    Log.Debug("gc window of metric: {} userName: {}", metricName, userName);
                    iterator.Remove();
                    continue;
                }
                Log.Debug("offer window of metric: {} userName: {} sum: {}", metricName, userName
                          , windowSum);
                topN.Offer(new RollingWindowManager.NameValuePair(userName, windowSum));
            }
            Log.Info("topN size for command {} is: {}", metricName, topN.Count);
            return(topN);
        }
예제 #2
0
        public virtual void TestReorderedAccess()
        {
            RollingWindow window = new RollingWindow(WindowLen, BucketCnt);
            long          time   = 2 * WindowLen + BucketLen * 3 / 2;

            window.IncAt(time, 5);
            time++;
            NUnit.Framework.Assert.AreEqual("The sum of rolling window does not reflect the recent update"
                                            , 5, window.GetSum(time));
            long reorderedTime = time - 2 * BucketLen;

            window.IncAt(reorderedTime, 6);
            NUnit.Framework.Assert.AreEqual("The sum of rolling window does not reflect the reordered update"
                                            , 11, window.GetSum(time));
            time = reorderedTime + WindowLen;
            NUnit.Framework.Assert.AreEqual("The sum of rolling window does not reflect rolling effect"
                                            , 5, window.GetSum(time));
        }
예제 #3
0
        public virtual void TestBasics()
        {
            RollingWindow window = new RollingWindow(WindowLen, BucketCnt);
            long          time   = 1;

            NUnit.Framework.Assert.AreEqual("The initial sum of rolling window must be 0", 0,
                                            window.GetSum(time));
            time = WindowLen + BucketLen * 3 / 2;
            NUnit.Framework.Assert.AreEqual("The initial sum of rolling window must be 0", 0,
                                            window.GetSum(time));
            window.IncAt(time, 5);
            NUnit.Framework.Assert.AreEqual("The sum of rolling window does not reflect the recent update"
                                            , 5, window.GetSum(time));
            time += BucketLen;
            window.IncAt(time, 6);
            NUnit.Framework.Assert.AreEqual("The sum of rolling window does not reflect the recent update"
                                            , 11, window.GetSum(time));
            time += WindowLen - BucketLen;
            NUnit.Framework.Assert.AreEqual("The sum of rolling window does not reflect rolling effect"
                                            , 6, window.GetSum(time));
            time += BucketLen;
            NUnit.Framework.Assert.AreEqual("The sum of rolling window does not reflect rolling effect"
                                            , 0, window.GetSum(time));
        }