Search in sources :

Example 36 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project ignite by apache.

the class HadoopExecutorServiceTest method testExecutesAll.

/**
 * @throws Exception If failed.
 */
public void testExecutesAll() throws Exception {
    final HadoopExecutorService exec = new HadoopExecutorService(log, "_GRID_NAME_", 10, 5);
    for (int i = 0; i < 5; i++) {
        final int loops = 5000;
        int threads = 17;
        final LongAdder sum = new LongAdder();
        multithreaded(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                for (int i = 0; i < loops; i++) {
                    exec.submit(new Callable<Void>() {

                        @Override
                        public Void call() throws Exception {
                            sum.increment();
                            return null;
                        }
                    });
                }
                return null;
            }
        }, threads);
        while (exec.active() != 0) {
            X.println("__ active: " + exec.active());
            Thread.sleep(200);
        }
        assertEquals(threads * loops, sum.sum());
        X.println("_ ok");
    }
    assertTrue(exec.shutdown(0));
}
Also used : LongAdder(java.util.concurrent.atomic.LongAdder) HadoopExecutorService(org.apache.ignite.internal.processors.hadoop.taskexecutor.HadoopExecutorService) Callable(java.util.concurrent.Callable)

Example 37 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project ignite by apache.

the class HadoopExecutorServiceTest method testShutdown.

/**
 * @throws Exception If failed.
 */
public void testShutdown() throws Exception {
    for (int i = 0; i < 5; i++) {
        final HadoopExecutorService exec = new HadoopExecutorService(log, "_GRID_NAME_", 10, 5);
        final LongAdder sum = new LongAdder();
        final AtomicBoolean finish = new AtomicBoolean();
        IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                while (!finish.get()) {
                    exec.submit(new Callable<Void>() {

                        @Override
                        public Void call() throws Exception {
                            sum.increment();
                            return null;
                        }
                    });
                }
                return null;
            }
        }, 19);
        Thread.sleep(200);
        assertTrue(exec.shutdown(50));
        long res = sum.sum();
        assertTrue(res > 0);
        finish.set(true);
        fut.get();
        // Nothing was executed after shutdown.
        assertEquals(res, sum.sum());
        X.println("_ ok");
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongAdder(java.util.concurrent.atomic.LongAdder) HadoopExecutorService(org.apache.ignite.internal.processors.hadoop.taskexecutor.HadoopExecutorService) Callable(java.util.concurrent.Callable)

Example 38 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project ignite by apache.

the class GridBoundedConcurrentLinkedHashSetLoadTest method main.

/**
 * @param args Arguments.
 */
public static void main(String[] args) throws Exception {
    QueuePolicy qPlc = args.length > 0 ? QueuePolicy.valueOf(args[0]) : SINGLE_Q;
    int threadCnt = args.length > 1 ? Integer.valueOf(args[1]) : Runtime.getRuntime().availableProcessors();
    X.println("Queue policy: " + qPlc);
    X.println("Threads: " + threadCnt);
    ExecutorService pool = Executors.newFixedThreadPool(threadCnt);
    final Collection<IgniteUuid> set = new GridBoundedConcurrentLinkedHashSet<>(10240, 32, 0.75f, 128, qPlc);
    X.println("Set: " + set);
    final LongAdder execCnt = new LongAdder();
    final AtomicBoolean finish = new AtomicBoolean();
    // Thread that measures and outputs performance statistics.
    Thread collector = new Thread(new Runnable() {

        @SuppressWarnings({ "BusyWait", "InfiniteLoopStatement" })
        @Override
        public void run() {
            GridCumulativeAverage avgTasksPerSec = new GridCumulativeAverage();
            try {
                while (!finish.get()) {
                    Thread.sleep(UPDATE_INTERVAL_SEC * 1000);
                    long curTasksPerSec = execCnt.sumThenReset() / UPDATE_INTERVAL_SEC;
                    X.println(">>> Tasks/s: " + curTasksPerSec);
                    avgTasksPerSec.update(curTasksPerSec);
                }
            } catch (InterruptedException ignored) {
                X.println(">>> Interrupted.");
                Thread.currentThread().interrupt();
            }
        }
    });
    collector.start();
    Collection<Callable<Object>> producers = new ArrayList<>(threadCnt);
    for (int i = 0; i < threadCnt; i++) producers.add(new Callable<Object>() {

        @SuppressWarnings({ "unchecked", "InfiniteLoopStatement" })
        @Override
        public Object call() throws Exception {
            UUID id = UUID.randomUUID();
            try {
                while (!finish.get()) {
                    set.add(IgniteUuid.fromUuid(id));
                    execCnt.increment();
                }
                return null;
            } catch (Throwable t) {
                t.printStackTrace();
                throw new Exception(t);
            } finally {
                X.println("Thread finished.");
            }
        }
    });
    pool.invokeAll(producers);
}
Also used : ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) GridBoundedConcurrentLinkedHashSet(org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashSet) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongAdder(java.util.concurrent.atomic.LongAdder) IgniteUuid(org.apache.ignite.lang.IgniteUuid) GridCumulativeAverage(org.apache.ignite.loadtests.util.GridCumulativeAverage) ExecutorService(java.util.concurrent.ExecutorService) UUID(java.util.UUID) QueuePolicy(org.jsr166.ConcurrentLinkedHashMap.QueuePolicy)

