Example usage for com.google.common.collect Lists newArrayListWithCapacity

List of usage examples for com.google.common.collect Lists newArrayListWithCapacity

Introduction

In this page you can find the example usage for com.google.common.collect Lists newArrayListWithCapacity.

Prototype

@GwtCompatible(serializable = true)
public static <E> ArrayList<E> newArrayListWithCapacity(int initialArraySize) 

Source Link

Document

Creates an ArrayList instance backed by an array with the specified initial size; simply delegates to ArrayList#ArrayList(int) .

Usage

From source file:org.apache.pulsar.testclient.PerformanceProducer.java

public static void main(String[] args) throws Exception {
    final Arguments arguments = new Arguments();
    JCommander jc = new JCommander(arguments);
    jc.setProgramName("pulsar-perf-producer");

    try {/* ww  w. java2  s.c o  m*/
        jc.parse(args);
    } catch (ParameterException e) {
        System.out.println(e.getMessage());
        jc.usage();
        System.exit(-1);
    }

    if (arguments.help) {
        jc.usage();
        System.exit(-1);
    }

    if (arguments.destinations.size() != 1) {
        System.out.println("Only one topic name is allowed");
        jc.usage();
        System.exit(-1);
    }

    if (arguments.confFile != null) {
        Properties prop = new Properties(System.getProperties());
        prop.load(new FileInputStream(arguments.confFile));

        if (arguments.serviceURL == null) {
            arguments.serviceURL = prop.getProperty("brokerServiceUrl");
        }

        if (arguments.serviceURL == null) {
            arguments.serviceURL = prop.getProperty("webServiceUrl");
        }

        // fallback to previous-version serviceUrl property to maintain backward-compatibility
        if (arguments.serviceURL == null) {
            arguments.serviceURL = prop.getProperty("serviceUrl", "http://localhost:8080/");
        }

        if (arguments.authPluginClassName == null) {
            arguments.authPluginClassName = prop.getProperty("authPlugin", null);
        }

        if (arguments.authParams == null) {
            arguments.authParams = prop.getProperty("authParams", null);
        }
    }

    arguments.testTime = TimeUnit.SECONDS.toMillis(arguments.testTime);

    // Dump config variables
    ObjectMapper m = new ObjectMapper();
    ObjectWriter w = m.writerWithDefaultPrettyPrinter();
    log.info("Starting Pulsar perf producer with config: {}", w.writeValueAsString(arguments));

    // Read payload data from file if needed
    byte payloadData[];
    if (arguments.payloadFilename != null) {
        payloadData = Files.readAllBytes(Paths.get(arguments.payloadFilename));
    } else {
        payloadData = new byte[arguments.msgSize];
    }

    // Now processing command line arguments
    String prefixTopicName = arguments.destinations.get(0);
    List<Future<Producer>> futures = Lists.newArrayList();

    EventLoopGroup eventLoopGroup;
    if (SystemUtils.IS_OS_LINUX) {
        eventLoopGroup = new EpollEventLoopGroup(Runtime.getRuntime().availableProcessors(),
                new DefaultThreadFactory("pulsar-perf-producer"));
    } else {
        eventLoopGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors(),
                new DefaultThreadFactory("pulsar-perf-producer"));
    }

    ClientConfiguration clientConf = new ClientConfiguration();
    clientConf.setConnectionsPerBroker(arguments.maxConnections);
    clientConf.setStatsInterval(arguments.statsIntervalSeconds, TimeUnit.SECONDS);
    if (isNotBlank(arguments.authPluginClassName)) {
        clientConf.setAuthentication(arguments.authPluginClassName, arguments.authParams);
    }

    PulsarClient client = new PulsarClientImpl(arguments.serviceURL, clientConf, eventLoopGroup);

    ProducerConfiguration producerConf = new ProducerConfiguration();
    producerConf.setSendTimeout(0, TimeUnit.SECONDS);
    producerConf.setCompressionType(arguments.compression);
    // enable round robin message routing if it is a partitioned topic
    producerConf.setMessageRoutingMode(MessageRoutingMode.RoundRobinPartition);
    if (arguments.batchTime > 0) {
        producerConf.setBatchingMaxPublishDelay(arguments.batchTime, TimeUnit.MILLISECONDS);
        producerConf.setBatchingEnabled(true);
        producerConf.setMaxPendingMessages(arguments.msgRate);
    }

    // Block if queue is full else we will start seeing errors in sendAsync
    producerConf.setBlockIfQueueFull(true);

    for (int i = 0; i < arguments.numTopics; i++) {
        String topic = (arguments.numTopics == 1) ? prefixTopicName
                : String.format("%s-%d", prefixTopicName, i);
        log.info("Adding {} publishers on destination {}", arguments.numProducers, topic);

        for (int j = 0; j < arguments.numProducers; j++) {
            futures.add(client.createProducerAsync(topic, producerConf));
        }
    }

    final List<Producer> producers = Lists.newArrayListWithCapacity(futures.size());
    for (Future<Producer> future : futures) {
        producers.add(future.get());
    }

    log.info("Created {} producers", producers.size());

    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            printAggregatedStats();
        }
    });

    Collections.shuffle(producers);
    AtomicBoolean isDone = new AtomicBoolean();

    executor.submit(() -> {
        try {
            RateLimiter rateLimiter = RateLimiter.create(arguments.msgRate);

            long startTime = System.currentTimeMillis();

            // Send messages on all topics/producers
            long totalSent = 0;
            while (true) {
                for (Producer producer : producers) {
                    if (arguments.testTime > 0) {
                        if (System.currentTimeMillis() - startTime > arguments.testTime) {
                            log.info("------------------- DONE -----------------------");
                            printAggregatedStats();
                            isDone.set(true);
                            Thread.sleep(5000);
                            System.exit(0);
                        }
                    }

                    if (arguments.numMessages > 0) {
                        if (totalSent++ >= arguments.numMessages) {
                            log.info("------------------- DONE -----------------------");
                            printAggregatedStats();
                            isDone.set(true);
                            Thread.sleep(5000);
                            System.exit(0);
                        }
                    }
                    rateLimiter.acquire();

                    final long sendTime = System.nanoTime();

                    producer.sendAsync(payloadData).thenRun(() -> {
                        messagesSent.increment();
                        bytesSent.add(payloadData.length);

                        long latencyMicros = NANOSECONDS.toMicros(System.nanoTime() - sendTime);
                        recorder.recordValue(latencyMicros);
                        cumulativeRecorder.recordValue(latencyMicros);
                    }).exceptionally(ex -> {
                        log.warn("Write error on message", ex);
                        System.exit(-1);
                        return null;
                    });
                }
            }
        } catch (Throwable t) {
            log.error("Got error", t);
        }
    });

    // Print report stats
    long oldTime = System.nanoTime();

    Histogram reportHistogram = null;

    String statsFileName = "perf-producer-" + System.currentTimeMillis() + ".hgrm";
    log.info("Dumping latency stats to {}", statsFileName);

    PrintStream histogramLog = new PrintStream(new FileOutputStream(statsFileName), false);
    HistogramLogWriter histogramLogWriter = new HistogramLogWriter(histogramLog);

    // Some log header bits
    histogramLogWriter.outputLogFormatVersion();
    histogramLogWriter.outputLegend();

    while (true) {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            break;
        }

        if (isDone.get()) {
            break;
        }

        long now = System.nanoTime();
        double elapsed = (now - oldTime) / 1e9;

        double rate = messagesSent.sumThenReset() / elapsed;
        double throughput = bytesSent.sumThenReset() / elapsed / 1024 / 1024 * 8;

        reportHistogram = recorder.getIntervalHistogram(reportHistogram);

        log.info(
                "Throughput produced: {}  msg/s --- {} Mbit/s --- Latency: mean: {} ms - med: {} - 95pct: {} - 99pct: {} - 99.9pct: {} - 99.99pct: {} - Max: {}",
                throughputFormat.format(rate), throughputFormat.format(throughput),
                dec.format(reportHistogram.getMean() / 1000.0),
                dec.format(reportHistogram.getValueAtPercentile(50) / 1000.0),
                dec.format(reportHistogram.getValueAtPercentile(95) / 1000.0),
                dec.format(reportHistogram.getValueAtPercentile(99) / 1000.0),
                dec.format(reportHistogram.getValueAtPercentile(99.9) / 1000.0),
                dec.format(reportHistogram.getValueAtPercentile(99.99) / 1000.0),
                dec.format(reportHistogram.getMaxValue() / 1000.0));

        histogramLogWriter.outputIntervalHistogram(reportHistogram);
        reportHistogram.reset();

        oldTime = now;
    }

    client.close();
}

