Search in sources :

Example 11 with JCommander

use of com.beust.jcommander.JCommander in project jslint4java by happygiraffe.

the class Main method processOptions.

private List<String> processOptions(String[] args) {
    JSLintFlags jslintFlags = new JSLintFlags();
    Flags flags = new Flags();
    JCommander jc = new JCommander(new Object[] { flags, jslintFlags });
    jc.setProgramName(FULL_PROGRAM_NAME);
    try {
        jc.parse(args);
    } catch (ParameterException e) {
        info(e.getMessage());
        usage(jc);
    }
    if (flags.version) {
        version();
    }
    if (flags.help) {
        usage(jc);
    }
    if (flags.encoding != null) {
        encoding = flags.encoding;
    }
    lint = makeLint(flags);
    setResultFormatter(flags.report);
    for (ParameterDescription pd : jc.getParameters()) {
        Parameterized p = pd.getParameterized();
        // Is it declared on JSLintFlags?
        if (!pd.getObject().getClass().isAssignableFrom(JSLintFlags.class)) {
            continue;
        }
        try {
            // Need to get Option.
            Option o = getOption(p.getName());
            // Need to get value.
            Object val = p.get(jslintFlags);
            if (val == null) {
                continue;
            }
            Class<?> type = p.getType();
            if (type.isAssignableFrom(Boolean.class)) {
                lint.addOption(o);
            } else // In theory, everything else should be a String for later parsing.
            if (type.isAssignableFrom(String.class)) {
                lint.addOption(o, (String) val);
            } else {
                die("unknown type \"" + type + "\" (for " + p.getName() + ")");
            }
        } catch (IllegalArgumentException e) {
            die(e.getMessage());
        }
    }
    if (flags.files.isEmpty()) {
        usage(jc);
        // can never happen
        return null;
    } else {
        return flags.files;
    }
}
Also used : Parameterized(com.beust.jcommander.Parameterized) JCommander(com.beust.jcommander.JCommander) ParameterException(com.beust.jcommander.ParameterException) Option(com.googlecode.jslint4java.Option) ParameterDescription(com.beust.jcommander.ParameterDescription)

Example 12 with JCommander

use of com.beust.jcommander.JCommander in project error-prone by google.

the class DocGenTool method main.

public static void main(String[] args) throws IOException {
    Options options = new Options();
    new JCommander(options, args);
    Path bugPatterns = Paths.get(options.bugPatterns);
    if (!Files.exists(bugPatterns)) {
        usage("Cannot find bugPatterns file: " + options.bugPatterns);
    }
    Path explanationDir = Paths.get(options.explanations);
    if (!Files.exists(explanationDir)) {
        usage("Cannot find explanations dir: " + options.explanations);
    }
    Path wikiDir = Paths.get(options.docsRepository);
    Files.createDirectories(wikiDir);
    Path exampleDirBase = Paths.get(options.examplesDir);
    if (!Files.exists(exampleDirBase)) {
        usage("Cannot find example directory: " + options.examplesDir);
    }
    Path bugpatternDir = wikiDir.resolve("bugpattern");
    if (!Files.exists(bugpatternDir)) {
        Files.createDirectories(bugpatternDir);
    }
    Files.createDirectories(wikiDir.resolve("_data"));
    BugPatternFileGenerator generator = new BugPatternFileGenerator(bugpatternDir, exampleDirBase, explanationDir, options.target == Target.EXTERNAL, options.usePygments, options.baseUrl);
    try (Writer w = Files.newBufferedWriter(wikiDir.resolve("bugpatterns.md"), StandardCharsets.UTF_8)) {
        List<BugPatternInstance> patterns = readLines(bugPatterns.toFile(), UTF_8, generator);
        new BugPatternIndexWriter().dump(patterns, w, options.target, enabledChecks());
    }
}
Also used : Path(java.nio.file.Path) JCommander(com.beust.jcommander.JCommander) Writer(java.io.Writer)

Example 13 with JCommander

use of com.beust.jcommander.JCommander in project pulsar by yahoo.

the class CmdBase method run.

