Example usage for java.util.concurrent.atomic AtomicBoolean get

List of usage examples for java.util.concurrent.atomic AtomicBoolean get

Introduction

In this page you can find the example usage for java.util.concurrent.atomic AtomicBoolean get.

Prototype

public final boolean get() 

Source Link

Document

Returns the current value, with memory effects as specified by VarHandle#getVolatile .

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    AtomicBoolean atomicBoolean = new AtomicBoolean();

    System.out.println(atomicBoolean.get());
}

From source file:org.apache.tinkerpop.gremlin.driver.util.ProfilingApplication.java

public static void main(final String[] args) {
    final Map<String, Object> options = ElementHelper.asMap(args);
    final boolean noExit = Boolean.parseBoolean(options.getOrDefault("noExit", "false").toString());
    final int parallelism = Integer.parseInt(options.getOrDefault("parallelism", "16").toString());
    final BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("profiler-%d")
            .build();/*from   www.  j av  a  2s  .c om*/
    final ExecutorService executor = Executors.newFixedThreadPool(parallelism, threadFactory);

    final String host = options.getOrDefault("host", "localhost").toString();
    final int minExpectedRps = Integer.parseInt(options.getOrDefault("minExpectedRps", "1000").toString());
    final int timeout = Integer.parseInt(options.getOrDefault("timeout", "1200000").toString());
    final int warmups = Integer.parseInt(options.getOrDefault("warmups", "5").toString());
    final int executions = Integer.parseInt(options.getOrDefault("executions", "10").toString());
    final int nioPoolSize = Integer.parseInt(options.getOrDefault("nioPoolSize", "1").toString());
    final int requests = Integer.parseInt(options.getOrDefault("requests", "10000").toString());
    final int minConnectionPoolSize = Integer
            .parseInt(options.getOrDefault("minConnectionPoolSize", "256").toString());
    final int maxConnectionPoolSize = Integer
            .parseInt(options.getOrDefault("maxConnectionPoolSize", "256").toString());
    final int minSimultaneousUsagePerConnection = Integer
            .parseInt(options.getOrDefault("minSimultaneousUsagePerConnection", "8").toString());
    final int maxSimultaneousUsagePerConnection = Integer
            .parseInt(options.getOrDefault("maxSimultaneousUsagePerConnection", "32").toString());
    final int maxInProcessPerConnection = Integer
            .parseInt(options.getOrDefault("maxInProcessPerConnection", "64").toString());
    final int minInProcessPerConnection = Integer
            .parseInt(options.getOrDefault("minInProcessPerConnection", "16").toString());
    final int maxWaitForConnection = Integer
            .parseInt(options.getOrDefault("maxWaitForConnection", "3000").toString());
    final int workerPoolSize = Integer.parseInt(options.getOrDefault("workerPoolSize", "2").toString());
    final int tooSlowThreshold = Integer.parseInt(options.getOrDefault("tooSlowThreshold", "125").toString());
    final String channelizer = options
            .getOrDefault("channelizer", Channelizer.WebSocketChannelizer.class.getName()).toString();
    final String serializer = options.getOrDefault("serializer", Serializers.GRYO_V1D0.name()).toString();

    final String script = options.getOrDefault("script", "1+1").toString();

    final Cluster cluster = Cluster.build(host).minConnectionPoolSize(minConnectionPoolSize)
            .maxConnectionPoolSize(maxConnectionPoolSize)
            .minSimultaneousUsagePerConnection(minSimultaneousUsagePerConnection)
            .maxSimultaneousUsagePerConnection(maxSimultaneousUsagePerConnection)
            .minInProcessPerConnection(minInProcessPerConnection)
            .maxInProcessPerConnection(maxInProcessPerConnection).nioPoolSize(nioPoolSize)
            .channelizer(channelizer).maxWaitForConnection(maxWaitForConnection)
            .serializer(Serializers.valueOf(serializer)).workerPoolSize(workerPoolSize).create();

    try {
        final Object fileName = options.get("store");
        final File f = null == fileName ? null : new File(fileName.toString());
        if (f != null && f.length() == 0) {
            try (final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(f, true)))) {
                writer.println(
                        "parallelism\tnioPoolSize\tminConnectionPoolSize\tmaxConnectionPoolSize\tminSimultaneousUsagePerConnection\tmaxSimultaneousUsagePerConnection\tminInProcessPerConnection\tmaxInProcessPerConnection\tworkerPoolSize\trequestPerSecond");
            }
        }

        // not much point to continuing with a line of tests if we can't get at least minExpectedRps.
        final AtomicBoolean meetsRpsExpectation = new AtomicBoolean(true);
        System.out.println("---------------------------WARMUP CYCLE---------------------------");
        for (int ix = 0; ix < warmups && meetsRpsExpectation.get(); ix++) {
            final long averageRequestsPerSecond = new ProfilingApplication("warmup-" + (ix + 1), cluster, 1000,
                    executor, script, tooSlowThreshold).execute();
            meetsRpsExpectation.set(averageRequestsPerSecond > minExpectedRps);
            TimeUnit.SECONDS.sleep(1); // pause between executions
        }

        final AtomicBoolean exceededTimeout = new AtomicBoolean(false);
        long totalRequestsPerSecond = 0;

        // no need to execute this if we didn't pass the basic expectation in the warmups
        if (meetsRpsExpectation.get()) {
            final long start = System.nanoTime();
            System.out.println("----------------------------TEST CYCLE----------------------------");
            for (int ix = 0; ix < executions && !exceededTimeout.get(); ix++) {
                totalRequestsPerSecond += new ProfilingApplication("test-" + (ix + 1), cluster, requests,
                        executor, script, tooSlowThreshold).execute();
                exceededTimeout.set((System.nanoTime() - start) > TimeUnit.NANOSECONDS.convert(timeout,
                        TimeUnit.MILLISECONDS));
                TimeUnit.SECONDS.sleep(1); // pause between executions
            }
        }

        final int averageRequestPerSecond = !meetsRpsExpectation.get() || exceededTimeout.get() ? 0
                : Math.round(totalRequestsPerSecond / executions);
        System.out.println(String.format("avg req/sec: %s", averageRequestPerSecond));
        if (f != null) {
            try (final PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(f, true)))) {
                writer.println(String.join("\t", String.valueOf(parallelism), String.valueOf(nioPoolSize),
                        String.valueOf(minConnectionPoolSize), String.valueOf(maxConnectionPoolSize),
                        String.valueOf(minSimultaneousUsagePerConnection),
                        String.valueOf(maxSimultaneousUsagePerConnection),
                        String.valueOf(minInProcessPerConnection), String.valueOf(maxInProcessPerConnection),
                        String.valueOf(workerPoolSize), String.valueOf(averageRequestPerSecond)));
            }
        }

        if (!noExit)
            System.exit(0);
    } catch (Exception ex) {
        ex.printStackTrace();
        if (!noExit)
            System.exit(1);
    } finally {
        executor.shutdown();
        cluster.close();
    }
}