From source file:gobblin.data.management.copy.SinglePartitionCopyableDataset.java

public static Collection<Partition<CopyableFile>> singlePartition(Collection<? extends CopyableFile> files,
        String name) {// w w  w. ja  v  a2s.c  om
    List<CopyableFile> copyableFiles = Lists.newArrayListWithCapacity(files.size());
    for (CopyableFile file : files) {
        copyableFiles.add(file);
    }
    return ImmutableList.of(new Partition.Builder<CopyableFile>(name).add(copyableFiles).build());
}

From source file:com.turn.camino.config.ConfigUtil.java

/**
 * Transforms map of key/values to list of objects
 *
 * @param map map of key/value pairs/*from  w w  w  .j a va2s . co  m*/
 * @param transformer transformation logic
 * @return list of objects
 */
public static <K, V, T> List<T> mapToList(Map<K, V> map, Transformer<K, V, T> transformer) {
    List<T> list = Lists.newArrayListWithCapacity(map.size());
    for (Map.Entry<K, V> entry : map.entrySet()) {
        list.add(transformer.transform(entry.getKey(), entry.getValue()));
    }
    return list;
}

From source file:ch.ethz.tik.hrouting.util.ConversionUtil.java

public static ArrayList<LatLng> getLatLngPath(List<Node> nodePath) {
    int size = nodePath.size();
    ArrayList<LatLng> latLngList = Lists.newArrayListWithCapacity(size);
    int i = 0;//  ww  w  .j  a  v a2 s. com
    for (Node node : nodePath) {
        LatLng latLng = new LatLng(node.getLatitude(), node.getLongitude());
        latLngList.add(i++, latLng);
    }
    return latLngList;
}

