Search in sources :

Example 1 with TYPE

use of reactor.netty.Metrics.TYPE 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)

Aggregations

Gauge (io.micrometer.api.instrument.Gauge)1 MeterRegistry (io.micrometer.api.instrument.MeterRegistry)1 Metrics (io.micrometer.api.instrument.Metrics)1 SimpleMeterRegistry (io.micrometer.api.instrument.simple.SimpleMeterRegistry)1 ByteBuf (io.netty.buffer.ByteBuf)1 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)1 ChannelOption (io.netty.channel.ChannelOption)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 Function (java.util.function.Function)1 IntStream (java.util.stream.IntStream)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 AfterEach (org.junit.jupiter.api.AfterEach)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 Mono (reactor.core.publisher.Mono)1 BaseHttpTest (reactor.netty.BaseHttpTest)1