Search in sources :

Example 1 with PoolState

use of com.firenio.buffer.PooledByteBufAllocator.PoolState in project baseio by generallycloud.

the class TestPooledBytebuf method testAlloc.

@Test
public void testAlloc() throws Exception {
    DevelopConfig.BUF_DEBUG = true;
    LoggerFactory.setEnableSLF4JLogger(false);
    final AtomicInteger alloc_single_times = new AtomicInteger();
    final AtomicInteger alloc_unpooled_times = new AtomicInteger();
    final int core = Math.max(4, Util.availableProcessors());
    final PooledByteBufAllocator a = TestAllocUtil.heap(1024 * 1024 * 4 * core);
    CountDownLatch c = new CountDownLatch(core);
    for (int i = 0; i < core; i++) {
        Util.exec(() -> {
            Random r = new Random();
            Queue<ByteBuf> bufs = new LinkedList<>();
            for (int j = 0; j < 1024 * 1024; j++) {
                int pa = r.nextInt(256);
                if (pa == 0) {
                    pa = 1;
                    alloc_single_times.getAndIncrement();
                }
                ByteBuf buf = a.allocate(pa);
                if (!buf.isPooled()) {
                    alloc_unpooled_times.getAndIncrement();
                }
                if ((pa & 1) == 1) {
                    buf.release();
                } else {
                    bufs.offer(buf);
                }
                if (bufs.size() > 256) {
                    ByteBuf buf1 = bufs.poll();
                    buf1.release();
                }
            }
            for (ByteBuf buf : bufs) {
                buf.release();
            }
            c.countDown();
        });
    }
    c.await();
    PoolState s = a.getState();
    System.out.println("alloc_single_times: " + alloc_single_times.get());
    System.out.println("alloc_unpooled_times: " + alloc_unpooled_times.get());
    Assert.assertEquals(0, s.buf);
    Assert.assertEquals(s.free, s.memory);
    Assert.assertEquals(s.mfree, s.memory);
}
Also used : Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PoolState(com.firenio.buffer.PooledByteBufAllocator.PoolState) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(com.firenio.buffer.ByteBuf) LinkedList(java.util.LinkedList) PooledByteBufAllocator(com.firenio.buffer.PooledByteBufAllocator) Test(org.junit.Test)

Aggregations

ByteBuf (com.firenio.buffer.ByteBuf)1 PooledByteBufAllocator (com.firenio.buffer.PooledByteBufAllocator)1 PoolState (com.firenio.buffer.PooledByteBufAllocator.PoolState)1 LinkedList (java.util.LinkedList)1 Random (java.util.Random)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Test (org.junit.Test)1