public boolean run(String[] args) {
    try {
        jcommander.parse(args);
    } catch (Exception e) {
        System.err.println(e.getMessage());
        System.err.println();
        jcommander.usage();
        return false;
    }
    String cmd = jcommander.getParsedCommand();
    if (cmd == null) {
        jcommander.usage();
        return false;
    } else {
        JCommander obj = jcommander.getCommands().get(cmd);
        CliCommand cmdObj = (CliCommand) obj.getObjects().get(0);
        try {
            cmdObj.run();
            return true;
        } catch (ParameterException e) {
            System.err.println(e.getMessage());
            System.err.println();
            jcommander.usage();
            return false;
        } catch (ConnectException e) {
            System.err.println(e.getMessage());
            System.err.println();
            System.err.println("Error connecting to: " + admin.getServiceUrl());
            return false;
        } catch (PulsarAdminException e) {
            System.err.println(e.getHttpError());
            System.err.println();
            System.err.println("Reason: " + e.getMessage());
            return false;
        } catch (Exception e) {
            System.err.println("Got exception: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
}
Also used : JCommander(com.beust.jcommander.JCommander) ParameterException(com.beust.jcommander.ParameterException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) ParameterException(com.beust.jcommander.ParameterException) ConnectException(com.yahoo.pulsar.client.admin.PulsarAdminException.ConnectException) PulsarAdminException(com.yahoo.pulsar.client.admin.PulsarAdminException) ConnectException(com.yahoo.pulsar.client.admin.PulsarAdminException.ConnectException)

Example 14 with JCommander

use of com.beust.jcommander.JCommander in project pulsar by yahoo.

the class PulsarAdminTool method run.

boolean run(String[] args) {
    if (args.length == 0) {
        jcommander.usage();
        return false;
    }
    try {
        jcommander.parse(new String[] { args[0] });
    } catch (Exception e) {
        System.err.println(e.getMessage());
        System.err.println();
        jcommander.usage();
        return false;
    }
    if (help) {
        jcommander.usage();
        return false;
    }
    if (jcommander.getParsedCommand() == null) {
        jcommander.usage();
        return false;
    } else {
        String cmd = jcommander.getParsedCommand();
        JCommander obj = jcommander.getCommands().get(cmd);
        CmdBase cmdObj = (CmdBase) obj.getObjects().get(0);
        return cmdObj.run(Arrays.copyOfRange(args, 1, args.length));
    }
}
Also used : JCommander(com.beust.jcommander.JCommander) MalformedURLException(java.net.MalformedURLException)

Example 15 with JCommander

use of com.beust.jcommander.JCommander in project pulsar by yahoo.

the class PerformanceProducer method main.

public static void main(String[] args) throws Exception {
    final Arguments arguments = new Arguments();
    JCommander jc = new JCommander(arguments);
    jc.setProgramName("pulsar-perf-producer");
    try {
        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();
}
Also used : Histogram(org.HdrHistogram.Histogram) ProducerConfiguration(com.yahoo.pulsar.client.api.ProducerConfiguration) Properties(java.util.Properties) DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) JCommander(com.beust.jcommander.JCommander) ParameterException(com.beust.jcommander.ParameterException) PulsarClient(com.yahoo.pulsar.client.api.PulsarClient) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) HistogramLogWriter(org.HdrHistogram.HistogramLogWriter) PrintStream(java.io.PrintStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) FileInputStream(java.io.FileInputStream) RateLimiter(com.google.common.util.concurrent.RateLimiter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Producer(com.yahoo.pulsar.client.api.Producer) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) FileOutputStream(java.io.FileOutputStream) Future(java.util.concurrent.Future) PulsarClientImpl(com.yahoo.pulsar.client.impl.PulsarClientImpl) ClientConfiguration(com.yahoo.pulsar.client.api.ClientConfiguration)

Aggregations

JCommander (com.beust.jcommander.JCommander)65 ParameterException (com.beust.jcommander.ParameterException)15 IOException (java.io.IOException)10 Map (java.util.Map)6 ParameterDescription (com.beust.jcommander.ParameterDescription)4 FileInputStream (java.io.FileInputStream)3 Console (com.beust.jcommander.internal.Console)2 DefaultCommand (com.evolveum.midpoint.cli.common.DefaultCommand)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Charsets (com.google.common.base.Charsets)2 Throwables (com.google.common.base.Throwables)2 HostAndPort (com.google.common.net.HostAndPort)2 Service (com.google.common.util.concurrent.Service)2 CreationException (com.google.inject.CreationException)2 SystemException (com.torodb.core.exceptions.SystemException)2 BackendPasswordConfig (com.torodb.packaging.config.model.backend.BackendPasswordConfig)2 AbstractDerby (com.torodb.packaging.config.model.backend.derby.AbstractDerby)2 AbstractPostgres (com.torodb.packaging.config.model.backend.postgres.AbstractPostgres)2 AbstractReplication (com.torodb.packaging.config.model.protocol.mongo.AbstractReplication)2 MongoPasswordConfig (com.torodb.packaging.config.model.protocol.mongo.MongoPasswordConfig)2