Search in sources :

Example 6 with ParameterException

use of com.beust.jcommander.ParameterException 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 7 with ParameterException

use of com.beust.jcommander.ParameterException 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 8 with ParameterException

use of com.beust.jcommander.ParameterException 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)

Example 9 with ParameterException

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

the class PerformanceClient method loadArguments.

public Arguments loadArguments(String[] args) {
    Arguments arguments = new Arguments();
    jc = new JCommander(arguments);
    jc.setProgramName("pulsar-websocket-perf-producer");
    try {
        jc.parse(args);
    } catch (ParameterException e) {
        log.error(e.getMessage());
        jc.usage();
        System.exit(-1);
    }
    if (arguments.help) {
        jc.usage();
        System.exit(-1);
    }
    if (arguments.confFile != null) {
        Properties prop = new Properties(System.getProperties());
        try {
            prop.load(new FileInputStream(arguments.confFile));
        } catch (IOException e) {
            log.error("Error in loading config file");
            jc.usage();
            System.exit(1);
        }
        if (arguments.proxyURL == null) {
            arguments.proxyURL = 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);
    return arguments;
}
Also used : JCommander(com.beust.jcommander.JCommander) ParameterException(com.beust.jcommander.ParameterException) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Example 10 with ParameterException

use of com.beust.jcommander.ParameterException in project drill by apache.

the class DumpCat method main.

public static void main(String[] args) throws Exception {
    final DumpCat dumpCat = new DumpCat();
    final Options o = new Options();
    JCommander jc = null;
    try {
        jc = new JCommander(o, args);
        jc.setProgramName("./drill_dumpcat");
    } catch (ParameterException e) {
        System.out.println(e.getMessage());
        final String[] valid = { "-f", "file" };
        new JCommander(o, valid).usage();
        System.exit(-1);
    }
    if (o.help) {
        jc.usage();
        System.exit(0);
    }
    /*Check if dump file exists*/
    final File file = new File(o.location);
    if (!file.exists()) {
        System.out.println(String.format("Trace file %s not created", o.location));
        System.exit(-1);
    }
    try (final FileInputStream input = new FileInputStream(file.getAbsoluteFile())) {
        if (o.batch < 0) {
            dumpCat.doQuery(input);
        } else {
            dumpCat.doBatch(input, o.batch, o.include_headers);
        }
    }
}
Also used : JCommander(com.beust.jcommander.JCommander) ParameterException(com.beust.jcommander.ParameterException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

ParameterException (com.beust.jcommander.ParameterException)19 JCommander (com.beust.jcommander.JCommander)15 RateLimiter (com.google.common.util.concurrent.RateLimiter)4 PulsarClient (com.yahoo.pulsar.client.api.PulsarClient)4 FileInputStream (java.io.FileInputStream)4 Message (com.yahoo.pulsar.client.api.Message)3 File (java.io.File)3 IOException (java.io.IOException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 ClientConfiguration (com.yahoo.pulsar.client.api.ClientConfiguration)2 Consumer (com.yahoo.pulsar.client.api.Consumer)2 ConsumerConfiguration (com.yahoo.pulsar.client.api.ConsumerConfiguration)2 Producer (com.yahoo.pulsar.client.api.Producer)2 PulsarClientException (com.yahoo.pulsar.client.api.PulsarClientException)2 PulsarClientImpl (com.yahoo.pulsar.client.impl.PulsarClientImpl)2 DestinationName (com.yahoo.pulsar.common.naming.DestinationName)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2