Search in sources :

Example 86 with LongAdder

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

the class GridIoManager method runIoTest.

/**
 * @param warmup Warmup duration in milliseconds.
 * @param duration Test duration in milliseconds.
 * @param threads Thread count.
 * @param latencyLimit Max latency in nanoseconds.
 * @param rangesCnt Ranges count in resulting histogram.
 * @param payLoadSize Payload size in bytes.
 * @param procFromNioThread {@code True} to process requests in NIO threads.
 * @param nodes Nodes participating in test.
 */
public void runIoTest(final long warmup, final long duration, final int threads, final long latencyLimit, final int rangesCnt, final int payLoadSize, final boolean procFromNioThread, final List<ClusterNode> nodes) {
    ExecutorService svc = Executors.newFixedThreadPool(threads + 1);
    final AtomicBoolean warmupFinished = new AtomicBoolean();
    final AtomicBoolean done = new AtomicBoolean();
    final CyclicBarrier bar = new CyclicBarrier(threads + 1);
    final LongAdder cnt = new LongAdder();
    final long sleepDuration = 5000;
    final byte[] payLoad = new byte[payLoadSize];
    final Map<UUID, IoTestThreadLocalNodeResults>[] res = new Map[threads];
    boolean failed = true;
    try {
        svc.execute(new Runnable() {

            @Override
            public void run() {
                boolean failed = true;
                try {
                    bar.await();
                    long start = System.currentTimeMillis();
                    if (log.isInfoEnabled())
                        log.info("IO test started " + "[warmup=" + warmup + ", duration=" + duration + ", threads=" + threads + ", latencyLimit=" + latencyLimit + ", rangesCnt=" + rangesCnt + ", payLoadSize=" + payLoadSize + ", procFromNioThreads=" + procFromNioThread + ']');
                    for (; ; ) {
                        if (!warmupFinished.get() && System.currentTimeMillis() - start > warmup) {
                            if (log.isInfoEnabled())
                                log.info("IO test warmup finished.");
                            warmupFinished.set(true);
                            start = System.currentTimeMillis();
                        }
                        if (warmupFinished.get() && System.currentTimeMillis() - start > duration) {
                            if (log.isInfoEnabled())
                                log.info("IO test finished, will wait for all threads to finish.");
                            done.set(true);
                            bar.await();
                            failed = false;
                            break;
                        }
                        if (log.isInfoEnabled())
                            log.info("IO test [opsCnt/sec=" + (cnt.sumThenReset() * 1000 / sleepDuration) + ", warmup=" + !warmupFinished.get() + ", elapsed=" + (System.currentTimeMillis() - start) + ']');
                        Thread.sleep(sleepDuration);
                    }
                    // At this point all threads have finished the test and
                    // stored data to the resulting array of maps.
                    // Need to iterate it over and sum values for all threads.
                    printIoTestResults(res);
                } catch (InterruptedException | BrokenBarrierException e) {
                    U.error(log, "IO test failed.", e);
                } finally {
                    if (failed)
                        bar.reset();
                }
            }
        });
        for (int i = 0; i < threads; i++) {
            final int i0 = i;
            res[i] = U.newHashMap(nodes.size());
            svc.execute(new Runnable() {

                @Override
                public void run() {
                    boolean failed = true;
                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    int size = nodes.size();
                    Map<UUID, IoTestThreadLocalNodeResults> res0 = res[i0];
                    try {
                        boolean warmupFinished0 = false;
                        bar.await();
                        for (; ; ) {
                            if (done.get())
                                break;
                            if (!warmupFinished0)
                                warmupFinished0 = warmupFinished.get();
                            ClusterNode node = nodes.get(rnd.nextInt(size));
                            List<IgniteIoTestMessage> msgs = sendIoTest(node, payLoad, procFromNioThread).get();
                            cnt.increment();
                            for (IgniteIoTestMessage msg : msgs) {
                                UUID nodeId = msg.senderNodeId();
                                assert nodeId != null;
                                IoTestThreadLocalNodeResults nodeRes = res0.get(nodeId);
                                if (nodeRes == null)
                                    res0.put(nodeId, nodeRes = new IoTestThreadLocalNodeResults(rangesCnt, latencyLimit));
                                nodeRes.onResult(msg);
                            }
                        }
                        bar.await();
                        failed = false;
                    } catch (Exception e) {
                        U.error(log, "IO test worker thread failed.", e);
                    } finally {
                        if (failed)
                            bar.reset();
                    }
                }
            });
        }
        failed = false;
    } finally {
        if (failed)
            U.shutdownNow(GridIoManager.class, svc, log);
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) IgniteDeploymentCheckedException(org.apache.ignite.internal.IgniteDeploymentCheckedException) IgniteClientDisconnectedCheckedException(org.apache.ignite.internal.IgniteClientDisconnectedCheckedException) IgniteException(org.apache.ignite.IgniteException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ClusterTopologyCheckedException(org.apache.ignite.internal.cluster.ClusterTopologyCheckedException) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongAdder(java.util.concurrent.atomic.LongAdder) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) ExecutorService(java.util.concurrent.ExecutorService) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ArrayList(java.util.ArrayList) List(java.util.List) UUID(java.util.UUID) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 87 with LongAdder

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

the class GridCircularBufferPerformanceTest method testAdderThroughput.

/**
 * @throws Exception If failed.
 */
public void testAdderThroughput() throws Exception {
    final int size = 256 * 1024;
    final ArrayBlockingQueue<Integer> buf = new ArrayBlockingQueue<>(size);
    final LongAdder cnt = new LongAdder();
    final AtomicBoolean finished = new AtomicBoolean();
    multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finished.get()) {
                U.sleep(5000);
                info("Ops/sec: " + cnt.sumThenReset() / 5);
            }
            return null;
        }
    }, 1);
    multithreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finished.get()) cnt.increment();
            return null;
        }
    }, 8);
    info("Buffer: " + buf);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) LongAdder(java.util.concurrent.atomic.LongAdder)

