58 private final Optional<VerboseMode> verbose;
59 private final Optional<Boolean> failOnError;
60 private final List<ProfilerConfig> profilers = new ArrayList<ProfilerConfig>();
61 private final Optional<TimeUnit> timeUnit;
62 private final Optional<Integer> opsPerInvocation;
63 private final List<String> regexps = new ArrayList<String>();
64 private final Optional<Integer> fork;
65 private final Optional<Integer> warmupFork;
66 private final Optional<String> output;
67 private final Optional<String> result;
68 private final Optional<ResultFormatType> resultFormat;
69 private final Optional<String> jvm;
70 private final Optional<Collection<String>> jvmArgs;
71 private final Optional<Collection<String>> jvmArgsAppend;
72 private final Optional<Collection<String>> jvmArgsPrepend;
73 private final List<String> excludes = new ArrayList<String>();
74 private final Optional<WarmupMode> warmupMode;
75 private final List<String> warmupMicros = new ArrayList<String>();
76 private final Multimap<String, String> params = new HashMultimap<String, String>();
77 private final boolean list;
78 private final boolean listResultFormats;
79 private final boolean help;
80 private final boolean listProfilers;
81
82 private final transient OptionParser parser;
83
84 /**
85 * Parses the given command line.
86 * @param argv argument list
87 * @throws CommandLineOptionException if some options are misspelled
88 */
89 public CommandLineOptions(String... argv) throws CommandLineOptionException {
90 parser = new OptionParser();
91 parser.formatHelpWith(new OptionFormatter());
92
93 OptionSpec<Integer> optMeasureCount = parser.accepts("i", "Number of measurement iterations to do.")
94 .withRequiredArg().withValuesConvertedBy(IntegerValueConverter.POSITIVE).describedAs("int");
95
96 OptionSpec<Integer> optMeasureBatchSize = parser.accepts("bs", "Batch size: number of benchmark method calls per operation. " +
97 "(some benchmark modes can ignore this setting)")
180 OptionSpec<String> optResultFormat = parser.accepts("rf", "Result format type. See the list of available result formats first.")
181 .withRequiredArg().ofType(String.class).describedAs("type");
182
183 OptionSpec<String> optWarmupMode = parser.accepts("wm", "Warmup mode for warming up selected benchmarks. Warmup modes are: " + Arrays.toString(WarmupMode.values()) + ".")
184 .withRequiredArg().ofType(String.class).describedAs("mode");
185
186 OptionSpec<String> optExcludes = parser.accepts("e", "Benchmarks to exclude from the run.")
187 .withRequiredArg().withValuesSeparatedBy(',').ofType(String.class).describedAs("regexp+");
188
189 OptionSpec<String> optParams = parser.accepts("p", "Benchmark parameters. This option is expected to be used once" +
190 " per parameter. Parameter name and parameter values should be separated with equals sign." +
191 " Parameter values should be separated with commas.")
192 .withRequiredArg().ofType(String.class).describedAs("param={v,}*");
193
194 OptionSpec<String> optWarmupBenchmarks = parser.accepts("wmb", "Warmup benchmarks to include in the run " +
195 "in addition to already selected. JMH will not measure these benchmarks, but only use them" +
196 " for the warmup.")
197 .withRequiredArg().withValuesSeparatedBy(',').ofType(String.class).describedAs("regexp+");
198
199 parser.accepts("l", "List matching benchmarks and exit.");
200 parser.accepts("lrf", "List result formats.");
201 parser.accepts("lprof", "List profilers.");
202 parser.accepts("h", "Display help.");
203
204 try {
205 OptionSet set = parser.parse(argv);
206
207 if (set.has(optExcludes)) {
208 excludes.addAll(optExcludes.values(set));
209 }
210
211 if (set.has(optWarmupBenchmarks)) {
212 warmupMicros.addAll(optWarmupBenchmarks.values(set));
213 }
214
215 if (set.has(optTU)) {
216 String va = optTU.value(set);
217 TimeUnit tu;
218 if (va.equalsIgnoreCase("ns")) {
219 tu = TimeUnit.NANOSECONDS;
242 warmupMode = Optional.of(WarmupMode.valueOf(optWarmupMode.value(set)));
243 } catch (IllegalArgumentException iae) {
244 throw new CommandLineOptionException(iae.getMessage(), iae);
245 }
246 } else {
247 warmupMode = Optional.none();
248 }
249
250 if (set.has(optResultFormat)) {
251 try {
252 resultFormat = Optional.of(ResultFormatType.valueOf(optResultFormat.value(set).toUpperCase()));
253 } catch (IllegalArgumentException iae) {
254 throw new CommandLineOptionException(iae.getMessage(), iae);
255 }
256 } else {
257 resultFormat = Optional.none();
258 }
259
260 help = set.has("h");
261 list = set.has("l");
262 listResultFormats = set.has("lrf");
263 listProfilers = set.has("lprof");
264
265 iterations = toOptional(optMeasureCount, set);
266 batchSize = toOptional(optMeasureBatchSize, set);
267 runTime = toOptional(optMeasureTime, set);
268 warmupIterations = toOptional(optWarmupCount, set);
269 warmupBatchSize = toOptional(optWarmupBatchSize, set);
270 warmupTime = toOptional(optWarmupTime, set);
271 timeout = toOptional(optTimeoutTime, set);
272 threads = toOptional(optThreads, set);
273 synchIterations = toOptional(optSyncIters, set);
274 gcEachIteration = toOptional(optGC, set);
275 failOnError = toOptional(optFOE, set);
276 fork = toOptional(optForks, set);
277 warmupFork = toOptional(optWarmupForks, set);
278 output = toOptional(optOutput, set);
279 result = toOptional(optOutputResults, set);
280
281 if (set.has(optBenchmarkMode)) {
385 }
386
387 public void listProfilers() {
388 ProfilerFactory.listProfilers(System.out);
389 }
390
391 public void listResultFormats() {
392 StringBuilder sb = new StringBuilder();
393
394 for (ResultFormatType f : ResultFormatType.values()) {
395 sb.append(f.toString().toLowerCase());
396 sb.append(", ");
397 }
398 sb.setLength(sb.length() - 2);
399
400 System.out.println("Available formats: " + sb.toString());
401 }
402
403 public boolean shouldList() {
404 return list;
405 }
406
407 public boolean shouldListResultFormats() {
408 return listResultFormats;
409 }
410
411 public boolean shouldHelp() {
412 return help;
413 }
414
415 public boolean shouldListProfilers() {
416 return listProfilers;
417 }
418
419 @Override
420 public Optional<WarmupMode> getWarmupMode() {
421 return warmupMode;
422 }
423
424 @Override
|
58 private final Optional<VerboseMode> verbose;
59 private final Optional<Boolean> failOnError;
60 private final List<ProfilerConfig> profilers = new ArrayList<ProfilerConfig>();
61 private final Optional<TimeUnit> timeUnit;
62 private final Optional<Integer> opsPerInvocation;
63 private final List<String> regexps = new ArrayList<String>();
64 private final Optional<Integer> fork;
65 private final Optional<Integer> warmupFork;
66 private final Optional<String> output;
67 private final Optional<String> result;
68 private final Optional<ResultFormatType> resultFormat;
69 private final Optional<String> jvm;
70 private final Optional<Collection<String>> jvmArgs;
71 private final Optional<Collection<String>> jvmArgsAppend;
72 private final Optional<Collection<String>> jvmArgsPrepend;
73 private final List<String> excludes = new ArrayList<String>();
74 private final Optional<WarmupMode> warmupMode;
75 private final List<String> warmupMicros = new ArrayList<String>();
76 private final Multimap<String, String> params = new HashMultimap<String, String>();
77 private final boolean list;
78 private final boolean listWithParams;
79 private final boolean listResultFormats;
80 private final boolean help;
81 private final boolean listProfilers;
82
83 private final transient OptionParser parser;
84
85 /**
86 * Parses the given command line.
87 * @param argv argument list
88 * @throws CommandLineOptionException if some options are misspelled
89 */
90 public CommandLineOptions(String... argv) throws CommandLineOptionException {
91 parser = new OptionParser();
92 parser.formatHelpWith(new OptionFormatter());
93
94 OptionSpec<Integer> optMeasureCount = parser.accepts("i", "Number of measurement iterations to do.")
95 .withRequiredArg().withValuesConvertedBy(IntegerValueConverter.POSITIVE).describedAs("int");
96
97 OptionSpec<Integer> optMeasureBatchSize = parser.accepts("bs", "Batch size: number of benchmark method calls per operation. " +
98 "(some benchmark modes can ignore this setting)")
181 OptionSpec<String> optResultFormat = parser.accepts("rf", "Result format type. See the list of available result formats first.")
182 .withRequiredArg().ofType(String.class).describedAs("type");
183
184 OptionSpec<String> optWarmupMode = parser.accepts("wm", "Warmup mode for warming up selected benchmarks. Warmup modes are: " + Arrays.toString(WarmupMode.values()) + ".")
185 .withRequiredArg().ofType(String.class).describedAs("mode");
186
187 OptionSpec<String> optExcludes = parser.accepts("e", "Benchmarks to exclude from the run.")
188 .withRequiredArg().withValuesSeparatedBy(',').ofType(String.class).describedAs("regexp+");
189
190 OptionSpec<String> optParams = parser.accepts("p", "Benchmark parameters. This option is expected to be used once" +
191 " per parameter. Parameter name and parameter values should be separated with equals sign." +
192 " Parameter values should be separated with commas.")
193 .withRequiredArg().ofType(String.class).describedAs("param={v,}*");
194
195 OptionSpec<String> optWarmupBenchmarks = parser.accepts("wmb", "Warmup benchmarks to include in the run " +
196 "in addition to already selected. JMH will not measure these benchmarks, but only use them" +
197 " for the warmup.")
198 .withRequiredArg().withValuesSeparatedBy(',').ofType(String.class).describedAs("regexp+");
199
200 parser.accepts("l", "List matching benchmarks and exit.");
201 parser.accepts("lp", "List matching benchmarks with parameters and exit.");
202 parser.accepts("lrf", "List result formats.");
203 parser.accepts("lprof", "List profilers.");
204 parser.accepts("h", "Display help.");
205
206 try {
207 OptionSet set = parser.parse(argv);
208
209 if (set.has(optExcludes)) {
210 excludes.addAll(optExcludes.values(set));
211 }
212
213 if (set.has(optWarmupBenchmarks)) {
214 warmupMicros.addAll(optWarmupBenchmarks.values(set));
215 }
216
217 if (set.has(optTU)) {
218 String va = optTU.value(set);
219 TimeUnit tu;
220 if (va.equalsIgnoreCase("ns")) {
221 tu = TimeUnit.NANOSECONDS;
244 warmupMode = Optional.of(WarmupMode.valueOf(optWarmupMode.value(set)));
245 } catch (IllegalArgumentException iae) {
246 throw new CommandLineOptionException(iae.getMessage(), iae);
247 }
248 } else {
249 warmupMode = Optional.none();
250 }
251
252 if (set.has(optResultFormat)) {
253 try {
254 resultFormat = Optional.of(ResultFormatType.valueOf(optResultFormat.value(set).toUpperCase()));
255 } catch (IllegalArgumentException iae) {
256 throw new CommandLineOptionException(iae.getMessage(), iae);
257 }
258 } else {
259 resultFormat = Optional.none();
260 }
261
262 help = set.has("h");
263 list = set.has("l");
264 listWithParams = set.has("lp");
265 listResultFormats = set.has("lrf");
266 listProfilers = set.has("lprof");
267
268 iterations = toOptional(optMeasureCount, set);
269 batchSize = toOptional(optMeasureBatchSize, set);
270 runTime = toOptional(optMeasureTime, set);
271 warmupIterations = toOptional(optWarmupCount, set);
272 warmupBatchSize = toOptional(optWarmupBatchSize, set);
273 warmupTime = toOptional(optWarmupTime, set);
274 timeout = toOptional(optTimeoutTime, set);
275 threads = toOptional(optThreads, set);
276 synchIterations = toOptional(optSyncIters, set);
277 gcEachIteration = toOptional(optGC, set);
278 failOnError = toOptional(optFOE, set);
279 fork = toOptional(optForks, set);
280 warmupFork = toOptional(optWarmupForks, set);
281 output = toOptional(optOutput, set);
282 result = toOptional(optOutputResults, set);
283
284 if (set.has(optBenchmarkMode)) {
388 }
389
390 public void listProfilers() {
391 ProfilerFactory.listProfilers(System.out);
392 }
393
394 public void listResultFormats() {
395 StringBuilder sb = new StringBuilder();
396
397 for (ResultFormatType f : ResultFormatType.values()) {
398 sb.append(f.toString().toLowerCase());
399 sb.append(", ");
400 }
401 sb.setLength(sb.length() - 2);
402
403 System.out.println("Available formats: " + sb.toString());
404 }
405
406 public boolean shouldList() {
407 return list;
408 }
409
410 public boolean shouldListWithParams() {
411 return listWithParams;
412 }
413
414 public boolean shouldListResultFormats() {
415 return listResultFormats;
416 }
417
418 public boolean shouldHelp() {
419 return help;
420 }
421
422 public boolean shouldListProfilers() {
423 return listProfilers;
424 }
425
426 @Override
427 public Optional<WarmupMode> getWarmupMode() {
428 return warmupMode;
429 }
430
431 @Override
|