提交 678cab39 编写于 作者: C Chengyang He 提交者: Hu Zongtang

[ISSUE #1656] Fix StatsItem bug (#1704)

Fix bug: The stats data could be inaccurate of first minute/hour/day
上级 ad7622c1
......@@ -152,6 +152,9 @@ public class StatsItem {
public void samplingInSeconds() {
synchronized (this.csListMinute) {
if (this.csListMinute.size() == 0) {
this.csListMinute.add(new CallSnapshot(System.currentTimeMillis() - 10 * 1000, 0, 0));
}
this.csListMinute.add(new CallSnapshot(System.currentTimeMillis(), this.times.get(), this.value
.get()));
if (this.csListMinute.size() > 7) {
......@@ -162,6 +165,9 @@ public class StatsItem {
public void samplingInMinutes() {
synchronized (this.csListHour) {
if (this.csListHour.size() == 0) {
this.csListHour.add(new CallSnapshot(System.currentTimeMillis() - 10 * 60 * 1000, 0, 0));
}
this.csListHour.add(new CallSnapshot(System.currentTimeMillis(), this.times.get(), this.value
.get()));
if (this.csListHour.size() > 7) {
......@@ -172,6 +178,9 @@ public class StatsItem {
public void samplingInHour() {
synchronized (this.csListDay) {
if (this.csListDay.size() == 0) {
this.csListDay.add(new CallSnapshot(System.currentTimeMillis() - 1 * 60 * 60 * 1000, 0, 0));
}
this.csListDay.add(new CallSnapshot(System.currentTimeMillis(), this.times.get(), this.value
.get()));
if (this.csListDay.size() > 25) {
......
......@@ -44,6 +44,35 @@ public class StatsItemSetTest {
assertEquals(10, test_unit_moment().longValue());
}
@Test
public void test_statsOfFirstStatisticsCycle() throws InterruptedException {
final StatsItemSet statsItemSet = new StatsItemSet("topicTest", scheduler, null);
executor = new ThreadPoolExecutor(10, 20, 10, TimeUnit.SECONDS,
new ArrayBlockingQueue<Runnable>(100), new ThreadFactoryImpl("testMultiThread"));
for (int i = 0; i < 10; i++) {
executor.submit(new Runnable() {
@Override
public void run() {
statsItemSet.addValue("topicTest", 2, 1);
}
});
}
while (true) {
if (executor.getCompletedTaskCount() == 10) {
break;
}
Thread.sleep(1000);
}
// simulate schedule task execution
statsItemSet.getStatsItem("topicTest").samplingInSeconds();
statsItemSet.getStatsItem("topicTest").samplingInMinutes();
statsItemSet.getStatsItem("topicTest").samplingInHour();
assertEquals(20L, statsItemSet.getStatsDataInMinute("topicTest").getSum());
assertEquals(20L, statsItemSet.getStatsDataInHour("topicTest").getSum());
assertEquals(20L, statsItemSet.getStatsDataInDay("topicTest").getSum());
}
private AtomicLong test_unit() throws InterruptedException {
final StatsItemSet statsItemSet = new StatsItemSet("topicTest", scheduler, null);
executor = new ThreadPoolExecutor(10, 20, 10, TimeUnit.SECONDS,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册