From source file:com.netflix.atlas.client.interpreter.Interpreter.java

/**
 * Tokenize the given expression.//from   www.  j  ava2  s .  com
 */
public static List<Object> getTokens(String expr) {
    String[] tokens = expr.trim().split("\\s*,\\s*");
    List<Object> nonEmpty = Lists.newArrayListWithCapacity(tokens.length);
    for (String t : tokens) {
        if (!t.isEmpty()) {
            nonEmpty.add(t);
        }
    }
    return nonEmpty;
}

From source file:com.cloudera.oryx.als.computation.known.CollectKnownItemsFn.java

private static String setToString(LongFloatMap map) {
    LongPrimitiveIterator it = map.keySetIterator();
    Collection<String> keyStrings = Lists.newArrayListWithCapacity(map.size());
    while (it.hasNext()) {
        keyStrings.add(Long.toString(it.nextLong()));
    }//from w w w .  j  a  v  a 2  s  .  co m
    return DelimitedDataUtils.encode(keyStrings);
}

From source file:com.cinchapi.concourse.server.concurrent.RangeTokens.java

/**
 * Convert the specified range {@code token} to one or more {@link Range
 * ranges} that provide the appropriate coverage.
 * //from  w w  w.j ava 2  s .  c  o  m
 * @param token
 * @return the Ranges
 */
public static Iterable<Range<Value>> convertToRange(RangeToken token) {
    List<Range<Value>> ranges = Lists.newArrayListWithCapacity(1);
    if (token.getOperator() == Operator.EQUALS || token.getOperator() == null) { // null operator means
                                                                                 // the range token is for
                                                                                 // writing
        ranges.add(Range.singleton(token.getValues()[0]));
    } else if (token.getOperator() == Operator.NOT_EQUALS) {
        ranges.add(Range.lessThan(token.getValues()[0]));
        ranges.add(Range.greaterThan(token.getValues()[0]));
    } else if (token.getOperator() == Operator.GREATER_THAN) {
        ranges.add(Range.greaterThan(token.getValues()[0]));
    } else if (token.getOperator() == Operator.GREATER_THAN_OR_EQUALS) {
        ranges.add(Range.atLeast(token.getValues()[0]));
    } else if (token.getOperator() == Operator.LESS_THAN) {
        ranges.add(Range.lessThan(token.getValues()[0]));
    } else if (token.getOperator() == Operator.LESS_THAN_OR_EQUALS) {
        ranges.add(Range.atMost(token.getValues()[0]));
    } else if (token.getOperator() == Operator.BETWEEN) {
        Value a = token.getValues()[0];
        Value b = token.getValues()[1];
        if (a == Value.NEGATIVE_INFINITY && b == Value.POSITIVE_INFINITY) {
            ranges.add(Range.<Value>all());
        } else if (token.getValues().length == 3) {
            ranges.add(Range.open(a, b));
        } else if (token.getValues().length == 4) {
            ranges.add(Range.closed(a, b));
        } else if (token.getValues().length == 5) {
            ranges.add(Range.openClosed(a, b));
        } else {
            ranges.add(Range.closedOpen(a, b));
        }
    } else if (token.getOperator() == Operator.REGEX || token.getOperator() == Operator.NOT_REGEX) {
        ranges.add(Range.<Value>all());
    } else {
        throw new UnsupportedOperationException();
    }
    return ranges;
}

