Search in sources :

Example 6 with MetricsOptions

use of io.vertx.core.metrics.MetricsOptions in project vert.x by eclipse.

the class MetricsContextTest method testHttpClientRequest.

private void testHttpClientRequest(Function<Vertx, Context> contextFactory, BiConsumer<Thread, Context> checker) throws Exception {
    AtomicReference<Thread> expectedThread = new AtomicReference<>();
    AtomicReference<Context> expectedContext = new AtomicReference<>();
    AtomicBoolean requestBeginCalled = new AtomicBoolean();
    AtomicBoolean responseEndCalled = new AtomicBoolean();
    AtomicBoolean socketConnectedCalled = new AtomicBoolean();
    AtomicBoolean socketDisconnectedCalled = new AtomicBoolean();
    AtomicBoolean bytesReadCalled = new AtomicBoolean();
    AtomicBoolean bytesWrittenCalled = new AtomicBoolean();
    AtomicBoolean closeCalled = new AtomicBoolean();
    VertxMetricsFactory factory = (vertx, options) -> new DummyVertxMetrics() {

        @Override
        public HttpClientMetrics createMetrics(HttpClient client, HttpClientOptions options) {
            return new DummyHttpClientMetrics() {

                @Override
                public Void requestBegin(Void endpointMetric, Void socketMetric, SocketAddress localAddress, SocketAddress remoteAddress, HttpClientRequest request) {
                    requestBeginCalled.set(true);
                    return null;
                }

                @Override
                public void responseEnd(Void requestMetric, HttpClientResponse response) {
                    responseEndCalled.set(true);
                }

                @Override
                public Void connected(SocketAddress remoteAddress, String remoteName) {
                    socketConnectedCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                    return null;
                }

                @Override
                public void disconnected(Void socketMetric, SocketAddress remoteAddress) {
                    socketDisconnectedCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public void bytesRead(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
                    bytesReadCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public void bytesWritten(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
                    bytesWrittenCalled.set(true);
                    checker.accept(expectedThread.get(), expectedContext.get());
                }

                @Override
                public void close() {
                    closeCalled.set(true);
                }

                @Override
                public boolean isEnabled() {
                    return true;
                }
            };
        }
    };
    Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
    HttpServer server = vertx.createHttpServer();
    server.requestHandler(req -> {
        req.endHandler(buf -> {
            HttpServerResponse resp = req.response();
            resp.setChunked(true).write(Buffer.buffer("bye")).end();
            resp.close();
        });
    });
    CountDownLatch latch = new CountDownLatch(1);
    server.listen(8080, "localhost", onSuccess(s -> {
        latch.countDown();
    }));
    awaitLatch(latch);
    Context ctx = contextFactory.apply(vertx);
    ctx.runOnContext(v1 -> {
        expectedThread.set(Thread.currentThread());
        expectedContext.set(Vertx.currentContext());
        HttpClient client = vertx.createHttpClient();
        checker.accept(expectedThread.get(), expectedContext.get());
        HttpClientRequest req = client.put(8080, "localhost", "/");
        req.handler(resp -> {
            executeInVanillaThread(() -> {
                client.close();
                vertx.close(v2 -> {
                    assertTrue(requestBeginCalled.get());
                    assertTrue(responseEndCalled.get());
                    assertTrue(socketConnectedCalled.get());
                    assertTrue(socketDisconnectedCalled.get());
                    assertTrue(bytesReadCalled.get());
                    assertTrue(bytesWrittenCalled.get());
                    assertTrue(closeCalled.get());
                    testComplete();
                });
            });
        });
        req.setChunked(true).write("hello");
        req.end();
    });
    await();
}
Also used : io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) io.vertx.core.http(io.vertx.core.http) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) Ignore(org.junit.Ignore) BiConsumer(java.util.function.BiConsumer) io.vertx.core.spi.metrics(io.vertx.core.spi.metrics) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) CountDownLatch(java.util.concurrent.CountDownLatch) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 7 with MetricsOptions

use of io.vertx.core.metrics.MetricsOptions in project vert.x by eclipse.

the class MetricsContextTest method testDeploy.