Example 88 with LongAdder

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

the class GridCircularBufferPerformanceTest method testThroughput.

/**
 * @throws Exception If failed.
 */
public void testThroughput() throws Exception {
    int size = 256 * 1024;
    final GridCircularBuffer<Integer> buf = new GridCircularBuffer<>(size);
    final LongAdder cnt = new LongAdder();
    final AtomicBoolean finished = new AtomicBoolean();
    multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finished.get()) {
                U.sleep(5000);
                info("Ops/sec: " + cnt.sumThenReset() / 5);
            }
            return null;
        }
    }, 1);
    multithreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finished.get()) {
                buf.add(1);
                cnt.increment();
            }
            return null;
        }
    }, 8);
    info("Buffer: " + buf);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongAdder(java.util.concurrent.atomic.LongAdder) GridCircularBuffer(org.apache.ignite.internal.util.GridCircularBuffer)

Example 89 with LongAdder

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

the class GridTestCacheStore method loadCache.

/**
 * Preload data from store. In this case we just auto-generate random values.
 *
 * @param clo Callback for every key.
 * @param args Optional arguments.
 */
@Override
public void loadCache(final IgniteBiInClosure<GridTestKey, Long> clo, Object... args) {
    // Number of threads is passed in as argument by caller.
    final int numThreads = (Integer) args[0];
    int entryCnt = (Integer) args[1];
    log.info("Number of load threads: " + numThreads);
    log.info("Number of cache entries to load: " + entryCnt);
    ExecutorService execSvc = Executors.newFixedThreadPool(numThreads);
    try {
        ExecutorCompletionService<Object> completeSvc = new ExecutorCompletionService<>(execSvc);
        final IgniteCache<GridTestKey, Long> cache = ignite.cache("partitioned");
        assert cache != null;
        final LongAdder adder = new LongAdder();
        for (int i = 0; i < numThreads; i++) {
            final int threadId = i;
            final int perThreadKeys = entryCnt / numThreads;
            final int mod = entryCnt % numThreads;
            completeSvc.submit(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    int start = threadId * perThreadKeys;
                    int end = start + perThreadKeys;
                    if (threadId + 1 == numThreads)
                        end += mod;
                    for (long i = start; i < end; i++) {
                        if (ignite.affinity(cache.getName()).mapKeyToNode(GridTestKey.affinityKey(i)).isLocal()) {
                            // Only add if key is local.
                            clo.apply(new GridTestKey(i), i);
                            adder.increment();
                        }
                        if (i % 10000 == 0)
                            log.info("Loaded " + adder.intValue() + " keys.");
                    }
                    return null;
                }
            });
        }
        // Wait for threads to complete.
        for (int i = 0; i < numThreads; i++) {
            try {
                completeSvc.take().get();
            } catch (InterruptedException | ExecutionException e) {
                throw new CacheLoaderException(e);
            }
        }
        // Final print out.
        log.info("Loaded " + adder.intValue() + " keys.");
    } finally {
        execSvc.shutdown();
    }
}
Also used : ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) CacheLoaderException(javax.cache.integration.CacheLoaderException) ExecutionException(java.util.concurrent.ExecutionException) LongAdder(java.util.concurrent.atomic.LongAdder) CacheLoaderException(javax.cache.integration.CacheLoaderException) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException)

