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:edu.umn.msi.tropix.proteomics.tools.ITraqQuantification.java

public static void main(final String[] args) throws Exception {
    if (args.length < 2) {
        usage();/*  w  ww.  j a va 2 s  .c  om*/
        System.exit(-1);
    }

    final List<File> mzxmlFiles = Lists.newArrayListWithCapacity(args.length - 1);
    for (int i = 0; i < args.length - 2; i++) {
        mzxmlFiles.add(new File(args[i]));
    }
    final File scaffoldFile = new File(args[args.length - 2]);
    final File outFile = new File(args[args.length - 1]);
    // TODO: Take in type some how
    final QuantitationOptions options = QuantitationOptions
            .forInput(mzxmlFiles, new InputReport(scaffoldFile, ReportType.SCAFFOLD)).withOutput(outFile)
            .is4Plex().get();

    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "edu/umn/msi/tropix/proteomics/itraqquantitation/applicationContext.xml");
    @SuppressWarnings("unchecked")
    final Closure<QuantitationOptions> closure = (Closure<QuantitationOptions>) context
            .getBean("quantitationClosure");
    closure.apply(options);
}

From source file:org.prebake.client.Main.java

public static void main(String... argv) {
    long t0 = System.nanoTime();
    final Logger logger = Logger.getLogger(Main.class.getName());
    CommandLineArgs args = new CommandLineArgs(argv);
    if (!CommandLineArgs.setUpLogger(args, logger)) {
        System.out.println(USAGE);
        System.exit(0);//from  w w w .  ja  va2s  . c  o m
    }

    if (!args.getFlags().isEmpty()) {
        System.err.println(USAGE);
        if (!args.getFlags().isEmpty()) {
            System.err.println("Unused flags : " + Joiner.on(' ').join(args.getFlags()));
        }
        System.exit(-1);
    }

    Bake bake = new Bake(logger) {

        @Override
        protected Connection connect(int port) throws IOException {
            final Socket socket = new Socket(InetAddress.getLoopbackAddress(), port);
            return new Connection() {
                public InputStream getInputStream() throws IOException {
                    return new FilterInputStream(socket.getInputStream()) {
                        @Override
                        public void close() throws IOException {
                            socket.shutdownInput();
                        }
                    };
                }

                public OutputStream getOutputStream() throws IOException {
                    return new FilterOutputStream(socket.getOutputStream()) {
                        @Override
                        public void close() throws IOException {
                            socket.shutdownOutput();
                        }
                    };
                }

                public void close() throws IOException {
                    socket.close();
                }
            };
        }

        @Override
        protected void launch(Path prebakeDir, List<String> argv) throws IOException {
            Map<String, String> env = Maps.newLinkedHashMap();
            boolean doneWithPreJavaFlags = false;
            List<String> cmd = Lists.newArrayListWithCapacity(argv.size() + 1);
            cmd.add("java");
            for (String arg : argv) {
                if (!doneWithPreJavaFlags && arg.startsWith("-Denv.")) {
                    // See PATH environment variable channeling in CommandLineArgs
                    int eq = arg.indexOf('=');
                    env.put(arg.substring(6, eq).toUpperCase(Locale.ROOT), arg.substring(eq + 1));
                } else {
                    cmd.add(arg);
                }
                if (!arg.startsWith("-")) {
                    doneWithPreJavaFlags = true;
                }
            }
            File logFile = new File(prebakeDir.resolve(FileNames.LOGS).resolve("main.log").toUri());
            logger.log(Level.INFO, "Execing {0} with env {1} > {2}", new Object[] { cmd, env, logFile });
            ProcessBuilder pb = new ProcessBuilder(cmd.toArray(new String[0]));
            pb.environment().putAll(env);
            pb.redirectOutput(logFile).redirectErrorStream(true).start();
        }

        @Override
        protected void sleep(int millis) throws InterruptedException {
            Thread.sleep(millis);
        }
    };
    Path cwd = FileSystems.getDefault().getPath(System.getProperty("user.dir"));
    int result;
    try {
        Path prebakeDir = bake.findPrebakeDir(cwd);
        Collection<String> argVals = args.getValues();
        Commands commands = bake.decodeArgv(prebakeDir.getParent(),
                argVals.toArray(new String[argVals.size()]));
        result = bake.issueCommands(prebakeDir, commands, System.out);
    } catch (IOException ex) {
        ex.printStackTrace();
        result = -1;
    }
    logger.log(Level.INFO, "Build took {0}.", prettyTime(System.nanoTime() - t0));
    System.exit(result);
}

