Search in sources :

Example 16 with ByteBuf

use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.

the class TestExpansion method directPool.

@Test
public void directPool() throws Exception {
    ByteBufAllocator alloc = TestAllocUtil.direct(1024 * 4);
    ByteBuf buf = alloc.allocate(2);
    for (int i = 0; i < 10; i++) {
        buf.writeBytes(data);
        buf.writeByte(a);
        alloc.allocate(1);
    }
    Assert.assertTrue(v(buf));
}
Also used : PooledByteBufAllocator(com.firenio.buffer.PooledByteBufAllocator) ByteBufAllocator(com.firenio.buffer.ByteBufAllocator) ByteBuf(com.firenio.buffer.ByteBuf) Test(org.junit.Test)

Example 17 with ByteBuf

use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.

the class TestExpansion method testExpansion.

@Test
public void testExpansion() throws Exception {
    Options.setBufAutoExpansion(true);
    ByteBufAllocatorGroup g = new ByteBufAllocatorGroup(1, 8, 1, Unsafe.BUF_DIRECT);
    g.start();
    PooledByteBufAllocator alloc = g.getAllocator(0);
    ByteBuf buf1 = alloc.allocate(2);
    buf1.expansion(6);
    buf1.release();
    ByteBuf buf2 = alloc.allocate(2);
    buf2.expansion(4);
    buf2.release();
    Assert.assertTrue(alloc.getState().buf == 0);
    g.stop();
}
Also used : ByteBufAllocatorGroup(com.firenio.buffer.ByteBufAllocatorGroup) ByteBuf(com.firenio.buffer.ByteBuf) PooledByteBufAllocator(com.firenio.buffer.PooledByteBufAllocator) Test(org.junit.Test)

Example 18 with ByteBuf

use of com.firenio.buffer.ByteBuf 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)

Example 19 with ByteBuf

use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.

the class TestByteBufCopy method directGetDirect.

Object directGetDirect() {
    ByteBuf buf = _direct();
    directData().readBytes(buf);
    return buf;
}
Also used : ByteBuf(com.firenio.buffer.ByteBuf)

Example 20 with ByteBuf

use of com.firenio.buffer.ByteBuf in project baseio by generallycloud.

the class TestByteBufCopy method arrayGetDirect.

Object arrayGetDirect() {
    ByteBuf buf = _direct();
    arrayData().readBytes(buf);
    return buf;
}
Also used : ByteBuf(com.firenio.buffer.ByteBuf)

Aggregations

ByteBuf (com.firenio.buffer.ByteBuf)58 Test (org.junit.Test)18 ByteBufAllocator (com.firenio.buffer.ByteBufAllocator)5 Channel (com.firenio.component.Channel)5 PooledByteBufAllocator (com.firenio.buffer.PooledByteBufAllocator)4 Frame (com.firenio.component.Frame)3 IoEventHandle (com.firenio.component.IoEventHandle)3 NioEventLoopGroup (com.firenio.component.NioEventLoopGroup)3 LengthValueCodec (com.firenio.codec.lengthvalue.LengthValueCodec)2 ChannelAcceptor (com.firenio.component.ChannelAcceptor)2 ChannelConnector (com.firenio.component.ChannelConnector)2 LoggerChannelOpenListener (com.firenio.component.LoggerChannelOpenListener)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SSLEngine (javax.net.ssl.SSLEngine)2 SSLEngineResult (javax.net.ssl.SSLEngineResult)2 HandshakeStatus (javax.net.ssl.SSLEngineResult.HandshakeStatus)2 ByteBufAllocatorGroup (com.firenio.buffer.ByteBufAllocatorGroup)1 PoolState (com.firenio.buffer.PooledByteBufAllocator.PoolState)1