Example 90 with LongAdder

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

the class GridIoManagerBenchmark0 method testThroughput.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("deprecation")
public void testThroughput() throws Exception {
    final IgniteKernal sndKernal = (IgniteKernal) grid(0);
    final IgniteKernal rcvKernal = (IgniteKernal) grid(1);
    final ClusterNode sndNode = sndKernal.localNode();
    final ClusterNode rcvNode = rcvKernal.localNode();
    final GridIoManager snd = sndKernal.context().io();
    final GridIoManager rcv = rcvKernal.context().io();
    info("Senders: " + THREADS);
    info("Messages: " + CONCUR_MSGS);
    final Semaphore sem = new Semaphore(CONCUR_MSGS);
    final LongAdder msgCntr = new LongAdder();
    final String topic = "test-topic";
    rcv.addMessageListener(topic, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg, byte plc) {
            try {
                rcv.sendToCustomTopic(sndNode, topic, (Message) msg, PUBLIC_POOL);
            } catch (IgniteCheckedException e) {
                error("Failed to send message.", e);
            }
        }
    });
    snd.addMessageListener(topic, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg, byte plc) {
            msgCntr.increment();
            sem.release();
        }
    });
    Timer t = new Timer("results-reporter");
    t.schedule(new TimerTask() {

        private long ts = System.currentTimeMillis();

        @Override
        public void run() {
            long newTs = System.currentTimeMillis();
            long qrys = msgCntr.sumThenReset();
            long time = newTs - ts;
            X.println("Communication benchmark [qps=" + qrys * 1000 / time + ", executed=" + qrys + ", time=" + time + ']');
            ts = newTs;
        }
    }, 10000, 10000);
    final AtomicBoolean finish = new AtomicBoolean();
    IgniteInternalFuture<?> f = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                IgniteUuid msgId = IgniteUuid.randomUuid();
                while (!finish.get()) {
                    sem.acquire();
                    snd.sendToCustomTopic(rcvNode, topic, new GridTestMessage(msgId, (String) null), PUBLIC_POOL);
                }
            } catch (IgniteCheckedException e) {
                X.println("Message send failed", e);
            } catch (InterruptedException ignored) {
            // No-op.
            }
            return null;
        }
    }, THREADS, "send-thread");
    Thread.sleep(TEST_TIMEOUT);
    finish.set(true);
    sem.release(CONCUR_MSGS * 2);
    t.cancel();
    f.get();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) Message(org.apache.ignite.plugin.extensions.communication.Message) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) Semaphore(java.util.concurrent.Semaphore) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) LongAdder(java.util.concurrent.atomic.LongAdder) Timer(java.util.Timer) TimerTask(java.util.TimerTask) GridIoManager(org.apache.ignite.internal.managers.communication.GridIoManager) IgniteUuid(org.apache.ignite.lang.IgniteUuid) UUID(java.util.UUID)

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