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));
}
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();
}
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);
}
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;
}
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;
}
Aggregations