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