From source file:com.arpnetworking.tsdaggregator.TsdAggregator.java

/**
 * Entry point for Time Series Data (TSD) Aggregator.
 *
 * @param args the command line arguments
 */// w  w w .  j  a  v a2 s . c om
public static void main(final String[] args) {
    LOGGER.info("Launching tsd-aggregator");

    // Global initialization
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(final Thread thread, final Throwable throwable) {
            LOGGER.error("Unhandled exception!", throwable);
        }
    });

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
            context.stop();
        }
    }));

    System.setProperty("org.vertx.logger-delegate-factory-class-name",
            "org.vertx.java.core.logging.impl.SLF4JLogDelegateFactory");

    // Run the tsd aggregator
    if (args.length != 1) {
        throw new RuntimeException("No configuration file specified");
    }
    LOGGER.debug(String.format("Loading configuration from file; file=%s", args[0]));

    final File configurationFile = new File(args[0]);
    final Configurator<TsdAggregator, TsdAggregatorConfiguration> configurator = new Configurator<>(
            TsdAggregator.class, TsdAggregatorConfiguration.class);
    final ObjectMapper objectMapper = TsdAggregatorConfiguration.createObjectMapper();
    final DynamicConfiguration configuration = new DynamicConfiguration.Builder().setObjectMapper(objectMapper)
            .addSourceBuilder(
                    new JsonNodeFileSource.Builder().setObjectMapper(objectMapper).setFile(configurationFile))
            .addTrigger(new FileTrigger.Builder().setFile(configurationFile).build()).addListener(configurator)
            .build();

    configuration.launch();

    final AtomicBoolean isRunning = new AtomicBoolean(true);
    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            LOGGER.info("Stopping tsd-aggregator");
            configuration.shutdown();
            configurator.shutdown();
            isRunning.set(false);
        }
    }));

    while (isRunning.get()) {
        try {
            Thread.sleep(30000);
        } catch (final InterruptedException e) {
            break;
        }
    }

    LOGGER.info("Exiting tsd-aggregator");
}

