Search in sources :

Example 31 with ArgumentParser

use of net.sourceforge.argparse4j.inf.ArgumentParser in project kafka by apache.

the class ProducerPerformance method main.

public static void main(String[] args) throws Exception {
    ArgumentParser parser = argParser();
    try {
        Namespace res = parser.parseArgs(args);
        /* parse args */
        String topicName = res.getString("topic");
        long numRecords = res.getLong("numRecords");
        Integer recordSize = res.getInt("recordSize");
        int throughput = res.getInt("throughput");
        List<String> producerProps = res.getList("producerConfig");
        String producerConfig = res.getString("producerConfigFile");
        String payloadFilePath = res.getString("payloadFile");
        // since default value gets printed with the help text, we are escaping \n there and replacing it with correct value here.
        String payloadDelimiter = res.getString("payloadDelimiter").equals("\\n") ? "\n" : res.getString("payloadDelimiter");
        if (producerProps == null && producerConfig == null) {
            throw new ArgumentParserException("Either --producer-props or --producer.config must be specified.", parser);
        }
        List<byte[]> payloadByteList = new ArrayList<>();
        if (payloadFilePath != null) {
            Path path = Paths.get(payloadFilePath);
            System.out.println("Reading payloads from: " + path.toAbsolutePath());
            if (Files.notExists(path) || Files.size(path) == 0) {
                throw new IllegalArgumentException("File does not exist or empty file provided.");
            }
            String[] payloadList = new String(Files.readAllBytes(path), "UTF-8").split(payloadDelimiter);
            System.out.println("Number of messages read: " + payloadList.length);
            for (String payload : payloadList) {
                payloadByteList.add(payload.getBytes(StandardCharsets.UTF_8));
            }
        }
        Properties props = new Properties();
        if (producerConfig != null) {
            props.putAll(Utils.loadProps(producerConfig));
        }
        if (producerProps != null)
            for (String prop : producerProps) {
                String[] pieces = prop.split("=");
                if (pieces.length != 2)
                    throw new IllegalArgumentException("Invalid property: " + prop);
                props.put(pieces[0], pieces[1]);
            }
        props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.ByteArraySerializer");
        props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.ByteArraySerializer");
        KafkaProducer<byte[], byte[]> producer = new KafkaProducer<byte[], byte[]>(props);
        /* setup perf test */
        byte[] payload = null;
        Random random = new Random(0);
        if (recordSize != null) {
            payload = new byte[recordSize];
            for (int i = 0; i < payload.length; ++i) payload[i] = (byte) (random.nextInt(26) + 65);
        }
        ProducerRecord<byte[], byte[]> record;
        Stats stats = new Stats(numRecords, 5000);
        long startMs = System.currentTimeMillis();
        ThroughputThrottler throttler = new ThroughputThrottler(throughput, startMs);
        for (int i = 0; i < numRecords; i++) {
            if (payloadFilePath != null) {
                payload = payloadByteList.get(random.nextInt(payloadByteList.size()));
            }
            record = new ProducerRecord<>(topicName, payload);
            long sendStartMs = System.currentTimeMillis();
            Callback cb = stats.nextCompletion(sendStartMs, payload.length, stats);
            producer.send(record, cb);
            if (throttler.shouldThrottle(i, sendStartMs)) {
                throttler.throttle();
            }
        }
        /* print final results */
        producer.close();
        stats.printTotal();
    } catch (ArgumentParserException e) {
        if (args.length == 0) {
            parser.printHelp();
            Exit.exit(0);
        } else {
            parser.handleError(e);
            Exit.exit(1);
        }
    }
}
Also used : Path(java.nio.file.Path) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) ArrayList(java.util.ArrayList) Properties(java.util.Properties) ArgumentParser(net.sourceforge.argparse4j.inf.ArgumentParser) Namespace(net.sourceforge.argparse4j.inf.Namespace) Callback(org.apache.kafka.clients.producer.Callback) Random(java.util.Random) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException)

Example 32 with ArgumentParser

use of net.sourceforge.argparse4j.inf.ArgumentParser in project kafka by apache.

the class VerifiableProducer method createFromArgs.

/** Construct a VerifiableProducer object from command-line arguments. */
public static VerifiableProducer createFromArgs(String[] args) {
    ArgumentParser parser = argParser();
    VerifiableProducer producer = null;
    try {
        Namespace res;
        res = parser.parseArgs(args);
        int maxMessages = res.getInt("maxMessages");
        String topic = res.getString("topic");
        int throughput = res.getInt("throughput");
        String configFile = res.getString("producer.config");
        Integer valuePrefix = res.getInt("valuePrefix");
        Properties producerProps = new Properties();
        producerProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, res.getString("brokerList"));
        producerProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        producerProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        producerProps.put(ProducerConfig.ACKS_CONFIG, Integer.toString(res.getInt("acks")));
        // No producer retries
        producerProps.put("retries", "0");
        if (configFile != null) {
            try {
                producerProps.putAll(loadProps(configFile));
            } catch (IOException e) {
                throw new ArgumentParserException(e.getMessage(), parser);
            }
        }
        producer = new VerifiableProducer(producerProps, topic, throughput, maxMessages, valuePrefix);
    } catch (ArgumentParserException e) {
        if (args.length == 0) {
            parser.printHelp();
            Exit.exit(0);
        } else {
            parser.handleError(e);
            Exit.exit(1);
        }
    }
    return producer;
}
Also used : IOException(java.io.IOException) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) Properties(java.util.Properties) ArgumentParser(net.sourceforge.argparse4j.inf.ArgumentParser) Namespace(net.sourceforge.argparse4j.inf.Namespace)

