From 6525b7eef07ebed850996632bbd570162360a6ef Mon Sep 17 00:00:00 2001 From: "koo.taejin" Date: Fri, 15 Jul 2016 15:41:09 +0900 Subject: [PATCH] added to insert a number of agent into rdb periodically. #1883 add memoryDao and test --- .../dao/memory/MemoryAgentStatisticsDao.java | 65 +++++++++++++++++++ .../memory/MemoryAgentStatisticsDaoTest.java | 62 ++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 web/src/main/java/com/navercorp/pinpoint/web/dao/memory/MemoryAgentStatisticsDao.java create mode 100644 web/src/test/java/com/navercorp/pinpoint/web/dao/memory/MemoryAgentStatisticsDaoTest.java diff --git a/web/src/main/java/com/navercorp/pinpoint/web/dao/memory/MemoryAgentStatisticsDao.java b/web/src/main/java/com/navercorp/pinpoint/web/dao/memory/MemoryAgentStatisticsDao.java new file mode 100644 index 000000000000..1c41e185741a --- /dev/null +++ b/web/src/main/java/com/navercorp/pinpoint/web/dao/memory/MemoryAgentStatisticsDao.java @@ -0,0 +1,65 @@ +package com.navercorp.pinpoint.web.dao.memory; + +import com.navercorp.pinpoint.web.dao.AgentStatisticsDao; +import com.navercorp.pinpoint.web.vo.AgentCountStatistics; +import com.navercorp.pinpoint.web.vo.Range; +import org.springframework.stereotype.Repository; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +/** + * @author Taejin Koo + */ +@Repository +public class MemoryAgentStatisticsDao implements AgentStatisticsDao { + + private Map agentCountPerTime = new TreeMap<>(new LongComparator()); + + @Override + public boolean insertAgentCount(AgentCountStatistics agentCountStatistics) { + agentCountPerTime.put(agentCountStatistics.getTimestamp(), agentCountStatistics.getAgentCount()); + return true; + } + + @Override + public List selectAgentCount(Range range) { + Long to = range.getTo(); + long from = range.getFrom(); + + List result = new ArrayList<>(); + + Iterator> iterator = agentCountPerTime.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry next = iterator.next(); + + Long key = next.getKey(); + if (key > to) { + continue; + } + if (key < from) { + break; + } + + result.add(new AgentCountStatistics(next.getValue(), key)); + } + + return result; + } + + private static class LongComparator implements Comparator { + + @Override + public int compare(Long o1, Long o2) { + int compare = Long.compare(o2, o1); + + // if same then overwrite. + return compare; + } + } + +} diff --git a/web/src/test/java/com/navercorp/pinpoint/web/dao/memory/MemoryAgentStatisticsDaoTest.java b/web/src/test/java/com/navercorp/pinpoint/web/dao/memory/MemoryAgentStatisticsDaoTest.java new file mode 100644 index 000000000000..3ffb3b9f7b23 --- /dev/null +++ b/web/src/test/java/com/navercorp/pinpoint/web/dao/memory/MemoryAgentStatisticsDaoTest.java @@ -0,0 +1,62 @@ +package com.navercorp.pinpoint.web.dao.memory; + +import com.navercorp.pinpoint.web.vo.AgentCountStatistics; +import com.navercorp.pinpoint.web.vo.Range; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ThreadLocalRandom; + +/** + * @author Taejin Koo + */ +public class MemoryAgentStatisticsDaoTest { + + private static List testDataList; + + @BeforeClass + public static void setup() { + testDataList = createTestData(100); + } + + private static List createTestData(int size) { + List data = new ArrayList<>(size); + + for (int i = 0; i < size; i++) { + AgentCountStatistics agentCountStatistics = new AgentCountStatistics(ThreadLocalRandom.current().nextInt(0, Integer.MAX_VALUE), (i * 100) + 100); + data.add(agentCountStatistics); + } + + return data; + } + + @Test + public void simpleTest() throws Exception { + MemoryAgentStatisticsDao dao = new MemoryAgentStatisticsDao(); + for (AgentCountStatistics testData : testDataList) { + dao.insertAgentCount(testData); + } + + Range range = new Range(660L, 1320L); + List agentCountStatisticses = dao.selectAgentCount(range); + Assert.assertEquals(7, agentCountStatisticses.size()); + + + range = new Range(7100L, System.currentTimeMillis()); + agentCountStatisticses = dao.selectAgentCount(range); + Assert.assertEquals(30, agentCountStatisticses.size()); + + range = new Range(0L, System.currentTimeMillis()); + agentCountStatisticses = dao.selectAgentCount(range); + Assert.assertEquals(100, agentCountStatisticses.size()); + + long currentTime = System.currentTimeMillis(); + range = new Range(currentTime, currentTime + 100); + agentCountStatisticses = dao.selectAgentCount(range); + Assert.assertEquals(0, agentCountStatisticses.size()); + } + +}