private void testDeploy(boolean worker, boolean multiThreaded, BiConsumer<Thread, Context> checker) {
    AtomicReference<Thread> verticleThread = new AtomicReference<>();
    AtomicReference<Context> verticleContext = new AtomicReference<>();
    AtomicBoolean deployedCalled = new AtomicBoolean();
    AtomicBoolean undeployedCalled = new AtomicBoolean();
    VertxMetricsFactory factory = (vertx, options) -> new DummyVertxMetrics() {

        @Override
        public void verticleDeployed(Verticle verticle) {
            deployedCalled.set(true);
            checker.accept(verticleThread.get(), verticleContext.get());
        }

        @Override
        public void verticleUndeployed(Verticle verticle) {
            undeployedCalled.set(true);
            checker.accept(verticleThread.get(), verticleContext.get());
        }
    };
    Vertx vertx = vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
    vertx.deployVerticle(new AbstractVerticle() {

        @Override
        public void start() throws Exception {
            verticleThread.set(Thread.currentThread());
            verticleContext.set(Vertx.currentContext());
        }
    }, new DeploymentOptions().setWorker(worker).setMultiThreaded(multiThreaded), ar1 -> {
        assertTrue(ar1.succeeded());
        vertx.undeploy(ar1.result(), ar2 -> {
            assertTrue(ar1.succeeded());
            assertTrue(deployedCalled.get());
            assertTrue(undeployedCalled.get());
            testComplete();
        });
    });
    await();
}
Also used : io.vertx.core(io.vertx.core) DatagramSocket(io.vertx.core.datagram.DatagramSocket) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) io.vertx.core.net(io.vertx.core.net) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) io.vertx.core.http(io.vertx.core.http) CountDownLatch(java.util.concurrent.CountDownLatch) EventBus(io.vertx.core.eventbus.EventBus) Buffer(io.vertx.core.buffer.Buffer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) Ignore(org.junit.Ignore) BiConsumer(java.util.function.BiConsumer) io.vertx.core.spi.metrics(io.vertx.core.spi.metrics) DatagramSocketOptions(io.vertx.core.datagram.DatagramSocketOptions) MessageConsumer(io.vertx.core.eventbus.MessageConsumer) MetricsOptions(io.vertx.core.metrics.MetricsOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) DummyVertxMetrics(io.vertx.core.metrics.impl.DummyVertxMetrics) VertxMetricsFactory(io.vertx.core.spi.VertxMetricsFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 8 with MetricsOptions

use of io.vertx.core.metrics.MetricsOptions in project vert.x by eclipse.

the class MetricsOptionsTest method testJsonOptions.

@Test
public void testJsonOptions() {
    MetricsOptions options = new MetricsOptions(new JsonObject());
    assertFalse(options.isEnabled());
    Random rand = new Random();
    boolean metricsEnabled = rand.nextBoolean();
    String customValue = TestUtils.randomAlphaString(10);
    options = new MetricsOptions(new JsonObject().put("enabled", metricsEnabled).put("custom", customValue));
    assertEquals(metricsEnabled, options.isEnabled());
    assertEquals(metricsEnabled, options.toJson().getBoolean("enabled"));
    assertEquals(customValue, options.toJson().getString("custom"));
}
Also used : MetricsOptions(io.vertx.core.metrics.MetricsOptions) Random(java.util.Random) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 9 with MetricsOptions

use of io.vertx.core.metrics.MetricsOptions in project vert.x by eclipse.

the class MetricsTest method testThreadPoolMetricsWithNamedExecuteBlocking.

@Test
public void testThreadPoolMetricsWithNamedExecuteBlocking() {
    // Close the instance automatically created
    vertx.close();
    vertx = Vertx.vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(new FakeMetricsFactory())));
    WorkerExecutor workerExec = vertx.createSharedWorkerExecutor("my-pool", 10);
    Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
    FakePoolMetrics metrics = (FakePoolMetrics) all.get("my-pool");
    assertThat(metrics.getPoolSize(), is(10));
    assertThat(metrics.numberOfIdleThreads(), is(10));
    Handler<Future<Void>> job = getSomeDumbTask();
    AtomicInteger counter = new AtomicInteger();
    AtomicBoolean hadWaitingQueue = new AtomicBoolean();
    AtomicBoolean hadIdle = new AtomicBoolean();
    AtomicBoolean hadRunning = new AtomicBoolean();
    for (int i = 0; i < 100; i++) {
        workerExec.executeBlocking(job, false, ar -> {
            if (metrics.numberOfWaitingTasks() > 0) {
                hadWaitingQueue.set(true);
            }
            if (metrics.numberOfIdleThreads() > 0) {
                hadIdle.set(true);
            }
            if (metrics.numberOfRunningTasks() > 0) {
                hadRunning.set(true);
            }
            if (counter.incrementAndGet() == 100) {
                testComplete();
            }
        });
    }
    await();
    assertEquals(metrics.numberOfSubmittedTask(), 100);
    assertEquals(metrics.numberOfCompletedTasks(), 100);
    assertTrue(hadIdle.get());
    assertTrue(hadWaitingQueue.get());
    assertTrue(hadRunning.get());
    assertEquals(metrics.numberOfIdleThreads(), 10);
    assertEquals(metrics.numberOfRunningTasks(), 0);
    assertEquals(metrics.numberOfWaitingTasks(), 0);
}
Also used : MetricsOptions(io.vertx.core.metrics.MetricsOptions) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 10 with MetricsOptions

use of io.vertx.core.metrics.MetricsOptions in project vert.x by eclipse.

the class MetricsOptionsTest method testCopyOptions.

@Test
public void testCopyOptions() {
    MetricsOptions options = new MetricsOptions();
    Random rand = new Random();
    boolean metricsEnabled = rand.nextBoolean();
    options.setEnabled(metricsEnabled);
    options = new MetricsOptions(options);
    assertEquals(metricsEnabled, options.isEnabled());
}
Also used : MetricsOptions(io.vertx.core.metrics.MetricsOptions) Random(java.util.Random) Test(org.junit.Test)

Aggregations

MetricsOptions (io.vertx.core.metrics.MetricsOptions)27 Test (org.junit.Test)22 DummyVertxMetrics (io.vertx.core.metrics.impl.DummyVertxMetrics)16 VertxMetricsFactory (io.vertx.core.spi.VertxMetricsFactory)14 CountDownLatch (java.util.concurrent.CountDownLatch)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 io.vertx.core (io.vertx.core)12 Buffer (io.vertx.core.buffer.Buffer)12 DatagramSocket (io.vertx.core.datagram.DatagramSocket)12 DatagramSocketOptions (io.vertx.core.datagram.DatagramSocketOptions)12 EventBus (io.vertx.core.eventbus.EventBus)12 MessageConsumer (io.vertx.core.eventbus.MessageConsumer)12 io.vertx.core.http (io.vertx.core.http)12 io.vertx.core.net (io.vertx.core.net)12 io.vertx.core.spi.metrics (io.vertx.core.spi.metrics)12 BiConsumer (java.util.function.BiConsumer)12 Function (java.util.function.Function)12 Ignore (org.junit.Ignore)12 VertxOptions (io.vertx.core.VertxOptions)7