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);
}
}
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations