Search in sources :

Example 6 with PoolMetrics

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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics) Test(org.junit.Test)

Example 7 with PoolMetrics

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);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileSystem(io.vertx.core.file.FileSystem) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics) Test(org.junit.Test)

Example 8 with PoolMetrics

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;
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics)

Example 9 with PoolMetrics

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());
    }
}
Also used : PoolMetrics(io.vertx.core.spi.metrics.PoolMetrics)

Aggregations

PoolMetrics (io.vertx.core.spi.metrics.PoolMetrics)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Test (org.junit.Test)3 EventLoop (io.netty.channel.EventLoop)1 io.vertx.core (io.vertx.core)1 FileSystem (io.vertx.core.file.FileSystem)1 Logger (io.vertx.core.impl.logging.Logger)1 LoggerFactory (io.vertx.core.impl.logging.LoggerFactory)1 JsonObject (io.vertx.core.json.JsonObject)1 MetricsOptions (io.vertx.core.metrics.MetricsOptions)1 VertxThreadFactory (io.vertx.core.spi.VertxThreadFactory)1 VertxTracer (io.vertx.core.spi.tracing.VertxTracer)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Executor (java.util.concurrent.Executor)1 ExecutorService (java.util.concurrent.ExecutorService)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1