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