Search in sources :

Example 1 with GridBoundedConcurrentLinkedHashSet

use of org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashSet 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" })
        @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>() {

        @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)

Aggregations

ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 LongAdder (java.util.concurrent.atomic.LongAdder)1 GridBoundedConcurrentLinkedHashSet (org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashSet)1 IgniteUuid (org.apache.ignite.lang.IgniteUuid)1 GridCumulativeAverage (org.apache.ignite.loadtests.util.GridCumulativeAverage)1 QueuePolicy (org.jsr166.ConcurrentLinkedHashMap.QueuePolicy)1