use of org.apache.cassandra.utils.concurrent.Ref.Visitor in project cassandra by apache.
the class RefCountedTest method testHashMap.
@Test
public void testHashMap() {
final Map<Object, Object> map = new HashMap<Object, Object>();
RefCounted.Tidy tidier = new RefCounted.Tidy() {
Object ref = map;
@Override
public void tidy() throws Exception {
}
@Override
public String name() {
return "42";
}
};
Ref<Object> ref = new Ref(new AtomicReference<Map<Object, Object>>(map), tidier);
Object o = new Object();
for (int i = 0; i < entryCount; i++) {
map.put(new Object(), o);
}
Visitor visitor = new Visitor();
visitor.run();
ref.close();
System.out.println("HashMap visited " + visitor.lastVisitedCount + " iterations " + visitor.iterations);
//Should visit 2x the number of entries because of the wrapper Map.Entry objects
Assert.assertTrue(visitor.lastVisitedCount > (entryCount * 2) && visitor.lastVisitedCount < (entryCount * 2) + fudgeFactor);
//Should iterate 3x the number of entries since we have to traverse the key and value separately
Assert.assertTrue(visitor.iterations > (entryCount * 3) && visitor.iterations < (entryCount * 3) + fudgeFactor);
}
use of org.apache.cassandra.utils.concurrent.Ref.Visitor in project cassandra by apache.
the class RefCountedTest method testLinkedList.
@Test
public void testLinkedList() {
final List<Object> iterable = new LinkedList<Object>();
Pair<Object, Object> p = Pair.create(iterable, iterable);
RefCounted.Tidy tidier = new RefCounted.Tidy() {
Object ref = iterable;
@Override
public void tidy() throws Exception {
}
@Override
public String name() {
return "42";
}
};
Ref<Object> ref = new Ref(new AtomicReference<List<Object>>(iterable), tidier);
for (int i = 0; i < entryCount; i++) {
iterable.add(p);
}
Visitor visitor = new Visitor();
visitor.run();
ref.close();
System.out.println("LinkedList visited " + visitor.lastVisitedCount + " iterations " + visitor.iterations);
//Should visit a lot of list nodes, but no more since there is only one object stored in the list
Assert.assertTrue(visitor.lastVisitedCount > entryCount && visitor.lastVisitedCount < entryCount + fudgeFactor);
//Should have a lot of iterations to walk the list, but linear to the number of entries
Assert.assertTrue(visitor.iterations > (entryCount * 3) && visitor.iterations < (entryCount * 3) + fudgeFactor);
}
use of org.apache.cassandra.utils.concurrent.Ref.Visitor in project cassandra by apache.
the class RefCountedTest method testConcurrentLinkedQueueImpl.
private void testConcurrentLinkedQueueImpl(boolean bugTest) {
final Queue<Object> iterable = new ConcurrentLinkedQueue<Object>();
Pair<Object, Object> p = Pair.create(iterable, iterable);
RefCounted.Tidy tidier = new RefCounted.Tidy() {
Object ref = iterable;
@Override
public void tidy() throws Exception {
}
@Override
public String name() {
return "42";
}
};
Ref<Object> ref = new Ref(new AtomicReference<Queue<Object>>(iterable), tidier);
for (int i = 0; i < entryCount; i++) {
iterable.add(p);
}
Visitor visitor = new Visitor();
visitor.run();
ref.close();
System.out.println("ConcurrentLinkedQueue visited " + visitor.lastVisitedCount + " iterations " + visitor.iterations + " bug test " + bugTest);
if (bugTest) {
//Should have to visit a lot of queue nodes
Assert.assertTrue(visitor.lastVisitedCount > entryCount && visitor.lastVisitedCount < entryCount + fudgeFactor);
//Should have a lot of iterations to walk the queue, but linear to the number of entries
Assert.assertTrue(visitor.iterations > (entryCount * 2) && visitor.iterations < (entryCount * 2) + fudgeFactor);
} else {
//There are almost no objects in this linked list once it's iterated as a collection so visited count
//should be small
Assert.assertTrue(visitor.lastVisitedCount < 10);
//Should have a lot of iterations to walk the collection, but linear to the number of entries
Assert.assertTrue(visitor.iterations > entryCount && visitor.iterations < entryCount + fudgeFactor);
}
}
use of org.apache.cassandra.utils.concurrent.Ref.Visitor in project cassandra by apache.
the class RefCountedTest method testConcurrentMap.
@Test
public void testConcurrentMap() {
final Map<Object, Object> map = new ConcurrentHashMap<Object, Object>();
RefCounted.Tidy tidier = new RefCounted.Tidy() {
Object ref = map;
@Override
public void tidy() throws Exception {
}
@Override
public String name() {
return "42";
}
};
Ref<Object> ref = new Ref(new AtomicReference<Map<Object, Object>>(map), tidier);
Object o = new Object();
for (int i = 0; i < entryCount; i++) {
map.put(new Object(), o);
}
Visitor visitor = new Visitor();
visitor.run();
ref.close();
System.out.println("ConcurrentHashMap visited " + visitor.lastVisitedCount + " iterations " + visitor.iterations);
//Should visit roughly the same number of objects as entries because the value object is constant
//Map.Entry objects shouldn't be counted since it is iterated as a collection
Assert.assertTrue(visitor.lastVisitedCount > entryCount && visitor.lastVisitedCount < entryCount + fudgeFactor);
//Should visit 2x the number of entries since we have to traverse the key and value separately
Assert.assertTrue(visitor.iterations > entryCount * 2 && visitor.iterations < entryCount * 2 + fudgeFactor);
}
use of org.apache.cassandra.utils.concurrent.Ref.Visitor in project cassandra by apache.
the class RefCountedTest method testBlockingQueue.
@Test
public void testBlockingQueue() {
final BlockingQueue<Object> iterable = new LinkedBlockingQueue<Object>();
Pair<Object, Object> p = Pair.create(iterable, iterable);
RefCounted.Tidy tidier = new RefCounted.Tidy() {
Object ref = iterable;
@Override
public void tidy() throws Exception {
}
@Override
public String name() {
return "42";
}
};
Ref<Object> ref = new Ref(new AtomicReference<BlockingQueue<Object>>(iterable), tidier);
for (int i = 0; i < entryCount; i++) {
iterable.add(p);
}
Visitor visitor = new Visitor();
visitor.run();
ref.close();
System.out.println("BlockingQueue visited " + visitor.lastVisitedCount + " iterations " + visitor.iterations);
//There are almost no objects in this queue once it's iterated as a collection so visited count
//should be small
Assert.assertTrue(visitor.lastVisitedCount < 10);
//Should have a lot of iterations to walk the collection, but linear to the number of entries
Assert.assertTrue(visitor.iterations > entryCount && visitor.iterations < entryCount + fudgeFactor);
}
Aggregations