Search in sources :

Example 1 with GridCircularBuffer

use of org.apache.ignite.internal.util.GridCircularBuffer in project ignite by apache.

the class GridCircularBufferSelfTest method testSingleThreaded.

/**
     * @throws Exception If failed.
     */
public void testSingleThreaded() throws Exception {
    int size = 8;
    int iterCnt = size * 10;
    GridCircularBuffer<Integer> buf = new GridCircularBuffer<>(size);
    info("Created buffer: " + buf);
    Integer lastEvicted = null;
    for (int i = 0; i < iterCnt; i++) {
        Integer evicted = buf.add(i);
        info("Evicted: " + evicted);
        if (i >= size) {
            assert evicted != null;
            if (lastEvicted == null) {
                lastEvicted = evicted;
                continue;
            }
            assert lastEvicted + 1 == evicted : "Fail [lastEvicted=" + lastEvicted + ", evicted=" + evicted + ']';
            lastEvicted = evicted;
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCircularBuffer(org.apache.ignite.internal.util.GridCircularBuffer)

Example 2 with GridCircularBuffer

use of org.apache.ignite.internal.util.GridCircularBuffer in project ignite by apache.

the class GridCircularBufferSelfTest method testMutliThreaded.

/**
     * @throws Exception If failed.
     */
public void testMutliThreaded() throws Exception {
    int size = 32 * 1024;
    final GridCircularBuffer<Integer> buf = new GridCircularBuffer<>(size);
    final AtomicInteger itemGen = new AtomicInteger();
    info("Created buffer: " + buf);
    final int iterCnt = 1_000_000;
    multithreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            for (int i = 0; i < iterCnt; i++) {
                int item = itemGen.getAndIncrement();
                buf.add(item);
            }
            return null;
        }
    }, 32);
    info("Buffer: " + buf);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCircularBuffer(org.apache.ignite.internal.util.GridCircularBuffer)

Example 3 with GridCircularBuffer

use of org.apache.ignite.internal.util.GridCircularBuffer in project ignite by apache.

the class GridCircularBufferSelfTest method testMutliThreaded2.

/**
     * @throws Exception If failed.
     */
public void testMutliThreaded2() throws Exception {
    int size = 256 * 1024;
    final GridCircularBuffer<Integer> buf = new GridCircularBuffer<>(size);
    final AtomicInteger itemGen = new AtomicInteger();
    info("Created buffer: " + buf);
    final int iterCnt = 10_000;
    final ConcurrentLinkedDeque8<Integer> evictedQ = new ConcurrentLinkedDeque8<>();
    final ConcurrentLinkedDeque8<Integer> putQ = new ConcurrentLinkedDeque8<>();
    multithreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            for (int i = 0; i < iterCnt; i++) {
                int item = itemGen.getAndIncrement();
                putQ.add(item);
                Integer evicted = buf.add(item);
                if (evicted != null)
                    evictedQ.add(evicted);
            }
            return null;
        }
    }, 8);
    evictedQ.addAll(buf.items());
    assert putQ.containsAll(evictedQ);
    assert evictedQ.sizex() == putQ.sizex();
    info("Buffer: " + buf);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentLinkedDeque8(org.jsr166.ConcurrentLinkedDeque8) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCircularBuffer(org.apache.ignite.internal.util.GridCircularBuffer)

Example 4 with GridCircularBuffer

use of org.apache.ignite.internal.util.GridCircularBuffer in project ignite by apache.

the class GridCircularBufferPerformanceTest method testThroughput.

/**
     * @throws Exception If failed.
     */
public void testThroughput() throws Exception {
    int size = 256 * 1024;
    final GridCircularBuffer<Integer> buf = new GridCircularBuffer<>(size);
    final LongAdder8 cnt = new LongAdder8();
    final AtomicBoolean finished = new AtomicBoolean();
    multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finished.get()) {
                U.sleep(5000);
                info("Ops/sec: " + cnt.sumThenReset() / 5);
            }
            return null;
        }
    }, 1);
    multithreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finished.get()) {
                buf.add(1);
                cnt.increment();
            }
            return null;
        }
    }, 8);
    info("Buffer: " + buf);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridCircularBuffer(org.apache.ignite.internal.util.GridCircularBuffer) LongAdder8(org.jsr166.LongAdder8)

Aggregations

GridCircularBuffer (org.apache.ignite.internal.util.GridCircularBuffer)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 ConcurrentLinkedDeque8 (org.jsr166.ConcurrentLinkedDeque8)1 LongAdder8 (org.jsr166.LongAdder8)1