Example 33 with ArgumentParser

use of net.sourceforge.argparse4j.inf.ArgumentParser in project pcgen by PCGen.

the class Main method getParser.

/**
	 * @return an ArgumentParser used to peform argument parsing
	 */
private static ArgumentParser getParser() {
    ArgumentParser parser = ArgumentParsers.newArgumentParser(Constants.APPLICATION_NAME).defaultHelp(false).description("RPG Character Generator").version(PCGenPropBundle.getVersionNumber());
    parser.addArgument("-v", "--verbose").help("verbose logging").type(Boolean.class).action(Arguments.count());
    parser.addArgument("-V", "--version").action(Arguments.version());
    parser.addArgument("-J").help("ignore java version checks").action(Arguments.storeTrue());
    MutuallyExclusiveGroup startupMode = parser.addMutuallyExclusiveGroup().description("start up on a specific mode");
    startupMode.addArgument("-G", "--gmgen").help("GMGen mode").type(Boolean.class).action(Arguments.storeTrue());
    startupMode.addArgument("-N", "--npc").help("NPC generation mode").type(Boolean.class).action(Arguments.storeTrue());
    startupMode.addArgument("--name-generator").help("run the name generator").type(Boolean.class).action(Arguments.storeTrue());
    startupMode.addArgument("-D", "--tab").nargs(1);
    parser.addArgument("-s", "--settingsdir").nargs(1).type(Arguments.fileType().verifyIsDirectory().verifyCanRead().verifyExists());
    parser.addArgument("-m", "--campaignmode").nargs(1).type(String.class);
    parser.addArgument("-E", "--exportsheet").nargs(1).type(Arguments.fileType().verifyCanRead().verifyExists().verifyIsFile());
    parser.addArgument("-o", "--outputfile").nargs(1).type(Arguments.fileType().verifyCanCreate().verifyCanWrite().verifyNotExists());
    parser.addArgument("-c", "--character").nargs(1).type(Arguments.fileType().verifyCanRead().verifyExists().verifyIsFile());
    parser.addArgument("-p", "--party").nargs(1).type(Arguments.fileType().verifyCanRead().verifyExists().verifyIsFile());
    return parser;
}
Also used : MutuallyExclusiveGroup(net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup) ArgumentParser(net.sourceforge.argparse4j.inf.ArgumentParser)

Example 34 with ArgumentParser

use of net.sourceforge.argparse4j.inf.ArgumentParser in project bgpcep by opendaylight.

the class Arguments method initializeArgumentParser.