From source file:de.codesourcery.eve.skills.ui.utils.CalendarWidget.java

public static void main(String[] args) throws Exception {

    final SimpleDateFormat DF = new SimpleDateFormat("dd.MM");

    final Calendar specialDate = Calendar.getInstance();
    specialDate.add(Calendar.DAY_OF_MONTH, 5);

    final AtomicBoolean doStuff = new AtomicBoolean(false);

    final ICalendarRenderer renderer = new ICalendarRenderer() {

        @Override// w  w w  .java  2  s  .c o m
        public String getDateLabel(Date date) {
            return DF.format(date);
        }

        @Override
        public String getText(Date date) {
            if (DateUtils.isSameDay(date, specialDate.getTime()) && doStuff.get()) {
                return "SPECIAL !!!";
            }
            return "some\nmultiline\ntext";
        }

        @Override
        public String getToolTip(Date date) {
            return getText(date);
        }

        @Override
        public Color getTextColor(Date date) {
            return Color.RED;
        }
    };

    final JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new GridBagLayout());

    final CalendarWidget widget = new CalendarWidget(new Date(), renderer);

    widget.addSelectionListener(new ISelectionListener<Date>() {

        @Override
        public void selectionChanged(Date selected) {
            System.out.println("Selected date > " + selected);
        }
    });
    frame.getContentPane().add(widget, new ConstraintsBuilder().end());
    frame.pack();
    frame.setVisible(true);

    java.lang.Thread.sleep(2 * 1000);
    doStuff.set(true);
    widget.refreshDateLabel(specialDate.getTime());

}

From source file:com.mapr.synth.Synth.java