Example 39 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project ignite by apache.

the class GridJobExecutionSingleNodeSemaphoreLoadTest method main.

/**
 * @param args Command line arguments:
 *             1-st: Number of worker threads. Default equals to available CPU number / 2.
 *             2-nd: Concurrent tasks count. Default: 1024.
 *             3-rd: Test duration in seconds. 0 means infinite. Default: 0.
 *             4-th: File to output test results to.
 * @throws Exception If failed.
 */
public static void main(String[] args) throws Exception {
    GridFileLock fileLock = GridLoadTestUtils.fileLock();
    fileLock.lock();
    try {
        // Command line arguments.
        // 
        // NOTE: on MacOS better numbers are shown if public pool core and max sizes are
        // equal to CPU count. And producer threads count is equal to CPU count.
        // 
        int threadCnt = args.length > 0 ? Integer.parseInt(args[0]) : Runtime.getRuntime().availableProcessors() / 2;
        int taskCnt = args.length > 1 ? Integer.parseInt(args[1]) : 1024;
        final int duration = args.length > 2 ? Integer.parseInt(args[2]) : 0;
        final String outputFileName = args.length > 3 ? args[3] : null;
        final LongAdder execCnt = new LongAdder();
        try {
            final Ignite g = G.start("modules/tests/config/grid-job-load.xml");
            X.println("Thread count: " + threadCnt);
            X.println("Task count: " + taskCnt);
            X.println("Duration: " + duration);
            X.println("Warming up...");
            g.compute().execute(GridJobExecutionLoadTestTask.class, null);
            g.compute().execute(GridJobExecutionLoadTestTask.class, null);
            runTest(g, threadCnt, taskCnt, WARM_UP_DURATION, execCnt);
            System.gc();
            execCnt.reset();
            X.println("Running main test.");
            IgniteInternalFuture<Void> collectorFut = GridTestUtils.runAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    GridCumulativeAverage avgTasksPerSec = new GridCumulativeAverage();
                    try {
                        while (!Thread.currentThread().isInterrupted()) {
                            U.sleep(UPDATE_INTERVAL_SEC * 1000);
                            long curTasksPerSec = execCnt.sumThenReset() / UPDATE_INTERVAL_SEC;
                            X.println(">>> Tasks/s: " + curTasksPerSec);
                            avgTasksPerSec.update(curTasksPerSec);
                        }
                    } catch (IgniteInterruptedCheckedException ignored) {
                        X.println(">>> Interrupted.");
                        Thread.currentThread().interrupt();
                    }
                    X.println(">>> Average tasks/s: " + avgTasksPerSec);
                    if (outputFileName != null) {
                        X.println("Writing test results to a file: " + outputFileName);
                        try {
                            GridLoadTestUtils.appendLineToFile(outputFileName, "%s,%d", GridLoadTestUtils.DATE_TIME_FORMAT.format(new Date()), avgTasksPerSec.get());
                        } catch (IOException e) {
                            X.error("Failed to output to a file", e);
                        }
                    }
                    return null;
                }
            });
            runTest(g, threadCnt, taskCnt, duration * 1000, execCnt);
            X.println("All done, stopping.");
            collectorFut.cancel();
        } finally {
            G.stopAll(true);
        }
    } finally {
        fileLock.close();
    }
}
Also used : IOException(java.io.IOException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IOException(java.io.IOException) Date(java.util.Date) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) LongAdder(java.util.concurrent.atomic.LongAdder) GridCumulativeAverage(org.apache.ignite.loadtests.util.GridCumulativeAverage) Ignite(org.apache.ignite.Ignite) GridFileLock(org.apache.ignite.testframework.GridFileLock)

