Search in sources :

Example 91 with LongAdder

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

the class GridIoManagerBenchmark0 method testVariableLoad.

/**
 * @throws Exception If failed.
 */
@SuppressWarnings("deprecation")
public void testVariableLoad() 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";
    final Map<IgniteUuid, CountDownLatch> latches = new ConcurrentHashMap<>();
    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();
            CountDownLatch latch = latches.get(((GridTestMessage) msg).id());
            if (latch != null)
                latch.countDown();
        }
    });
    final AtomicBoolean finish = new AtomicBoolean();
    final AtomicReference<CountDownLatch> latchRef = new AtomicReference<>();
    IgniteInternalFuture<?> f = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finish.get()) {
                CountDownLatch latch = latchRef.get();
                if (latch != null)
                    U.await(latch);
                IgniteUuid msgId = IgniteUuid.randomUuid();
                sem.acquire();
                snd.sendToCustomTopic(rcvNode, topic, new GridTestMessage(msgId, (String) null), PUBLIC_POOL);
            }
            return null;
        }
    }, THREADS, "send-thread");
    IgniteInternalFuture<?> f1 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        private long ts = System.currentTimeMillis();

        @Override
        public Object call() throws Exception {
            try {
                while (!finish.get()) {
                    info(U.nl() + ">>>" + U.nl() + ">>> High load." + U.nl() + ">>>");
                    U.sleep(15 * 1000);
                    reportNumbers();
                    info(U.nl() + ">>>" + U.nl() + ">>> Low load." + U.nl() + ">>>");
                    CountDownLatch latch = new CountDownLatch(1);
                    try {
                        // Here will be a pause.
                        latchRef.set(latch);
                        U.sleep(7 * 1000);
                        reportNumbers();
                    } finally {
                        latch.countDown();
                    }
                }
            } catch (IgniteCheckedException e) {
                X.println("Message send failed", e);
            }
            return null;
        }

        /**
         */
        void reportNumbers() {
            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;
        }
    }, 1, "load-dispatcher");
    IgniteInternalFuture<?> f2 = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (!finish.get()) {
                U.sleep(1000);
                IgniteUuid msgId = IgniteUuid.randomUuid();
                CountDownLatch latch = new CountDownLatch(1);
                latches.put(msgId, latch);
                snd.sendToCustomTopic(rcvNode, topic, new GridTestMessage(msgId, (String) null), PUBLIC_POOL);
                long start = System.currentTimeMillis();
                latch.await();
                info("Response time: " + (System.currentTimeMillis() - start));
            }
            return null;
        }
    }, THREADS, "low-loader");
    Thread.sleep(TEST_TIMEOUT);
    finish.set(true);
    sem.release(CONCUR_MSGS * 2);
    f.get();
    f1.get();
    f2.get();
}
Also used : 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) IgniteUuid(org.apache.ignite.lang.IgniteUuid) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LongAdder(java.util.concurrent.atomic.LongAdder) GridIoManager(org.apache.ignite.internal.managers.communication.GridIoManager)

Example 92 with LongAdder

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

the class GridCacheQuerySimpleBenchmark method testPerformance.

/**
 * @throws Exception If failed.
 */
public void testPerformance() throws Exception {
    Random rnd = new GridRandom();
    final IgniteCache<Long, Person> c = ignite.cache("offheap-cache");
    X.println("___ PUT start");
    final int cnt = 100_000;
    final int maxSalary = cnt / 10;
    for (long i = 0; i < cnt; i++) c.put(i, new Person(rnd.nextInt(maxSalary), "Vasya " + i));
    X.println("___ PUT end");
    final AtomicBoolean end = new AtomicBoolean();
    final LongAdder puts = new LongAdder();
    IgniteInternalFuture<?> fut0 = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Random rnd = new GridRandom();
            while (!end.get()) {
                long i = rnd.nextInt(cnt);
                c.put(i, new Person(rnd.nextInt(maxSalary), "Vasya " + i));
                puts.increment();
            }
            return null;
        }
    }, 10);
    final LongAdder qrys = new LongAdder();
    IgniteInternalFuture<?> fut1 = multithreadedAsync(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            Random rnd = new GridRandom();
            while (!end.get()) {
                int salary = rnd.nextInt(maxSalary);
                c.query(new SqlFieldsQuery("select name from Person where salary = ?").setArgs(salary)).getAll();
                qrys.increment();
            }
            return null;
        }
    }, 10);
    int runTimeSec = 600;
    for (int s = 0; s < runTimeSec; s++) {
        Thread.sleep(1000);
        long puts0 = puts.sum();
        long qrys0 = qrys.sum();
        puts.add(-puts0);
        qrys.add(-qrys0);
        X.println("___ puts: " + puts0 + " qrys: " + qrys0);
    }
    end.set(true);
    fut0.get();
    fut1.get();
    X.println("___ STOP");
}
Also used : IOException(java.io.IOException) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GridRandom(org.apache.ignite.internal.util.GridRandom) Random(java.util.Random) GridRandom(org.apache.ignite.internal.util.GridRandom) LongAdder(java.util.concurrent.atomic.LongAdder)

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