public static void main(String[] args)
        throws IOException, CmdLineException, InterruptedException, ExecutionException {
    final Options opts = new Options();
    CmdLineParser parser = new CmdLineParser(opts);
    try {/*ww w. j a v  a2s  .c  o  m*/
        parser.parseArgument(args);
    } catch (CmdLineException e) {
        System.err.println("Usage: " + "[ -count <number>G|M|K ] " + "-schema schema-file "
                + "[-quote DOUBLE_QUOTE|BACK_SLASH|OPTIMISTIC] " + "[-format JSON|TSV|CSV|XML ] "
                + "[-threads n] " + "[-output output-directory-name] ");
        throw e;
    }
    Preconditions.checkArgument(opts.threads > 0 && opts.threads <= 2000,
            "Must have at least one thread and no more than 2000");

    if (opts.threads > 1) {
        Preconditions.checkArgument(!"-".equals(opts.output),
                "If more than on thread is used, you have to use -output to set the output directory");
    }

    File outputDir = new File(opts.output);
    if (!"-".equals(opts.output)) {
        if (!outputDir.exists()) {
            Preconditions.checkState(outputDir.mkdirs(),
                    String.format("Couldn't create output directory %s", opts.output));
        }
        Preconditions.checkArgument(outputDir.exists() && outputDir.isDirectory(),
                String.format("Couldn't create directory %s", opts.output));
    }

    if (opts.schema == null) {
        throw new IllegalArgumentException("Must specify schema file using [-schema filename] option");
    }
    final SchemaSampler sampler = new SchemaSampler(opts.schema);
    final AtomicLong rowCount = new AtomicLong();

    final List<ReportingWorker> tasks = Lists.newArrayList();
    int limit = (opts.count + opts.threads - 1) / opts.threads;
    int remaining = opts.count;
    for (int i = 0; i < opts.threads; i++) {

        final int count = Math.min(limit, remaining);
        remaining -= count;

        tasks.add(new ReportingWorker(opts, sampler, rowCount, count, i));
    }

    final double t0 = System.nanoTime() * 1e-9;
    ExecutorService pool = Executors.newFixedThreadPool(opts.threads);
    ScheduledExecutorService blinker = Executors.newScheduledThreadPool(1);
    final AtomicBoolean finalRun = new AtomicBoolean(false);

    final PrintStream sideLog = new PrintStream(new FileOutputStream("side-log"));
    Runnable blink = new Runnable() {
        public double oldT;
        private long oldN;

        @Override
        public void run() {
            double t = System.nanoTime() * 1e-9;
            long n = rowCount.get();
            System.err.printf("%s\t%d\t%.1f\t%d\t%.1f\t%.3f\n", finalRun.get() ? "F" : "R", opts.threads,
                    t - t0, n, n / (t - t0), (n - oldN) / (t - oldT));
            for (ReportingWorker task : tasks) {
                ReportingWorker.ThreadReport r = task.report();
                sideLog.printf("\t%d\t%.2f\t%.2f\t%.2f\t%.1f\t%.1f\n", r.fileNumber, r.threadTime, r.userTime,
                        r.wallTime, r.rows / r.threadTime, r.rows / r.wallTime);
            }
            oldN = n;
            oldT = t;
        }
    };
    if (!"-".equals(opts.output)) {
        blinker.scheduleAtFixedRate(blink, 0, 10, TimeUnit.SECONDS);
    }
    List<Future<Integer>> results = pool.invokeAll(tasks);

    int total = 0;
    for (Future<Integer> result : results) {
        total += result.get();
    }
    Preconditions.checkState(total == opts.count, String
            .format("Expected to generate %d lines of output, but actually generated %d", opts.count, total));
    pool.shutdownNow();
    blinker.shutdownNow();
    finalRun.set(true);
    sideLog.close();
    blink.run();
}

From source file:org.apache.accumulo.server.util.FindOfflineTablets.java

/**
 * @param args//from   www  . j  a  v  a2  s  .  co  m
 */
public static void main(String[] args) throws Exception {
    ClientOpts opts = new ClientOpts();
    opts.parseArgs(FindOfflineTablets.class.getName(), args);
    final AtomicBoolean scanning = new AtomicBoolean(false);
    Instance instance = opts.getInstance();
    MetaDataTableScanner rootScanner = new MetaDataTableScanner(instance,
            SecurityConstants.getSystemCredentials(), Constants.METADATA_ROOT_TABLET_KEYSPACE);
    MetaDataTableScanner metaScanner = new MetaDataTableScanner(instance,
            SecurityConstants.getSystemCredentials(), Constants.NON_ROOT_METADATA_KEYSPACE);
    @SuppressWarnings("unchecked")
    Iterator<TabletLocationState> scanner = (Iterator<TabletLocationState>) new IteratorChain(rootScanner,
            metaScanner);
    LiveTServerSet tservers = new LiveTServerSet(instance, DefaultConfiguration.getDefaultConfiguration(),
            new Listener() {
                @Override
                public void update(LiveTServerSet current, Set<TServerInstance> deleted,
                        Set<TServerInstance> added) {
                    if (!deleted.isEmpty() && scanning.get())
                        log.warn("Tablet servers deleted while scanning: " + deleted);
                    if (!added.isEmpty() && scanning.get())
                        log.warn("Tablet servers added while scanning: " + added);
                }
            });
    tservers.startListeningForTabletServerChanges();
    scanning.set(true);
    while (scanner.hasNext()) {
        TabletLocationState locationState = scanner.next();
        TabletState state = locationState.getState(tservers.getCurrentServers());
        if (state != null && state != TabletState.HOSTED && TableManager.getInstance()
                .getTableState(locationState.extent.getTableId().toString()) != TableState.OFFLINE)
            if (!locationState.extent.equals(Constants.ROOT_TABLET_EXTENT))
                System.out.println(locationState + " is " + state + "  #walogs:" + locationState.walogs.size());
    }
}

