use of io.vertx.core.spi.metrics.PoolMetrics in project vert.x by eclipse.
the class MetricsTest method testThreadPoolMetricsWithExecuteBlocking.
@Test
public void testThreadPoolMetricsWithExecuteBlocking() {
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()));
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++) {
vertx.executeBlocking(job, 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(), 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 MetricsTest method testThreadPoolMetricsWithInternalExecuteBlocking.
@Test
public void testThreadPoolMetricsWithInternalExecuteBlocking() {
// Internal blocking thread pool is used by blocking file system actions.
Map<String, PoolMetrics> all = FakePoolMetrics.getPoolMetrics();
FakePoolMetrics metrics = (FakePoolMetrics) all.get("vert.x-internal-blocking");
assertThat(metrics.getPoolSize(), is(getOptions().getInternalBlockingPoolSize()));
assertThat(metrics.numberOfIdleThreads(), is(getOptions().getInternalBlockingPoolSize()));
AtomicInteger counter = new AtomicInteger();
AtomicBoolean hadWaitingQueue = new AtomicBoolean();
AtomicBoolean hadIdle = new AtomicBoolean();
AtomicBoolean hadRunning = new AtomicBoolean();
FileSystem system = vertx.fileSystem();
for (int i = 0; i < 100; i++) {
vertx.executeBlocking(fut -> {
system.readFile("afile.html", buffer -> {
fut.complete(null);
});
}, 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(), 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 VertxImpl method createSharedWorkerExecutor.
@Override
public synchronized WorkerExecutorImpl createSharedWorkerExecutor(String name, int poolSize, long maxExecuteTime) {
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) {
ExecutorService workerExec = Executors.newFixedThreadPool(poolSize, new VertxThreadFactory(name + "-", checker, true, maxExecuteTime));
PoolMetrics workerMetrics = isMetricsEnabled() ? metrics.createMetrics(workerExec, "worker", name, poolSize) : null;
namedWorkerPools.put(name, sharedWorkerPool = new SharedWorkerPool(name, workerExec, workerMetrics));
} else {
sharedWorkerPool.refCount++;
}
ContextImpl context = getOrCreateContext();
WorkerExecutorImpl namedExec = new WorkerExecutorImpl(this, sharedWorkerPool, true);
context.addCloseHook(namedExec);
return namedExec;
}
use of io.vertx.core.spi.metrics.PoolMetrics in project vert.x by eclipse.
the class WorkerContext method execute.
private <T> void execute(TaskQueue queue, T argument, Handler<T> task) {
if (Context.isOnWorkerThread()) {
task.handle(argument);
} else {
PoolMetrics metrics = workerPool.metrics();
Object queueMetric = metrics != null ? metrics.submitted() : null;
queue.execute(() -> {
Object execMetric = null;
if (metrics != null) {
execMetric = metrics.begin(queueMetric);
}
try {
task.handle(argument);
} finally {
if (metrics != null) {
metrics.end(execMetric, true);
}
}
}, workerPool.executor());
}
}
Aggregations