private static ArgumentParser initializeArgumentParser() {
    final ArgumentParser parser = ArgumentParsers.newArgumentParser(PROGRAM_NAME);
    parser.addArgument("-i", toArgName(ACTIVE_CONNECTION_PARAMETER)).type(Boolean.class).setDefault(false).help(ACTIVE_CONNECTION_HELP);
    parser.addArgument("-ho", toArgName(HOLD_TIMER_PARAMETER)).type(Integer.class).setDefault(INITIAL_HOLD_TIME).help(INITIAL_HOLD_TIME_HELP);
    parser.addArgument("-pr", toArgName(PREFIXES_PARAMETER)).type(Integer.class).setDefault(0).help(PREFIXES_PARAMETER_HELP);
    parser.addArgument("-sc", toArgName(SPEAKERS_COUNT)).type(Integer.class).setDefault(0).help(SPEAKERS_COUNT_HELP);
    parser.addArgument("-mp", toArgName(MULTIPATH_PARAMETER)).type(Boolean.class).setDefault(false).help(MULTIPATH_PARAMETER_HELP);
    parser.addArgument("-" + AS_PARAMETER, toArgName(AS_PARAMETER)).type((ArgumentTypeTool<AsNumber>) as -> new AsNumber(Long.valueOf(as))).setDefault(new AsNumber(64496L)).help(AS_PARAMETER_HELP);
    parser.addArgument("-ec", toArgName(EXTENDED_COMMUNITIES_PARAMETER)).type((ArgumentTypeTool<List<String>>) extComInput -> Arrays.asList(extComInput.split(","))).setDefault(Collections.emptyList()).help(EXTENDED_COMMUNITIES_PARAMETER_HELP);
    parser.addArgument("-ll", toArgName(LOG_LEVEL)).type((ArgumentTypeTool<Level>) Level::toLevel).setDefault(Level.INFO).help("log levels");
    parser.addArgument("-ra", toArgName(REMOTE_ADDRESS_PARAMETER)).type((ArgumentTypeTool<List<InetSocketAddress>>) input -> InetSocketAddressUtil.parseAddresses(input, DEFAULT_REMOTE_PORT)).setDefault(Collections.singletonList(REMOTE_ADDRESS)).help(REMOTE_ADDRESS_PARAMETER_HELP);
    parser.addArgument("-la", toArgName(LOCAL_ADDRESS_PARAMETER)).type((ArgumentTypeTool<InetSocketAddress>) input -> getInetSocketAddress(input, DEFAULT_LOCAL_PORT)).setDefault(LOCAL_ADDRESS).help(LOCAL_ADDRESS_PARAMETER_HELP);
    return parser;
}
Also used : InetSocketAddressUtil(org.opendaylight.protocol.util.InetSocketAddressUtil) Arrays(java.util.Arrays) ArgumentsInput(org.opendaylight.protocol.util.ArgumentsInput) ArgumentParsers(net.sourceforge.argparse4j.ArgumentParsers) InetSocketAddress(java.net.InetSocketAddress) InetAddress(java.net.InetAddress) Level(ch.qos.logback.classic.Level) List(java.util.List) ArgumentParser(net.sourceforge.argparse4j.inf.ArgumentParser) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) ArgumentType(net.sourceforge.argparse4j.inf.ArgumentType) Namespace(net.sourceforge.argparse4j.inf.Namespace) AsNumber(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber) InetSocketAddressUtil.getInetSocketAddress(org.opendaylight.protocol.util.InetSocketAddressUtil.getInetSocketAddress) InetAddresses(com.google.common.net.InetAddresses) Argument(net.sourceforge.argparse4j.inf.Argument) Collections(java.util.Collections) InetSocketAddress(java.net.InetSocketAddress) InetSocketAddressUtil.getInetSocketAddress(org.opendaylight.protocol.util.InetSocketAddressUtil.getInetSocketAddress) List(java.util.List) Level(ch.qos.logback.classic.Level) ArgumentParser(net.sourceforge.argparse4j.inf.ArgumentParser) AsNumber(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.AsNumber)

Example 35 with ArgumentParser

use of net.sourceforge.argparse4j.inf.ArgumentParser in project java-docs-samples by GoogleCloudPlatform.

the class SynthesizeFile method main.

// [END tts_synthesize_ssml_file]
public static void main(String... args) throws Exception {
    ArgumentParser parser = ArgumentParsers.newFor("SynthesizeFile").build().defaultHelp(true).description("Synthesize a text file or ssml file.");
    MutuallyExclusiveGroup group = parser.addMutuallyExclusiveGroup().required(true);
    group.addArgument("--text").help("The text file from which to synthesize speech.");
    group.addArgument("--ssml").help("The ssml file from which to synthesize speech.");
    try {
        Namespace namespace = parser.parseArgs(args);
        if (namespace.get("text") != null) {
            synthesizeTextFile(namespace.getString("text"));
        } else {
            synthesizeSsmlFile(namespace.getString("ssml"));
        }
    } catch (ArgumentParserException e) {
        parser.handleError(e);
    }
}
Also used : MutuallyExclusiveGroup(net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup) ArgumentParserException(net.sourceforge.argparse4j.inf.ArgumentParserException) ArgumentParser(net.sourceforge.argparse4j.inf.ArgumentParser) Namespace(net.sourceforge.argparse4j.inf.Namespace)

Aggregations

ArgumentParser (net.sourceforge.argparse4j.inf.ArgumentParser)63 Namespace (net.sourceforge.argparse4j.inf.Namespace)35 ArgumentParserException (net.sourceforge.argparse4j.inf.ArgumentParserException)32 MutuallyExclusiveGroup (net.sourceforge.argparse4j.inf.MutuallyExclusiveGroup)13 Subparser (net.sourceforge.argparse4j.inf.Subparser)11 Properties (java.util.Properties)8 Before (org.junit.Before)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 IOException (java.io.IOException)5 ArgumentGroup (net.sourceforge.argparse4j.inf.ArgumentGroup)5 Path (java.nio.file.Path)4 Collections (java.util.Collections)4 ArgumentParsers (net.sourceforge.argparse4j.ArgumentParsers)4 Subparsers (net.sourceforge.argparse4j.inf.Subparsers)4 Platform (org.apache.kafka.trogdor.common.Platform)4 JsonRestServer (org.apache.kafka.trogdor.rest.JsonRestServer)4 File (java.io.File)3 Arrays (java.util.Arrays)3 Level (ch.qos.logback.classic.Level)2