From source file:accumulo.AccumuloStuff.java

public static void main(String[] args) throws Exception {
    File tmp = new File(System.getProperty("user.dir") + "/target/mac-test");
    if (tmp.exists()) {
        FileUtils.deleteDirectory(tmp);//ww w .j  a  va 2s . co m
    }
    tmp.mkdirs();
    String passwd = "password";

    MiniAccumuloConfigImpl cfg = new MiniAccumuloConfigImpl(tmp, passwd);
    cfg.setNumTservers(1);
    //    cfg.useMiniDFS(true);

    final MiniAccumuloClusterImpl cluster = cfg.build();
    setCoreSite(cluster);
    cluster.start();

    ExecutorService svc = Executors.newFixedThreadPool(2);

    try {
        Connector conn = cluster.getConnector("root", passwd);
        String table = "table";
        conn.tableOperations().create(table);

        final BatchWriter bw = conn.createBatchWriter(table, new BatchWriterConfig());
        final AtomicBoolean flushed = new AtomicBoolean(false);

        Runnable writer = new Runnable() {
            @Override
            public void run() {
                try {
                    Mutation m = new Mutation("row");
                    m.put("colf", "colq", "value");
                    bw.addMutation(m);
                    bw.flush();
                    flushed.set(true);
                } catch (Exception e) {
                    log.error("Got exception trying to flush mutation", e);
                }

                log.info("Exiting batchwriter thread");
            }
        };

        Runnable restarter = new Runnable() {
            @Override
            public void run() {
                try {
                    for (ProcessReference proc : cluster.getProcesses().get(ServerType.TABLET_SERVER)) {
                        cluster.killProcess(ServerType.TABLET_SERVER, proc);
                    }
                    cluster.exec(TabletServer.class);
                } catch (Exception e) {
                    log.error("Caught exception restarting tabletserver", e);
                }
                log.info("Exiting restart thread");
            }
        };

        svc.execute(writer);
        svc.execute(restarter);

        log.info("Waiting for shutdown");
        svc.shutdown();
        if (!svc.awaitTermination(120, TimeUnit.SECONDS)) {
            log.info("Timeout on shutdown exceeded");
            svc.shutdownNow();
        } else {
            log.info("Cleanly shutdown");
            log.info("Threadpool is terminated? " + svc.isTerminated());
        }

        if (flushed.get()) {
            log.info("****** BatchWriter was flushed *********");
        } else {
            log.info("****** BatchWriter was NOT flushed *********");
        }

        bw.close();

        log.info("Got record {}", Iterables.getOnlyElement(conn.createScanner(table, Authorizations.EMPTY)));
    } finally {
        cluster.stop();
    }
}

From source file:org.eclipse.swt.snippets.Snippet141.java