Example 40 with LongAdder

use of java.util.concurrent.atomic.LongAdder in project ignite by apache.

the class GridFutureListenPerformanceTest method main.

/**
 * @param args Args.
 * @throws InterruptedException If failed.
 */
public static void main(String[] args) throws InterruptedException {
    final LongAdder cnt = new LongAdder();
    final ConcurrentLinkedDeque<GridFutureAdapter<Object>> futs = new ConcurrentLinkedDeque<>();
    ExecutorService pool = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    Thread statThread = new Thread() {

        @SuppressWarnings("BusyWait")
        @Override
        public void run() {
            while (!done) {
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ignored) {
                    return;
                }
                System.out.println(new Date() + " Notifications per sec: " + (cnt.sumThenReset() / 5));
            }
        }
    };
    statThread.setDaemon(true);
    statThread.start();
    for (int i = 0; i < Runtime.getRuntime().availableProcessors(); i++) {
        pool.submit(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                Random rnd = new Random();
                while (!done) {
                    for (int j = 0; j < rnd.nextInt(10); j++) {
                        GridFutureAdapter<Object> fut = new GridFutureAdapter<>();
                        futs.add(fut);
                        for (int k = 1; k < rnd.nextInt(3); k++) {
                            fut.listen(new IgniteInClosure<IgniteInternalFuture<Object>>() {

                                @Override
                                public void apply(IgniteInternalFuture<Object> t) {
                                    try {
                                        t.get();
                                    } catch (IgniteCheckedException e) {
                                        e.printStackTrace();
                                    }
                                    cnt.increment();
                                }
                            });
                        }
                    }
                    GridFutureAdapter<Object> fut;
                    while ((fut = futs.poll()) != null) fut.onDone();
                }
                return null;
            }
        });
    }
    Thread.sleep(5 * 60 * 1000);
    done = true;
    pool.shutdownNow();
    pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
}
Also used : IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) Date(java.util.Date) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ConcurrentLinkedDeque(java.util.concurrent.ConcurrentLinkedDeque) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) LongAdder(java.util.concurrent.atomic.LongAdder) Random(java.util.Random) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

LongAdder (java.util.concurrent.atomic.LongAdder)92 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 Random (java.util.Random)13 ExecutorService (java.util.concurrent.ExecutorService)10 SplittableRandom (java.util.SplittableRandom)9 HashMap (java.util.HashMap)7 Map (java.util.Map)6 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)6 Theory (org.junit.experimental.theories.Theory)6 UUID (java.util.UUID)5 ArrayList (java.util.ArrayList)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 Ignite (org.apache.ignite.Ignite)4 IgniteUuid (org.apache.ignite.lang.IgniteUuid)4 Metric (org.springframework.boot.actuate.metrics.Metric)4 Date (java.util.Date)3 HashSet (java.util.HashSet)3 Callable (java.util.concurrent.Callable)3 CountDownLatch (java.util.concurrent.CountDownLatch)3