Search in sources :

Example 56 with PooledByteBufAllocator

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));
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) UserServiceServerImpl(rpc.turbo.benchmark.service.UserServiceServerImpl) User(rpc.turbo.benchmark.bean.User) ByteBufOutput(rpc.turbo.serialization.kryo.ByteBufOutput) UserService(rpc.turbo.benchmark.service.UserService) ByteBufInput(rpc.turbo.serialization.kryo.ByteBufInput) ByteBuf(io.netty.buffer.ByteBuf) Kryo(com.esotericsoftware.kryo.Kryo) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator)

Example 57 with PooledByteBufAllocator

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);
        }
    }
}
Also used : IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) SimpleMeterRegistry(io.micrometer.api.instrument.simple.SimpleMeterRegistry) ChannelOption(io.netty.channel.ChannelOption) USED_DIRECT_MEMORY(reactor.netty.Metrics.USED_DIRECT_MEMORY) CHUNK_SIZE(reactor.netty.Metrics.CHUNK_SIZE) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BaseHttpTest(reactor.netty.BaseHttpTest) THREAD_LOCAL_CACHES(reactor.netty.Metrics.THREAD_LOCAL_CACHES) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ACTIVE_HEAP_MEMORY(reactor.netty.Metrics.ACTIVE_HEAP_MEMORY) ByteBuf(io.netty.buffer.ByteBuf) Duration(java.time.Duration) DIRECT_ARENAS(reactor.netty.Metrics.DIRECT_ARENAS) BYTE_BUF_ALLOCATOR_PREFIX(reactor.netty.Metrics.BYTE_BUF_ALLOCATOR_PREFIX) HEAP_ARENAS(reactor.netty.Metrics.HEAP_ARENAS) Mono(reactor.core.publisher.Mono) USED_HEAP_MEMORY(reactor.netty.Metrics.USED_HEAP_MEMORY) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) ID(reactor.netty.Metrics.ID) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) NORMAL_CACHE_SIZE(reactor.netty.Metrics.NORMAL_CACHE_SIZE) SMALL_CACHE_SIZE(reactor.netty.Metrics.SMALL_CACHE_SIZE) TYPE(reactor.netty.Metrics.TYPE) MeterRegistry(io.micrometer.api.instrument.MeterRegistry) ACTIVE_DIRECT_MEMORY(reactor.netty.Metrics.ACTIVE_DIRECT_MEMORY) Metrics(io.micrometer.api.instrument.Metrics) Gauge(io.micrometer.api.instrument.Gauge) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) BaseHttpTest(reactor.netty.BaseHttpTest) Test(org.junit.jupiter.api.Test)

Example 58 with PooledByteBufAllocator

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());
}
Also used : Field(java.lang.reflect.Field) PooledObject(org.apache.commons.pool2.PooledObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) FastThreadLocal(io.netty.util.concurrent.FastThreadLocal) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Example 59 with PooledByteBufAllocator

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);
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Example 60 with PooledByteBufAllocator

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();
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) ByteBuf(io.netty.buffer.ByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator) Test(org.junit.Test) AbstractTest(com.ctrip.xpipe.AbstractTest)

Aggregations

PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)60 Test (org.junit.Test)37 LocalAddress (io.netty.channel.local.LocalAddress)29 ByteBuf (io.netty.buffer.ByteBuf)28 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)25 Subscription (rx.Subscription)25 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)24 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)24 DisposableWrapper (org.jocean.idiom.DisposableWrapper)20 IOException (java.io.IOException)18 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)17 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)17 ConnectException (java.net.ConnectException)17 CertificateException (java.security.cert.CertificateException)17 SSLException (javax.net.ssl.SSLException)17 TransportException (org.jocean.http.TransportException)17 TestSubscriber (rx.observers.TestSubscriber)16 Channel (io.netty.channel.Channel)9 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)9 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)8