From source file:org.attribyte.api.pubsub.impl.server.Server.java

/**
 * Starts the server.//from w w w.jav  a 2  s .c  om
 * @param args The startup args.
 * @throws Exception on startup error.
 */
public static void main(String[] args) throws Exception {

    if (args.length < 1) {
        System.err.println("Start-up error: Expecting <config file> [allowed topics file]");
        System.exit(1);
    }

    Properties commandLineOverrides = new Properties();
    args = InitUtil.fromCommandLine(args, commandLineOverrides);

    Properties props = new Properties();
    Properties logProps = new Properties();
    CLI.loadProperties(args, props, logProps);

    props.putAll(commandLineOverrides);
    logProps.putAll(commandLineOverrides);

    final Logger logger = initLogger(props, logProps);

    logger.info("Applied command line overrides: " + commandLineOverrides.toString());

    //Buffer and log hub events for logging and debug...

    final int MAX_STORED_SUBSCRIPTION_REQUESTS = 200;

    final ArrayBlockingQueue<SubscriptionEvent> recentSubscriptionRequests = new ArrayBlockingQueue<>(
            MAX_STORED_SUBSCRIPTION_REQUESTS);

    final HubEndpoint.EventHandler hubEventHandler = new HubEndpoint.EventHandler() {
        private synchronized void offer(SubscriptionEvent record) {
            if (!recentSubscriptionRequests.offer(record)) {
                List<SubscriptionEvent> drain = Lists
                        .newArrayListWithCapacity(MAX_STORED_SUBSCRIPTION_REQUESTS / 2);
                recentSubscriptionRequests.drainTo(drain, drain.size());
                recentSubscriptionRequests.offer(record);
            }
        }

        @Override
        public void subscriptionRequestAccepted(final Request request, final Response response,
                final Subscriber subscriber) {
            final SubscriptionEvent record;
            try {
                record = new SubscriptionRequestRecord(request, response, subscriber);
            } catch (IOException ioe) {
                return;
            }

            logger.info(record.toString());
            offer(record);
        }

        @Override
        public void subscriptionRequestRejected(final Request request, final Response response,
                final Subscriber subscriber) {

            final SubscriptionEvent record;
            try {
                record = new SubscriptionRequestRecord(request, response, subscriber);
            } catch (IOException ioe) {
                return;
            }

            logger.warn(record.toString());
            offer(record);
        }

        @Override
        public void subscriptionVerifyFailure(String callbackURL, int callbackResponseCode, String reason,
                int attempts, boolean abandoned) {
            final SubscriptionEvent record = new SubscriptionVerifyRecord(callbackURL, callbackResponseCode,
                    reason, attempts, abandoned);
            logger.warn(record.toString());
            offer(record);
        }

        @Override
        public void subscriptionVerified(Subscription subscription) {
            final SubscriptionEvent record = new SubscriptionVerifyRecord(subscription);
            logger.info(record.toString());
            offer(record);
        }
    };

    /**
     * A source for subscription request records (for console, etc).
     */
    final SubscriptionEvent.Source subscriptionEventSource = new SubscriptionEvent.Source() {
        public List<SubscriptionEvent> latestEvents(int limit) {
            List<SubscriptionEvent> records = Lists.newArrayList(recentSubscriptionRequests);
            Collections.sort(records);
            return records.size() < limit ? records : records.subList(0, limit);
        }
    };

    /**
     * A queue to which new topics are added as reported by the datastore event handler.
     */
    final BlockingQueue<Topic> newTopicQueue = new LinkedBlockingDeque<>();

    /**
     * A datastore event handler that offers new topics to a queue.
     */
    final HubDatastore.EventHandler topicEventHandler = new HubDatastore.EventHandler() {

        @Override
        public void newTopic(final Topic topic) throws DatastoreException {
            newTopicQueue.offer(topic);
        }

        @Override
        public void newSubscription(final Subscription subscription) throws DatastoreException {
            //Ignore
        }

        @Override
        public void exception(final Throwable t) {
            //Ignore
        }

        @Override
        public void setNext(final HubDatastore.EventHandler next) {
            //Ignore
        }
    };

    final HubEndpoint endpoint = new HubEndpoint("endpoint.", props, logger, hubEventHandler,
            topicEventHandler);

    final String topicAddedTopicURL = Strings.emptyToNull(props.getProperty("endpoint.topicAddedTopic", ""));
    final Topic topicAddedTopic = topicAddedTopicURL != null
            ? endpoint.getDatastore().getTopic(topicAddedTopicURL, true)
            : null;
    final Thread topicAddedNotifier = topicAddedTopic != null
            ? new Thread(new TopicAddedNotifier(newTopicQueue, endpoint, topicAddedTopic))
            : null;
    if (topicAddedNotifier != null) {
        topicAddedNotifier.setName("topic-added-notifier");
        topicAddedNotifier.start();
    }

    if (props.getProperty("endpoint.topics") != null) { //Add supported topics...
        for (String topicURL : Splitter.on(",").omitEmptyStrings().trimResults()
                .split(props.getProperty("endpoint.topics"))) {
            Topic topic = endpoint.getDatastore().getTopic(topicURL, true);
            System.out.println("Added topic, '" + topicURL + "' (" + topic.getId() + ")");
        }
    }

    final MetricRegistry registry = props.getProperty("endpoint.instrumentJVM", "true").equalsIgnoreCase("true")
            ? instrumentJVM(new MetricRegistry())
            : new MetricRegistry();

    if (props.getProperty("endpoint.instrumentSystem", "true").equalsIgnoreCase("true")) {
        instrumentSystem(registry);
    }

    registry.registerAll(endpoint);

    final HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry(); //TODO

    final Reporting reporting = new Reporting("metrics-reporting.", props, registry, null); //No filter...

    String httpAddress = props.getProperty("http.address", "127.0.0.1");
    int httpPort = Integer.parseInt(props.getProperty("http.port", "8086"));

    org.eclipse.jetty.server.Server server = new org.eclipse.jetty.server.Server();

    server.addLifeCycleListener(new LifeCycle.Listener() {

        public void lifeCycleFailure(LifeCycle event, Throwable cause) {
            System.out.println("Failure " + cause.toString());
        }

        public void lifeCycleStarted(LifeCycle event) {
            System.out.println("Started...");
        }

        public void lifeCycleStarting(LifeCycle event) {
            System.out.println("Server Starting...");
        }

        public void lifeCycleStopped(LifeCycle event) {
            System.out.println("Server Stopped...");
        }

        public void lifeCycleStopping(LifeCycle event) {
            System.out.println("Shutting down metrics reporting...");
            reporting.stop();
            if (topicAddedNotifier != null) {
                System.out.println("Shutting down new topic notifier...");
                topicAddedNotifier.interrupt();
            }
            System.out.println("Shutting down endpoint...");
            endpoint.shutdown();
            System.out.println("Shutdown endpoint...");
        }
    });

    HttpConfiguration httpConfig = new HttpConfiguration();
    httpConfig.setOutputBufferSize(32768);
    httpConfig.setRequestHeaderSize(8192);
    httpConfig.setResponseHeaderSize(8192);
    httpConfig.setSendServerVersion(false);
    httpConfig.setSendDateHeader(false);
    ServerConnector httpConnector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
    httpConnector.setHost(httpAddress);
    httpConnector.setPort(httpPort);
    httpConnector.setIdleTimeout(30000L);
    server.addConnector(httpConnector);
    HandlerCollection serverHandlers = new HandlerCollection();
    server.setHandler(serverHandlers);

    ServletContextHandler rootContext = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
    rootContext.setContextPath("/");

    final AdminConsole adminConsole;
    final List<String> allowedAssetPaths;

    if (props.getProperty("admin.enabled", "false").equalsIgnoreCase("true")) {

        File assetDirFile = getSystemFile("admin.assetDirectory", props);

        if (assetDirFile == null) {
            System.err.println("The 'admin.assetDirectory' must be configured");
            System.exit(1);
        }

        if (!assetDirFile.exists()) {
            System.err.println("The 'admin.assetDirectory'" + assetDirFile.getAbsolutePath() + "' must exist");
            System.exit(1);
        }

        if (!assetDirFile.isDirectory()) {
            System.err.println(
                    "The 'admin.assetDirectory'" + assetDirFile.getAbsolutePath() + "' must be a directory");
            System.exit(1);
        }

        if (!assetDirFile.canRead()) {
            System.err.println(
                    "The 'admin.assetDirectory'" + assetDirFile.getAbsolutePath() + "' must be readable");
            System.exit(1);
        }

        char[] adminUsername = props.getProperty("admin.username", "").toCharArray();
        char[] adminPassword = props.getProperty("admin.password", "").toCharArray();
        String adminRealm = props.getProperty("admin.realm", "pubsubhub");

        if (adminUsername.length == 0 || adminPassword.length == 0) {
            System.err.println("The 'admin.username' and 'admin.password' must be specified");
            System.exit(1);
        }

        File templateDirFile = getSystemFile("admin.templateDirectory", props);

        if (templateDirFile == null) {
            System.err.println("The 'admin.templateDirectory' must be specified");
            System.exit(1);
        }

        if (!templateDirFile.exists()) {
            System.err
                    .println("The 'admin.templateDirectory'" + assetDirFile.getAbsolutePath() + "' must exist");
            System.exit(1);
        }

        if (!templateDirFile.isDirectory()) {
            System.err.println(
                    "The 'admin.templateDirectory'" + assetDirFile.getAbsolutePath() + "' must be a directory");
            System.exit(1);
        }

        if (!templateDirFile.canRead()) {
            System.err.println(
                    "The 'admin.templateDirectory'" + assetDirFile.getAbsolutePath() + "' must be readable");
            System.exit(1);
        }

        adminConsole = new AdminConsole(rootContext, assetDirFile.getAbsolutePath(), endpoint,
                new AdminAuth(adminRealm, adminUsername, adminPassword), templateDirFile.getAbsolutePath(),
                logger);

        allowedAssetPaths = Lists.newArrayList(Splitter.on(',').omitEmptyStrings().trimResults()
                .split(props.getProperty("admin.assetPaths", "")));
        System.out.println("Admin console is enabled...");
    } else {
        adminConsole = null;
        allowedAssetPaths = ImmutableList.of();
    }

    serverHandlers.addHandler(rootContext);

    //TODO: Introduces incompatible dependency...
    /*
    InstrumentedHandler instrumentedHandler = new InstrumentedHandler(registry);
    instrumentedHandler.setName("http-server");
    instrumentedHandler.setHandler(rootContext);
    serverHandlers.addHandler(instrumentedHandler);
    */

    File requestLogPathFile = getSystemFile("http.log.path", props);
    if (requestLogPathFile != null) {

        if (!requestLogPathFile.exists()) {
            System.err
                    .println("The 'http.log.path', '" + requestLogPathFile.getAbsolutePath() + "' must exist");
            System.exit(1);
        }

        if (!requestLogPathFile.isDirectory()) {
            System.err.println(
                    "The 'http.log.path', '" + requestLogPathFile.getAbsolutePath() + "' must be a directory");
            System.exit(1);
        }

        if (!requestLogPathFile.canWrite()) {
            System.err.println(
                    "The 'http.log.path', '" + requestLogPathFile.getAbsolutePath() + "' is not writable");
            System.exit(1);
        }

        int requestLogRetainDays = Integer.parseInt(props.getProperty("http.log.retainDays", "14"));
        boolean requestLogExtendedFormat = props.getProperty("http.log.extendedFormat", "true")
                .equalsIgnoreCase("true");
        String requestLogTimeZone = props.getProperty("http.log.timeZone", TimeZone.getDefault().getID());
        String requestLogPrefix = props.getProperty("http.log.prefix", "requests");
        String requestLogPath = requestLogPathFile.getAbsolutePath();
        if (!requestLogPath.endsWith("/")) {
            requestLogPath = requestLogPath + "/";
        }

        NCSARequestLog requestLog = new NCSARequestLog(requestLogPath + requestLogPrefix + "-yyyy_mm_dd.log");
        requestLog.setRetainDays(requestLogRetainDays);
        requestLog.setAppend(true);
        requestLog.setExtended(requestLogExtendedFormat);
        requestLog.setLogTimeZone(requestLogTimeZone);
        requestLog.setLogCookies(false);
        requestLog.setPreferProxiedForAddress(true);

        RequestLogHandler requestLogHandler = new RequestLogHandler();
        requestLogHandler.setRequestLog(requestLog);
        serverHandlers.addHandler(requestLogHandler);
    }

    HubServlet hubServlet = new HubServlet(endpoint, logger);
    rootContext.addServlet(new ServletHolder(hubServlet), "/subscribe/*");

    InitUtil filterInit = new InitUtil("publish.", props);
    List<BasicAuthFilter> publishURLFilters = Lists.newArrayList();
    List<Object> publishURLFilterObjects = filterInit.initClassList("topicURLFilters", BasicAuthFilter.class);
    for (Object o : publishURLFilterObjects) {
        BasicAuthFilter filter = (BasicAuthFilter) o;
        filter.init(filterInit.getProperties());
        publishURLFilters.add(filter);
    }

    final long topicCacheMaxAgeSeconds = Long
            .parseLong(props.getProperty("endpoint.topicCache.maxAgeSeconds", "0"));
    final Cache<String, Topic> topicCache;
    if (topicCacheMaxAgeSeconds > 0) {
        topicCache = CacheBuilder.newBuilder().concurrencyLevel(16)
                .expireAfterWrite(topicCacheMaxAgeSeconds, TimeUnit.SECONDS).maximumSize(4096).build();
    } else {
        topicCache = null;
    }

    final String replicationTopicURL = Strings.emptyToNull(props.getProperty("endpoint.replicationTopic", ""));
    //Get or create replication topic, if configured.
    final Topic replicationTopic = replicationTopicURL != null
            ? endpoint.getDatastore().getTopic(replicationTopicURL, true)
            : null;

    int maxBodySizeBytes = filterInit.getIntProperty("maxBodySizeBytes",
            BroadcastServlet.DEFAULT_MAX_BODY_BYTES);
    boolean autocreateTopics = filterInit.getProperty("autocreateTopics", "false").equalsIgnoreCase("true");

    int maxSavedNotifications = filterInit.getIntProperty("maxSavedNotifications", 0);

    boolean jsonEnabled = filterInit.getProperty("jsonEnabled", "false").equalsIgnoreCase("true");

    final BroadcastServlet broadcastServlet = new BroadcastServlet(endpoint, maxBodySizeBytes, autocreateTopics,
            logger, publishURLFilters, topicCache, replicationTopic, maxSavedNotifications, jsonEnabled);
    rootContext.addServlet(new ServletHolder(broadcastServlet), "/notify/*");

    CallbackMetricsServlet callbackMetricsServlet = new CallbackMetricsServlet(endpoint);
    ServletHolder callbackMetricsServletHolder = new ServletHolder(callbackMetricsServlet);
    rootContext.addServlet(callbackMetricsServletHolder, "/metrics/callback/*");

    NotificationMetricsServlet notificationMetricsServlet = new NotificationMetricsServlet(endpoint);
    ServletHolder notificationMetricsServletHolder = new ServletHolder(notificationMetricsServlet);
    rootContext.addServlet(notificationMetricsServletHolder, "/metrics/notification/*");

    MetricsServlet metricsServlet = new MetricsServlet(registry);
    ServletHolder metricsServletHolder = new ServletHolder(metricsServlet);
    rootContext.setInitParameter(MetricsServlet.RATE_UNIT, "SECONDS");
    rootContext.setInitParameter(MetricsServlet.DURATION_UNIT, "MILLISECONDS");
    rootContext.setInitParameter(MetricsServlet.SHOW_SAMPLES, "false");
    rootContext.addServlet(metricsServletHolder, "/metrics/*");

    boolean outputHostAddys = props.getProperty("ping.outputHostAddresses", "false").equalsIgnoreCase("true");
    PingServlet pingServlet = new PingServlet(props.getProperty("http.instanceName", ""), outputHostAddys);
    rootContext.addServlet(new ServletHolder(pingServlet), "/ping/*");

    HealthCheckServlet healthCheckServlet = new HealthCheckServlet(healthCheckRegistry);
    for (Map.Entry<String, HealthCheck> healthCheck : endpoint.getDatastore().getHealthChecks().entrySet()) {
        healthCheckRegistry.register(healthCheck.getKey(), healthCheck.getValue());
    }
    healthCheckRegistry.register("no-deadlocked-threads", new ThreadDeadlockHealthCheck());

    rootContext.addServlet(new ServletHolder(healthCheckServlet), "/health/*");

    ThreadDumpServlet threadDumpServlet = new ThreadDumpServlet();
    rootContext.addServlet(new ServletHolder(threadDumpServlet), "/threads/*");

    if (adminConsole != null && allowedAssetPaths.size() > 0) {
        String adminPath = props.getProperty("admin.path", "/admin/");
        List<Invalidatable> invalidatables = Collections.<Invalidatable>singletonList(new Invalidatable() {
            @Override
            public void invalidate() {
                broadcastServlet.invalidateCaches();
                if (topicCache != null) {
                    topicCache.invalidateAll();
                }
            }
        });
        adminConsole.initServlets(rootContext, adminPath, allowedAssetPaths, invalidatables,
                subscriptionEventSource, broadcastServlet);
    }

    int numReporters = reporting.start();
    logger.info("Started " + numReporters + " metrics reporters");

    server.setDumpBeforeStop(false);
    server.setStopAtShutdown(true);
    server.start();
    server.join();
}