public static void main(String[] args) {
    display = new Display();
    shell = new Shell(display);
    shell.setText("Snippet 141");
    shell.setSize(300, 300);/* w ww.  j  a  va 2  s  .c o  m*/
    shell.open();
    shellGC = new GC(shell);
    shellBackground = shell.getBackground();

    FileDialog dialog = new FileDialog(shell);
    dialog.setFilterExtensions(new String[] { "*.gif" });
    String fileName = dialog.open();
    final AtomicBoolean stopAnimation = new AtomicBoolean(false);
    if (fileName != null) {
        loader = new ImageLoader();
        try {
            imageDataArray = loader.load(fileName);
            if (imageDataArray.length > 1) {
                animateThread = new Thread("Animation") {
                    @Override
                    public void run() {
                        /* Create an off-screen image to draw on, and fill it with the shell background. */
                        Image offScreenImage = new Image(display, loader.logicalScreenWidth,
                                loader.logicalScreenHeight);
                        GC offScreenImageGC = new GC(offScreenImage);
                        offScreenImageGC.setBackground(shellBackground);
                        offScreenImageGC.fillRectangle(0, 0, loader.logicalScreenWidth,
                                loader.logicalScreenHeight);

                        try {
                            /* Create the first image and draw it on the off-screen image. */
                            int imageDataIndex = 0;
                            ImageData imageData = imageDataArray[imageDataIndex];
                            if (image != null && !image.isDisposed())
                                image.dispose();
                            image = new Image(display, imageData);
                            offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
                                    imageData.x, imageData.y, imageData.width, imageData.height);

                            /* Now loop through the images, creating and drawing each one
                             * on the off-screen image before drawing it on the shell. */
                            int repeatCount = loader.repeatCount;
                            while ((loader.repeatCount == 0 || repeatCount > 0) && !stopAnimation.get()) {
                                switch (imageData.disposalMethod) {
                                case SWT.DM_FILL_BACKGROUND:
                                    /* Fill with the background color before drawing. */
                                    Color bgColor = null;
                                    if (useGIFBackground && loader.backgroundPixel != -1) {
                                        bgColor = new Color(display,
                                                imageData.palette.getRGB(loader.backgroundPixel));
                                    }
                                    offScreenImageGC.setBackground(bgColor != null ? bgColor : shellBackground);
                                    offScreenImageGC.fillRectangle(imageData.x, imageData.y, imageData.width,
                                            imageData.height);
                                    if (bgColor != null)
                                        bgColor.dispose();
                                    break;
                                case SWT.DM_FILL_PREVIOUS:
                                    /* Restore the previous image before drawing. */
                                    offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
                                            imageData.x, imageData.y, imageData.width, imageData.height);
                                    break;
                                }

                                imageDataIndex = (imageDataIndex + 1) % imageDataArray.length;
                                imageData = imageDataArray[imageDataIndex];
                                image.dispose();
                                image = new Image(display, imageData);
                                offScreenImageGC.drawImage(image, 0, 0, imageData.width, imageData.height,
                                        imageData.x, imageData.y, imageData.width, imageData.height);

                                /* Draw the off-screen image to the shell. */
                                shellGC.drawImage(offScreenImage, 0, 0);

                                /* Sleep for the specified delay time (adding commonly-used slow-down fudge factors). */
                                try {
                                    int ms = imageData.delayTime * 10;
                                    if (ms < 20)
                                        ms += 30;
                                    if (ms < 30)
                                        ms += 10;
                                    Thread.sleep(ms);
                                } catch (InterruptedException e) {
                                }

                                /* If we have just drawn the last image, decrement the repeat count and start again. */
                                if (imageDataIndex == imageDataArray.length - 1)
                                    repeatCount--;
                            }
                        } catch (SWTException ex) {
                            System.out.println("There was an error animating the GIF");
                        } finally {
                            if (offScreenImage != null && !offScreenImage.isDisposed())
                                offScreenImage.dispose();
                            if (offScreenImageGC != null && !offScreenImageGC.isDisposed())
                                offScreenImageGC.dispose();
                            if (image != null && !image.isDisposed())
                                image.dispose();
                        }
                    }
                };
                animateThread.setDaemon(true);
                animateThread.start();
            }
        } catch (SWTException ex) {
            System.out.println("There was an error loading the GIF");
        }
    }

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    stopAnimation.set(true);
    display.dispose();
}

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  ww w. j a v a2  s  .  co  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);
    }

    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: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 {//from w  w  w .j  av  a2s. 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);
    }

    // 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();
}