public virtual void TestTops() { long time = WindowLenMs + BucketLen * 3 / 2; for (int i = 0; i < users.Length; i++) { manager.RecordMetric(time, "open", users[i], (i + 1) * 2); } time++; for (int i_1 = 0; i_1 < users.Length; i_1++) { manager.RecordMetric(time, "close", users[i_1], i_1 + 1); } time++; RollingWindowManager.TopWindow tops = manager.Snapshot(time); NUnit.Framework.Assert.AreEqual("Unexpected number of ops", 2, tops.GetOps().Count ); foreach (RollingWindowManager.OP op in tops.GetOps()) { IList <RollingWindowManager.User> topUsers = op.GetTopUsers(); NUnit.Framework.Assert.AreEqual("Unexpected number of users", NTopUsers, topUsers .Count); if (op.GetOpType() == "open") { for (int i_2 = 0; i_2 < topUsers.Count; i_2++) { RollingWindowManager.User user = topUsers[i_2]; NUnit.Framework.Assert.AreEqual("Unexpected count for user " + user.GetUser(), (users .Length - i_2) * 2, user.GetCount()); } // Closed form of sum(range(2,42,2)) NUnit.Framework.Assert.AreEqual("Unexpected total count for op", (2 + (users.Length * 2)) * (users.Length / 2), op.GetTotalCount()); } } // move the window forward not to see the "open" results time += WindowLenMs - 2; tops = manager.Snapshot(time); NUnit.Framework.Assert.AreEqual("Unexpected number of ops", 1, tops.GetOps().Count ); RollingWindowManager.OP op_1 = tops.GetOps()[0]; NUnit.Framework.Assert.AreEqual("Should only see close ops", "close", op_1.GetOpType ()); IList <RollingWindowManager.User> topUsers_1 = op_1.GetTopUsers(); for (int i_3 = 0; i_3 < topUsers_1.Count; i_3++) { RollingWindowManager.User user = topUsers_1[i_3]; NUnit.Framework.Assert.AreEqual("Unexpected count for user " + user.GetUser(), (users .Length - i_3), user.GetCount()); } // Closed form of sum(range(1,21)) NUnit.Framework.Assert.AreEqual("Unexpected total count for op", (1 + users.Length ) * (users.Length / 2), op_1.GetTotalCount()); }
/// <summary>Take a snapshot of current top users in the past period.</summary> /// <param name="time">the current time</param> /// <returns> /// a TopWindow describing the top users for each metric in the /// window. /// </returns> public virtual RollingWindowManager.TopWindow Snapshot(long time) { RollingWindowManager.TopWindow window = new RollingWindowManager.TopWindow(windowLenMs ); if (Log.IsDebugEnabled()) { ICollection <string> metricNames = metricMap.Keys; Log.Debug("iterating in reported metrics, size={} values={}", metricNames.Count, metricNames); } foreach (KeyValuePair <string, RollingWindowManager.RollingWindowMap> entry in metricMap) { string metricName = entry.Key; RollingWindowManager.RollingWindowMap rollingWindows = entry.Value; RollingWindowManager.TopN topN = GetTopUsersForMetric(time, metricName, rollingWindows ); int size = topN.Count; if (size == 0) { continue; } RollingWindowManager.OP op = new RollingWindowManager.OP(metricName, topN.GetTotal ()); window.AddOp(op); // Reverse the users from the TopUsers using a stack, // since we'd like them sorted in descending rather than ascending order Stack <RollingWindowManager.NameValuePair> reverse = new Stack <RollingWindowManager.NameValuePair >(); for (int i = 0; i < size; i++) { reverse.Push(topN.Poll()); } for (int i_1 = 0; i_1 < size; i_1++) { RollingWindowManager.NameValuePair userEntry = reverse.Pop(); RollingWindowManager.User user = new RollingWindowManager.User(userEntry.name, Sharpen.Extensions.ValueOf (userEntry.value)); op.AddUser(user); } } return(window); }
public virtual void AddUser(RollingWindowManager.User u) { topUsers.AddItem(u); }