From source file:com.yahoo.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 {//from  w w w.  j  a v  a 2 s  .c  om
        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);
    }

    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:com.ebay.pulsar.analytics.metricstore.druid.query.model.QueryCacheHelper.java

public static byte[] computeAggregatorBytes(List<BaseAggregator> aggregators) {
    List<byte[]> cacheKeySet = Lists.newArrayListWithCapacity(aggregators.size());

    int totalSize = 0;
    for (BaseAggregator agg : aggregators) {
        final byte[] cacheKey = agg.cacheKey();
        cacheKeySet.add(cacheKey);//from   ww w  .j  a va 2 s  .  c om
        totalSize += cacheKey.length;
    }

    ByteBuffer retVal = ByteBuffer.allocate(totalSize);
    for (byte[] bytes : cacheKeySet) {
        retVal.put(bytes);
    }
    return retVal.array();
}

From source file:io.druid.query.QueryCacheHelper.java

public static byte[] computeAggregatorBytes(List<AggregatorFactory> aggregatorSpecs) {
    List<byte[]> cacheKeySet = Lists.newArrayListWithCapacity(aggregatorSpecs.size());

    int totalSize = 0;
    for (AggregatorFactory spec : aggregatorSpecs) {
        final byte[] cacheKey = spec.getCacheKey();
        cacheKeySet.add(cacheKey);//from   ww w . j a v a 2 s.c o m
        totalSize += cacheKey.length;
    }

    ByteBuffer retVal = ByteBuffer.allocate(totalSize);
    for (byte[] bytes : cacheKeySet) {
        retVal.put(bytes);
    }
    return retVal.array();
}

