Search in sources :

Example 1 with Visitor

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);
}
Also used : Visitor(org.apache.cassandra.utils.concurrent.Ref.Visitor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with Visitor

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);
}
Also used : Visitor(org.apache.cassandra.utils.concurrent.Ref.Visitor) LinkedList(java.util.LinkedList) List(java.util.List) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with Visitor

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);
    }
}
Also used : Visitor(org.apache.cassandra.utils.concurrent.Ref.Visitor) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 4 with Visitor

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);
}
Also used : Visitor(org.apache.cassandra.utils.concurrent.Ref.Visitor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with Visitor

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);
}
Also used : BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Visitor(org.apache.cassandra.utils.concurrent.Ref.Visitor) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Test(org.junit.Test)

Aggregations

Visitor (org.apache.cassandra.utils.concurrent.Ref.Visitor)7 Test (org.junit.Test)6 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 File (java.io.File)1 WeakReference (java.lang.ref.WeakReference)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Queue (java.util.Queue)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1