use of io.netty.buffer.PooledByteBufAllocator in project turbo-rpc by hank-whu.
the class KryoByteBufTest method main.
public static void main(String[] args) {
Kryo kryo = new Kryo();
// kryo.setWarnUnregisteredClasses(true);
// kryo.setReferences(false);
kryo.register(IntObj.class);
kryo.register(StringUTF8Obj.class);
kryo.register(LocalDateTimeObj.class);
kryo.register(ComplexObj.class);
kryo.register(int[].class);
kryo.register(ArrayList.class);
kryo.register(ArrayListObj.class);
ByteBufAllocator allocator = new PooledByteBufAllocator(true);
ByteBuf buffer = allocator.directBuffer(2, 1024 * 1024 * 8);
System.out.println("buffer.nioBufferCount: " + buffer.nioBufferCount());
System.out.println(buffer.nioBuffer(0, buffer.capacity()));
ByteBufOutput output = new ByteBufOutput(buffer);
UserService userService = new UserServiceServerImpl();
User user = userService.getUser(Long.MAX_VALUE).join();
while (true) {
buffer.clear();
output.setBuffer(buffer);
kryo.writeObject(output, user);
ByteBufInput input = new ByteBufInput(buffer);
User u = kryo.readObject(input, User.class);
System.out.println(u);
}
// buffer.clear();
// output.setBuffer(buffer);
// kryo.writeObject(output, user);
// System.out.println("user writeObject: " + output.total());
//
// ByteBufInput input = new ByteBufInput(buffer);
// System.out.println("user readableBytes: " + buffer.readableBytes());
// System.out.println("user readerIndex: " + buffer.readerIndex());
// System.out.println(kryo.readObject(input, User.class));
}
use of io.netty.buffer.PooledByteBufAllocator in project reactor-netty by reactor.
the class ByteBufAllocatorMetricsTest method test.
@Test
void test() throws Exception {
disposableServer = createServer().handle((req, res) -> res.header("Connection", "close").sendString(Mono.just("test"))).bindNow();
CountDownLatch latch = new CountDownLatch(1);
PooledByteBufAllocator alloc = new PooledByteBufAllocator(true);
createClient(disposableServer.port()).option(ChannelOption.ALLOCATOR, alloc).doOnResponse((res, conn) -> conn.channel().closeFuture().addListener(f -> latch.countDown())).metrics(true, Function.identity()).get().uri("/").responseContent().aggregate().asString().block(Duration.ofSeconds(30));
assertThat(latch.await(30, TimeUnit.SECONDS)).as("latch await").isTrue();
String id = alloc.metric().hashCode() + "";
String[] tags = new String[] { ID, id, TYPE, "pooled" };
checkExpectations(BYTE_BUF_ALLOCATOR_PREFIX, tags);
// Verify ACTIVE_HEAP_MEMORY and ACTIVE_DIRECT_MEMORY meters
List<ByteBuf> buffers = new ArrayList<>();
boolean releaseBufList = true;
try {
double currentActiveHeap = getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_HEAP_MEMORY, tags);
double currentActiveDirect = getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_DIRECT_MEMORY, tags);
IntStream.range(0, 10).mapToObj(i -> alloc.heapBuffer(102400)).forEach(buffers::add);
IntStream.range(0, 10).mapToObj(i -> alloc.directBuffer(102400)).forEach(buffers::add);
assertThat(getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_HEAP_MEMORY, tags)).isGreaterThan(currentActiveHeap);
assertThat(getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_DIRECT_MEMORY, tags)).isGreaterThan(currentActiveDirect);
currentActiveHeap = getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_HEAP_MEMORY, tags);
currentActiveDirect = getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_DIRECT_MEMORY, tags);
buffers.forEach(ByteBuf::release);
releaseBufList = false;
assertThat(getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_HEAP_MEMORY, tags)).isLessThan(currentActiveHeap);
assertThat(getGaugeValue(BYTE_BUF_ALLOCATOR_PREFIX + ACTIVE_DIRECT_MEMORY, tags)).isLessThan(currentActiveDirect);
} finally {
if (releaseBufList) {
buffers.forEach(ByteBuf::release);
}
}
}
use of io.netty.buffer.PooledByteBufAllocator in project x-pipe by ctripcorp.
the class NettyClientFactoryTest method testMakeObject.
@Test
public void testMakeObject() throws Exception {
PooledObject<NettyClient> pooledObject = factory.makeObject();
NettyClient client = pooledObject.getObject();
PooledByteBufAllocator allocator = (PooledByteBufAllocator) client.channel().alloc();
AtomicReference<Object> freeSweepAllocationThreshold = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
client.channel().eventLoop().execute(new Runnable() {
@Override
public void run() {
try {
logger.info("[thread] {}", Thread.currentThread().getClass());
Field field = FieldUtils.getDeclaredField(allocator.getClass(), "threadCache", true);
Object threadCache = field.get(allocator);
logger.info("[class]{}", threadCache.getClass());
FastThreadLocal fastThreadLocal = (FastThreadLocal) threadCache;
Object poolThreadCache = fastThreadLocal.get();
logger.info("[class]{}", poolThreadCache.getClass());
Field param = FieldUtils.getField(poolThreadCache.getClass(), "freeSweepAllocationThreshold", true);
logger.info("[{}]", param.get(poolThreadCache));
freeSweepAllocationThreshold.set(param.get(poolThreadCache));
} catch (IllegalAccessException e) {
logger.error("", e);
}
latch.countDown();
}
});
latch.await();
Assert.assertNotNull(freeSweepAllocationThreshold.get());
Assert.assertEquals(0, freeSweepAllocationThreshold.get());
}
use of io.netty.buffer.PooledByteBufAllocator in project x-pipe by ctripcorp.
the class DirectByteBufInStringOutPayloadTest method testContinuouslyInput.
@Test
public void testContinuouslyInput() throws Exception {
DirectByteBufInStringOutPayload payload = new DirectByteBufInStringOutPayload();
StringBuilder randomStr = new StringBuilder();
payload.startInput();
ByteBufAllocator allocator = new PooledByteBufAllocator();
for (int i = 0; i < 100; i++) {
String delta = randomString(100);
ByteBuf byteBuf = allocator.directBuffer(delta.length());
byteBuf.writeBytes(delta.getBytes());
randomStr.append(delta);
payload.in(byteBuf);
// byteBuf.release();
}
payload.endInput();
Assert.assertEquals(randomStr.toString(), payload.toString());
sleep(5000);
}
use of io.netty.buffer.PooledByteBufAllocator in project x-pipe by ctripcorp.
the class NettySimpleTest method testNettyInternalBuffer.
@Test
public void testNettyInternalBuffer() throws IOException {
ByteBufAllocator allocator = new PooledByteBufAllocator(true);
final ByteBuf byteBuf = allocator.buffer(1 << 10);
byteBuf.writeBytes("1234567890".getBytes());
System.out.println(byteBuf.readableBytes());
scheduled.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
// ByteBuffer byteBuffer = byteBuf.internalNioBuffer(0, byteBuf.readableBytes());
byteBuf.nioBuffers();
}
}, 0, 100, TimeUnit.MICROSECONDS);
System.out.println(byteBuf.readableBytes());
waitForAnyKeyToExit();
}
Aggregations