-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
added to insert a number of agent into rdb periodically. #1883
- Loading branch information
Showing
2 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
web/src/main/java/com/navercorp/pinpoint/web/dao/memory/MemoryAgentStatisticsDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Long, Integer> agentCountPerTime = new TreeMap<>(new LongComparator()); | ||
|
||
@Override | ||
public boolean insertAgentCount(AgentCountStatistics agentCountStatistics) { | ||
agentCountPerTime.put(agentCountStatistics.getTimestamp(), agentCountStatistics.getAgentCount()); | ||
return true; | ||
} | ||
|
||
@Override | ||
public List<AgentCountStatistics> selectAgentCount(Range range) { | ||
Long to = range.getTo(); | ||
long from = range.getFrom(); | ||
|
||
List<AgentCountStatistics> result = new ArrayList<>(); | ||
|
||
Iterator<Map.Entry<Long, Integer>> iterator = agentCountPerTime.entrySet().iterator(); | ||
while (iterator.hasNext()) { | ||
Map.Entry<Long, Integer> 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<Long> { | ||
|
||
@Override | ||
public int compare(Long o1, Long o2) { | ||
int compare = Long.compare(o2, o1); | ||
|
||
// if same then overwrite. | ||
return compare; | ||
} | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
web/src/test/java/com/navercorp/pinpoint/web/dao/memory/MemoryAgentStatisticsDaoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<AgentCountStatistics> testDataList; | ||
|
||
@BeforeClass | ||
public static void setup() { | ||
testDataList = createTestData(100); | ||
} | ||
|
||
private static List<AgentCountStatistics> createTestData(int size) { | ||
List<AgentCountStatistics> 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<AgentCountStatistics> 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()); | ||
} | ||
|
||
} |