Search in sources :

Example 1 with SynchronizedSummaryStatistics

use of org.apache.commons.math3.stat.descriptive.SynchronizedSummaryStatistics in project xian by happyyangyuan.

the class TestDistributedAtomicLong method testSimulation.

@Test
public void testSimulation() throws Exception {
    final int threadQty = 20;
    final int executionQty = 50;
    final AtomicInteger optimisticTries = new AtomicInteger();
    final AtomicInteger promotedLockTries = new AtomicInteger();
    final AtomicInteger failures = new AtomicInteger();
    final AtomicInteger errors = new AtomicInteger();
    final SummaryStatistics timingStats = new SynchronizedSummaryStatistics();
    List<Future<Void>> procs = Lists.newArrayList();
    ExecutorService executorService = Executors.newFixedThreadPool(threadQty);
    for (int i = 0; i < threadQty; ++i) {
        Callable<Void> proc = new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                doSimulation(executionQty, timingStats, optimisticTries, promotedLockTries, failures, errors);
                return null;
            }
        };
        procs.add(executorService.submit(proc));
    }
    for (Future<Void> f : procs) {
        f.get();
    }
    System.out.println("OptimisticTries: " + optimisticTries.get());
    System.out.println("PromotedLockTries: " + promotedLockTries.get());
    System.out.println("Failures: " + failures.get());
    System.out.println("Errors: " + errors.get());
    System.out.println();
    System.out.println("Avg time: " + timingStats.getMean());
    System.out.println("Max time: " + timingStats.getMax());
    System.out.println("Min time: " + timingStats.getMin());
    System.out.println("Qty: " + timingStats.getN());
    Assert.assertEquals(errors.get(), 0);
    Assert.assertTrue(optimisticTries.get() > 0);
    Assert.assertTrue(promotedLockTries.get() > 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) SynchronizedSummaryStatistics(org.apache.commons.math3.stat.descriptive.SynchronizedSummaryStatistics) SummaryStatistics(org.apache.commons.math3.stat.descriptive.SummaryStatistics) Future(java.util.concurrent.Future) SynchronizedSummaryStatistics(org.apache.commons.math3.stat.descriptive.SynchronizedSummaryStatistics) Callable(java.util.concurrent.Callable) Test(org.testng.annotations.Test)

Aggregations

Callable (java.util.concurrent.Callable)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SummaryStatistics (org.apache.commons.math3.stat.descriptive.SummaryStatistics)1 SynchronizedSummaryStatistics (org.apache.commons.math3.stat.descriptive.SynchronizedSummaryStatistics)1 Test (org.testng.annotations.Test)1