From source file:com.yahoo.yqlplus.engine.internal.plan.IndexedQueryPlanner.java

private static Iterable<IndexDescriptor> convertSet(Set<IndexKey> indexes) {
    List<IndexDescriptor> desc = Lists.newArrayListWithCapacity(indexes.size());
    for (IndexKey key : indexes) {
        IndexDescriptor.Builder b = IndexDescriptor.builder();
        for (String name : key.columnOrder) {
            b.addColumn(name, YQLCoreType.STRING, false, true);
        }//from  w  ww.  j  a  v a2s . c o  m
        desc.add(b.build());
    }
    return desc;
}

From source file:com.facebook.buck.rules.XmlTestResultParser.java

public static TestCaseSummary parse(File xml) throws IOException {
    Document doc = XmlDomParser.parse(xml);
    Element root = doc.getDocumentElement();
    Preconditions.checkState("testcase".equals(root.getTagName()));
    String testCaseName = root.getAttribute("name");

    NodeList testElements = doc.getElementsByTagName("test");
    List<TestResultSummary> testResults = Lists.newArrayListWithCapacity(testElements.getLength());
    for (int i = 0; i < testElements.getLength(); i++) {
        Element node = (Element) testElements.item(i);
        String testName = node.getAttribute("name");
        boolean isSuccess = Boolean.valueOf(node.getAttribute("success"));
        long time = Long.valueOf(node.getAttribute("time"));

        String message;/*  w ww .j  av  a 2 s .c om*/
        String stacktrace;
        if (isSuccess) {
            message = null;
            stacktrace = null;
        } else {
            message = node.getAttribute("message");
            stacktrace = node.getAttribute("stacktrace");
        }

        NodeList stdoutElements = node.getElementsByTagName("stdout");
        String stdOut;
        if (stdoutElements.getLength() == 1) {
            stdOut = stdoutElements.item(0).getTextContent();
        } else {
            stdOut = null;
        }

        NodeList stderrElements = node.getElementsByTagName("stderr");
        String stdErr;
        if (stderrElements.getLength() == 1) {
            stdErr = stderrElements.item(0).getTextContent();
        } else {
            stdErr = null;
        }

        TestResultSummary testResult = new TestResultSummary(testCaseName, testName, isSuccess, time, message,
                stacktrace, stdOut, stdErr);
        testResults.add(testResult);
    }

    return new TestCaseSummary(testCaseName, testResults);
}

From source file:org.gradle.api.internal.artifacts.ivyservice.resolveengine.graph.builder.MessageBuilderHelper.java

static Collection<String> pathTo(EdgeState edge, boolean includeLast) {
    List<List<EdgeState>> acc = Lists.newArrayListWithExpectedSize(1);
    pathTo(edge, Lists.<EdgeState>newArrayList(), acc, Sets.<NodeState>newHashSet());
    List<String> result = Lists.newArrayListWithCapacity(acc.size());
    for (List<EdgeState> path : acc) {
        EdgeState target = Iterators.getLast(path.iterator());
        StringBuilder sb = new StringBuilder();
        if (target.getSelector().getDependencyMetadata().isConstraint()) {
            sb.append("Constraint path ");
        } else {/*  www .j a va2 s. com*/
            sb.append("Dependency path ");
        }
        boolean first = true;
        for (EdgeState e : path) {
            if (!first) {
                sb.append(" --> ");
            }
            first = false;
            ModuleVersionIdentifier id = e.getFrom().getResolvedConfigurationId().getId();
            sb.append('\'').append(id).append('\'');
        }
        if (includeLast) {
            sb.append(" --> ");
            ModuleIdentifier moduleId = edge.getSelector().getTargetModule().getId();
            sb.append('\'').append(moduleId.getGroup()).append(':').append(moduleId.getName()).append('\'');
        }
        result.add(sb.toString());
    }
    return result;
}