From source file:com.palantir.atlasdb.table.generation.Triggers.java

public static <T, U extends T> List<T> getAllTriggers(Transaction t,
        List<Function<? super Transaction, U>> sharedTriggers, T[] triggers) {

    List<T> allTriggers = Lists.newArrayListWithCapacity(sharedTriggers.size() + triggers.length);
    for (T trigger : triggers) {
        allTriggers.add(trigger);/*from  w  w w  .ja va2 s .  c o m*/
    }
    for (Function<? super Transaction, ? extends T> sharedTrigger : sharedTriggers) {
        allTriggers.add(sharedTrigger.apply(t));
    }
    return allTriggers;
}

From source file:nebula.plugin.metrics.model.KeyValue.java

static List<KeyValue> mapToKeyValueList(Map<String, String> map) {
    Set<Map.Entry<String, String>> entries = map.entrySet();
    List<KeyValue> keyValues = Lists.newArrayListWithCapacity(entries.size());
    for (Map.Entry<String, String> entry : entries) {
        keyValues.add(entryToKeyValue(entry));
    }//from   w  w  w . j av  a2  s.  com
    return keyValues;
}

From source file:org.graylog.plugins.collector.collectors.Collectors.java

public static List<CollectorSummary> toSummaryList(List<Collector> collectors,
        Function<Collector, Boolean> isActiveFunction) {
    final List<CollectorSummary> collectorSummaries = Lists.newArrayListWithCapacity(collectors.size());
    for (Collector collector : collectors)
        collectorSummaries.add(collector.toSummary(isActiveFunction));

    return collectorSummaries;
}

From source file:com.google.gerrit.server.query.change.IsStarredByLegacyPredicate.java

private static List<Predicate<ChangeData>> predicates(Set<Change.Id> ids) {
    List<Predicate<ChangeData>> r = Lists.newArrayListWithCapacity(ids.size());
    for (Change.Id id : ids) {
        r.add(new LegacyChangeIdPredicate(id));
    }/*from  w ww  . j  a  v  a  2s .  c om*/
    return r;
}