use of io.vertx.core.spi.metrics.PoolMetrics 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.spi.metrics.PoolMetrics in project vert.x by eclipse.
the class MetricsTest method testWithWorkerVerticle.
private void testWithWorkerVerticle(DeploymentOptions options) {
AtomicInteger counter = new AtomicInteger();
Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
FakePoolMetrics metrics = (FakePoolMetrics) all.get("vert.x-worker-thread");
assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize()));
assertThat(metrics.numberOfIdleThreads(), is(getOptions().getWorkerPoolSize()));
AtomicBoolean hadWaitingQueue = new AtomicBoolean();
AtomicBoolean hadIdle = new AtomicBoolean();
AtomicBoolean hadRunning = new AtomicBoolean();
int count = 100;
Verticle worker = new AbstractVerticle() {
@Override
public void start(Future<Void> done) throws Exception {
vertx.eventBus().localConsumer("message", d -> {
try {
Thread.sleep(10);
if (metrics.numberOfWaitingTasks() > 0) {
hadWaitingQueue.set(true);
}
if (metrics.numberOfIdleThreads() > 0) {
hadIdle.set(true);
}
if (metrics.numberOfRunningTasks() > 0) {
hadRunning.set(true);
}
if (counter.incrementAndGet() == count) {
testComplete();
}
} catch (InterruptedException e) {
Thread.currentThread().isInterrupted();
}
});
done.complete();
}
};
vertx.deployVerticle(worker, options, s -> {
for (int i = 0; i < count; i++) {
vertx.eventBus().send("message", i);
}
});
await();
// The verticle deployment is also executed on the worker thread pool
assertEquals(metrics.numberOfSubmittedTask(), count + 1);
assertEquals(metrics.numberOfCompletedTasks(), count + 1);
assertTrue(hadIdle.get());
assertTrue(hadWaitingQueue.get());
assertTrue(hadRunning.get());
assertEquals(metrics.numberOfIdleThreads(), getOptions().getWorkerPoolSize());
assertEquals(metrics.numberOfRunningTasks(), 0);
assertEquals(metrics.numberOfWaitingTasks(), 0);
}
use of io.vertx.core.spi.metrics.PoolMetrics in project vert.x by eclipse.
the class ContextImpl method executeBlocking.
static <T> Future<T> executeBlocking(ContextInternal context, Handler<Promise<T>> blockingCodeHandler, WorkerPool workerPool, TaskQueue queue) {
PoolMetrics metrics = workerPool.metrics();
Object queueMetric = metrics != null ? metrics.submitted() : null;
Promise<T> promise = context.promise();
Future<T> fut = promise.future();
try {
Runnable command = () -> {
Object execMetric = null;
if (metrics != null) {
execMetric = metrics.begin(queueMetric);
}
context.dispatch(promise, f -> {
try {
blockingCodeHandler.handle(promise);
} catch (Throwable e) {
promise.tryFail(e);
}
});
if (metrics != null) {
metrics.end(execMetric, fut.succeeded());
}
};
Executor exec = workerPool.executor();
if (queue != null) {
queue.execute(command, exec);
} else {
exec.execute(command);
}
} catch (RejectedExecutionException e) {
// Pool is already shut down
if (metrics != null) {
metrics.rejected(queueMetric);
}
throw e;
}
return fut;
}
use of io.vertx.core.spi.metrics.PoolMetrics in project vert.x by eclipse.
the class VertxImpl method createSharedWorkerPool.
public synchronized SharedWorkerPool createSharedWorkerPool(String name, int poolSize, long maxExecuteTime, TimeUnit maxExecuteTimeUnit) {
if (poolSize < 1) {
throw new IllegalArgumentException("poolSize must be > 0");
}
if (maxExecuteTime < 1) {
throw new IllegalArgumentException("maxExecuteTime must be > 0");
}
SharedWorkerPool sharedWorkerPool = namedWorkerPools.get(name);
if (sharedWorkerPool == null) {
ThreadFactory workerThreadFactory = createThreadFactory(maxExecuteTime, maxExecuteTimeUnit, name + "-", true);
ExecutorService workerExec = executorServiceFactory.createExecutor(workerThreadFactory, poolSize, poolSize);
PoolMetrics workerMetrics = metrics != null ? metrics.createPoolMetrics("worker", name, poolSize) : null;
namedWorkerPools.put(name, sharedWorkerPool = new SharedWorkerPool(name, workerExec, workerMetrics));
} else {
sharedWorkerPool.refCount++;
}
return sharedWorkerPool;
}
use of io.vertx.core.spi.metrics.PoolMetrics in project vert.x by eclipse.
the class WorkerContext method run.
private <T> void run(ContextInternal ctx, TaskQueue queue, T value, Handler<T> task) {
Objects.requireNonNull(task, "Task handler must not be null");
PoolMetrics metrics = workerPool.metrics();
Object queueMetric = metrics != null ? metrics.submitted() : null;
queue.execute(() -> {
Object execMetric = null;
if (metrics != null) {
execMetric = metrics.begin(queueMetric);
}
try {
ctx.dispatch(value, task);
} finally {
if (metrics != null) {
metrics.end(execMetric, true);
}
}
}, workerPool.executor());
}
Aggregations