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

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

Introduction

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

Prototype

public final void set(boolean newValue) 

Source Link

Document

Sets the value to newValue , with memory effects as specified by VarHandle#setVolatile .

Usage

From source file:Main.java

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

    atomicBoolean.set(true);

    System.out.println(atomicBoolean);
}

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);/*from  w w  w  .j av  a2  s  .  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:com.arpnetworking.tsdaggregator.TsdAggregator.java

/**
 * Entry point for Time Series Data (TSD) Aggregator.
 *
 * @param args the command line arguments
 *///from   ww  w . j ava2s  .  c  o m
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: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 w w  w. j  a  v  a  2s .c  o  m
    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.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 {// w ww  . j a v a  2  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);
    }

    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 {/*  w  w  w  .j  ava2 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:com.easarrive.aws.plugins.common.service.impl.SimpleProducerConsumer.java

public static void main(String[] args) throws InterruptedException {
    final AWSCredentials credentials = new BasicAWSCredentials("AKIAIDPJMKK4UHLE3OVA",
            "A+cn+TT3tUs6xbto5k1IKkWwPLBq995aOkqKxZNY");

    final String endpoint = "sqs.us-west-2.amazonaws.com";
    final String queueName = "image";
    final int producerCount = 10;
    final int consumerCount = 3;
    final int batchSize = 3;
    final int messageSizeByte = 10000;
    final int runTimeMinutes = 100;

    // configure the SQS client with enough connections for all producer and
    // consumer threads
    AmazonSQS sqsClient = new AmazonSQSClient(credentials,
            new ClientConfiguration().withMaxConnections(producerCount + consumerCount));
    sqsClient.setEndpoint(endpoint);//from ww w .  jav  a2s .  c  o  m
    String queueUrl = sqsClient.getQueueUrl(new GetQueueUrlRequest(queueName)).getQueueUrl();

    // the flag to stop producer, consumer, and monitor threads
    AtomicBoolean stop = new AtomicBoolean(false);

    // start the producers
    final AtomicInteger producedCount = new AtomicInteger();
    Thread[] producers = new Thread[producerCount];
    for (int i = 0; i < producerCount; i++) {
        producers[i] = new BatchProducer(sqsClient, queueUrl, batchSize, messageSizeByte, producedCount, stop);
        producers[i].start();
    }

    // start the consumers
    final AtomicInteger consumedCount = new AtomicInteger();
    Thread[] consumers = new Thread[consumerCount];
    for (int i = 0; i < consumerCount; i++) {
        consumers[i] = new BatchConsumer(sqsClient, queueUrl, batchSize, consumedCount, stop);
        consumers[i].start();
    }

    // start the monitor (thread)
    Thread monitor = new Monitor(producedCount, consumedCount, stop);
    monitor.start();

    // wait for the specified amount of time then stop
    Thread.sleep(TimeUnit.MINUTES.toMillis(Math.min(runTimeMinutes, MAX_RUNTIME_MINUTES)));
    stop.set(true);

    // join all threads
    for (int i = 0; i < producerCount; i++)
        producers[i].join();

    for (int i = 0; i < consumerCount; i++)
        consumers[i].join();

    monitor.interrupt();
    monitor.join();
}

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

/**
 * @param args//from  w ww  . j  a v a  2  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:com.dxc.temp.SimpleProducerConsumer.java

public static void main(String[] args) throws InterruptedException {
    int argIndex = 0;

    final String accessKey = args[argIndex++];
    final String secretKey = args[argIndex++];
    final AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);

    final String endpoint = args[argIndex++];
    final String queueName = args[argIndex++];
    final int producerCount = Integer.parseInt(args[argIndex++]);
    final int consumerCount = Integer.parseInt(args[argIndex++]);
    final int batchSize = Integer.parseInt(args[argIndex++]);
    final int messageSizeByte = Integer.parseInt(args[argIndex++]);
    final int runTimeMinutes = Integer.parseInt(args[argIndex++]);

    // configure the SQS client with enough connections for all producer and
    // consumer threads
    AmazonSQS sqsClient = new AmazonSQSClient(credentials,
            new ClientConfiguration().withMaxConnections(producerCount + consumerCount));
    sqsClient.setEndpoint(endpoint);/*from  ww w.ja v  a2s  .c  om*/
    String queueUrl = sqsClient.getQueueUrl(new GetQueueUrlRequest(queueName)).getQueueUrl();

    // the flag to stop producer, consumer, and monitor threads
    AtomicBoolean stop = new AtomicBoolean(false);

    // start the producers
    final AtomicInteger producedCount = new AtomicInteger();
    Thread[] producers = new Thread[producerCount];
    for (int i = 0; i < producerCount; i++) {
        if (batchSize == 1)
            producers[i] = new Producer(sqsClient, queueUrl, messageSizeByte, producedCount, stop);
        else
            producers[i] = new BatchProducer(sqsClient, queueUrl, batchSize, messageSizeByte, producedCount,
                    stop);
        producers[i].start();
    }

    // start the consumers
    final AtomicInteger consumedCount = new AtomicInteger();
    Thread[] consumers = new Thread[consumerCount];
    for (int i = 0; i < consumerCount; i++) {
        if (batchSize == 1)
            consumers[i] = new Consumer(sqsClient, queueUrl, consumedCount, stop);
        else
            consumers[i] = new BatchConsumer(sqsClient, queueUrl, batchSize, consumedCount, stop);
        consumers[i].start();
    }

    // start the monitor (thread)
    Thread monitor = new Monitor(producedCount, consumedCount, stop);
    monitor.start();

    // wait for the specified amount of time then stop
    Thread.sleep(TimeUnit.MINUTES.toMillis(Math.min(runTimeMinutes, MAX_RUNTIME_MINUTES)));
    stop.set(true);

    // join all threads
    for (int i = 0; i < producerCount; i++)
        producers[i].join();

    for (int i = 0; i < consumerCount; i++)
        consumers[i].join();

    monitor.interrupt();
    monitor.join();
}

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//from ww  w.j  av a2s  . c om
        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());

}