Example usage for java.util.concurrent CompletionService take

List of usage examples for java.util.concurrent CompletionService take

Introduction

In this page you can find the example usage for java.util.concurrent CompletionService take.

Prototype

Future<V> take() throws InterruptedException;

Source Link

Document

Retrieves and removes the Future representing the next completed task, waiting if none are yet present.

Usage

From source file:com.flipkart.bifrost.CommunicationTest.java

@Test
public void testSendReceive() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

    Connection connection = new Connection(Lists.newArrayList("localhost"), "guest", "guest");
    connection.start();// ww w  .j a va 2s. c  om

    BifrostExecutor<Void> executor = BifrostExecutor.<Void>builder(TestAction.class).connection(connection)
            .objectMapper(mapper).requestQueue("bifrost-send").responseQueue("bifrost-recv").concurrency(10)
            .executorService(Executors.newFixedThreadPool(10)).build();

    BifrostRemoteCallExecutionServer<Void> executionServer = BifrostRemoteCallExecutionServer
            .<Void>builder(TestAction.class).objectMapper(mapper).connection(connection).concurrency(10)
            .requestQueue("bifrost-send").build();
    executionServer.start();

    long startTime = System.currentTimeMillis();
    AtomicInteger counter = new AtomicInteger(0);
    int requestCount = 100;
    CompletionService<Void> ecs = new ExecutorCompletionService<>(Executors.newFixedThreadPool(50));
    List<Future<Void>> futures = Lists.newArrayListWithCapacity(requestCount);
    for (int i = 0; i < requestCount; i++) {
        futures.add(ecs.submit(new ServiceCaller(executor, counter)));
    }
    for (int i = 0; i < requestCount; i++) {
        try {
            ecs.take().get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    System.out.println(
            String.format("Completed: %d in %d ms", counter.get(), (System.currentTimeMillis() - startTime)));
    executor.shutdown();
    executionServer.stop();
    connection.stop();

    Assert.assertEquals(requestCount, counter.get());
}

From source file:com.flipkart.bifrost.ListenTest.java

@Test
public void testSendReceive() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

    Connection connection = new Connection(Lists.newArrayList("localhost"), "guest", "guest");
    connection.start();//from   ww w .  j ava2 s.  c  om

    BifrostExecutor<Void> executor = BifrostExecutor.<Void>builder(TestAction.class).connection(connection)
            .objectMapper(mapper).requestQueue("bifrost-send").responseQueue("bifrost-recv").concurrency(20)
            .executorService(Executors.newFixedThreadPool(20)).build();

    BifrostRemoteCallExecutionServer<Void> executionServer = BifrostRemoteCallExecutionServer
            .<Void>builder(TestAction.class).objectMapper(mapper).connection(connection).concurrency(20)
            .requestQueue("bifrost-send").build();
    executionServer.start();

    long startTime = System.currentTimeMillis();
    AtomicInteger counter = new AtomicInteger(0);
    int requestCount = 1000000;
    CompletionService<Void> ecs = new ExecutorCompletionService<>(Executors.newFixedThreadPool(50));
    List<Future<Void>> futures = Lists.newArrayListWithCapacity(requestCount);
    for (int i = 0; i < requestCount; i++) {
        futures.add(ecs.submit(new ServiceCaller(executor, counter)));
    }
    for (int i = 0; i < requestCount; i++) {
        try {
            ecs.take().get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    while (counter.get() != requestCount)
        ;
    System.out.println(
            String.format("Completed: %d in %d ms", counter.get(), (System.currentTimeMillis() - startTime)));
    executor.shutdown();
    executionServer.stop();
    connection.stop();

    Assert.assertEquals(requestCount, counter.get());
}

From source file:com.amazon.s3.S3ParserTest.java

@Test
void testAmazonParseListAllMyBucketsParallelResponseTime() throws InterruptedException, ExecutionException {
    CompletionService<Boolean> completer = new ExecutorCompletionService<Boolean>(exec);

    for (int i = 0; i < LOOP_COUNT; i++)
        completer.submit(new Callable<Boolean>() {
            public Boolean call() throws IOException {
                runAmazonParseListAllMyBuckets();
                return true;
            }/*from  w w  w  .j a  v  a2s.  c o m*/
        });
    for (int i = 0; i < LOOP_COUNT; i++)
        assert completer.take().get();
}

From source file:com.amazon.s3.S3ParserTest.java

@Test(enabled = false)
void testAmazonParseListBucketResultParallelResponseTime() throws InterruptedException, ExecutionException {
    CompletionService<Boolean> completer = new ExecutorCompletionService<Boolean>(exec);

    for (int i = 0; i < LOOP_COUNT; i++)
        completer.submit(new Callable<Boolean>() {
            public Boolean call() throws IOException {
                runAmazonParseListBucketResult();
                return true;
            }/*from   w w  w.  jav  a 2s. com*/
        });
    for (int i = 0; i < LOOP_COUNT; i++)
        assert completer.take().get();
}

From source file:benchmark.hbase.report.LoggingReport.java

@Override
public void aggregateAndPrintResults(BenchmarkType benchMarkType,
        CompletionService<Histogram> executorCompletionService, int numOfThreads, long numOfRecords,
        Stopwatch executorStartTime) {// www  .  ja  v  a  2 s .c  om

    // Used to accumulate results from all histograms.
    final Histogram totalHistogram = Histograms.create();

    for (int i = 0; i < numOfThreads; i++) {
        try {
            final Future<Histogram> histogramFuture = executorCompletionService.take();
            Histogram histogram = histogramFuture.get();
            totalHistogram.add(histogram);
        } catch (final InterruptedException e) {
            log.error("Failed to retrieve data, got inturrupt signal", e);
            Thread.currentThread().interrupt();

            break;
        } catch (final ExecutionException e) {
            log.error("Failed to retrieve data", e);
        }
    }

    executorStartTime.stop();
    final long durationInSeconds = executorStartTime.elapsedTime(TimeUnit.SECONDS);
    final long durationInMs = executorStartTime.elapsedTime(TimeUnit.MILLISECONDS);
    // Using the durationInMs, since I would loose precision when using durationInSeconds.
    final long throughputPerSecond = 1000 * numOfRecords / durationInMs;

    final long min = totalHistogram.getMinValue();
    final double percentile25 = totalHistogram.getValueAtPercentile(25);
    final double percentile50 = totalHistogram.getValueAtPercentile(50);
    final double percentile75 = totalHistogram.getValueAtPercentile(75);
    final double percentile95 = totalHistogram.getValueAtPercentile(95);
    final double percentile99 = totalHistogram.getValueAtPercentile(99);
    final long max = totalHistogram.getMaxValue();
    final double mean = totalHistogram.getMean();
    final double stdDev = totalHistogram.getStdDeviation();
    final long totalMessagesCount = totalHistogram.getTotalCount();

    logInfo("=======================================");
    if (benchMarkType == BenchmarkType.READ_ONLY) {
        logInfo("READ ONLY BENCHMARK STATS");
    } else if (benchMarkType == BenchmarkType.WRITE_ONLY) {
        logInfo("WRITE ONLY BENCHMARK STATS");
    } else if (benchMarkType == BenchmarkType.READ_AND_WRITE) {
        logInfo("READ AND WRITE BENCHMARK STATS");
    } else {
        logInfo("UNKNOWN BENCHMARK STATS");
    }
    logInfo("=======================================");
    logInfo("DURATION (SECOND):      {}", durationInSeconds);
    logInfo("THROUGHPUT / SECOND:    {}", throughputPerSecond);
    logInfo("MIN:                    {}", min);
    logInfo("25th percentile:        {}", percentile25);
    logInfo("50th percentile:        {}", percentile50);
    logInfo("75th percentile:        {}", percentile75);
    logInfo("95th percentile:        {}", percentile95);
    logInfo("99th percentile:        {}", percentile99);
    logInfo("MAX:                    {}", max);
    logInfo("MEAN:                   {}", mean);
    logInfo("STD DEVIATION:          {}", stdDev);
    logInfo("CONCURRANCY:            {}", numOfThreads);
    logInfo("TotalRecords:           {}", totalMessagesCount);
    logInfo("\n\n\n");

}

From source file:org.jclouds.aws.s3.xml.S3ParserTest.java

@Test
void testParseListContainerResultParallelResponseTime() throws InterruptedException, ExecutionException {
    CompletionService<ListBucketResponse> completer = new ExecutorCompletionService<ListBucketResponse>(exec);
    for (int i = 0; i < LOOP_COUNT; i++)
        completer.submit(new Callable<ListBucketResponse>() {
            public ListBucketResponse call() throws IOException, SAXException, HttpException {
                return runParseListContainerResult();
            }//from w  w w .  j a v  a  2s  .  co  m
        });
    for (int i = 0; i < LOOP_COUNT; i++)
        assert completer.take().get() != null;
}

From source file:cn.clxy.upload.UploadFileService.java

private void doUpload() {

    listener.onStart(indexes != null ? indexes.size() : getPartCount());
    parts = new ArrayBlockingQueue<Part>(Config.maxRead);
    CompletionService<String> cs = new ExecutorCompletionService<String>(executor);

    cs.submit(readTask);//w  w w.j a va2s. c om

    for (int i = 0; i < Config.maxUpload; i++) {
        cs.submit(new UploadTask("upload." + i));
    }

    // Wait all done. total count = maxUpload + 1.
    for (int i = 0; i <= Config.maxUpload; i++) {
        Future<String> future = null;
        try {
            future = cs.take();
            checkFuture(future);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    // Notify sever all done.
    Future<String> result = executor.submit(notifyTask);
    checkFuture(result);
    listener.onSuccess();
}

From source file:org.gitana.platform.load.AbstractLoadTest.java

protected List<RunnerResult<V>> execute() throws Exception {
    ExecutorService executorService = createExecutorService();

    CompletionService<V> cs = new ExecutorCompletionService<V>(executorService);

    for (int i = 0; i < getIterationCount(); i++) {
        // create the runner
        Runner<V> runner = createRunner("runner-" + i);
        runner.init();//from   www .j  ava  2  s .co  m

        cs.submit(runner);
    }

    // wait for everything to finish
    List<RunnerResult<V>> results = new ArrayList<RunnerResult<V>>();
    for (int i = 0; i < getIterationCount(); i++) {
        RunnerResult<V> result = null;

        try {
            V v = cs.take().get();

            result = new RunnerResult<V>(v);
        } catch (Exception ex) {
            ex.printStackTrace();

            result = new RunnerResult<V>();
            result.setException(ex);
        }

        results.add(result);
    }

    return results;
}

From source file:org.jclouds.aws.s3.xml.S3ParserTest.java

@Test
void testParseListAllMyBucketsParallelResponseTime() throws InterruptedException, ExecutionException {
    CompletionService<SortedSet<BucketMetadata>> completer = new ExecutorCompletionService<SortedSet<BucketMetadata>>(
            exec);/* w w w . j a  va2  s. com*/
    for (int i = 0; i < LOOP_COUNT; i++)
        completer.submit(new Callable<SortedSet<BucketMetadata>>() {
            public SortedSet<BucketMetadata> call() throws IOException, SAXException, HttpException {
                return runParseListAllMyBuckets();
            }
        });
    for (int i = 0; i < LOOP_COUNT; i++)
        assert completer.take().get() != null;
}

From source file:cn.clxy.codes.upload.UploadFileService.java

private void doUpload(final List<Integer> indexes) {

    log.debug("Start! ===--------------------");

    BlockingQueue<Part> parts = new ArrayBlockingQueue<Part>(Config.MAX_READ);
    CompletionService<String> cs = new ExecutorCompletionService<String>(executor);

    log.debug("Reading started.");
    cs.submit(new ReadTask(file, indexes, parts));

    log.debug("Uploading started.");

    for (int i = 0; i < Config.MAX_UPLOAD; i++) {
        cs.submit(new UploadTask("upload." + i, uploader, parts));
    }/*w w  w  .j a v  a  2s.com*/

    // Wait all done. total count = maxUpload + 1.
    for (int i = 0; i <= Config.MAX_UPLOAD; i++) {
        Future<String> future = null;
        try {
            future = cs.take();
            checkFuture(future);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }

    // Notify sever all done.
    Future<String> result = executor.submit(new NotifyTask(file, uploader));
    checkFuture(result);

    log.debug("End! ===--------------------");
}