Skip to content

Commit

Permalink
Merge pull request #1911 from koo-taejin/#1883
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
koo-taejin authored Jul 15, 2016
2 parents 21cd723 + 6525b7e commit 385fb1c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
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;
}
}

}
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());
}

}

0 comments on commit 385fb1c

Please # to comment.