@@ -7,7 +7,7 @@ import reporting._
7
7
8
8
import org .openjdk .jmh .results .RunResult
9
9
import org .openjdk .jmh .runner .Runner
10
- import org .openjdk .jmh .runner .options .OptionsBuilder
10
+ import org .openjdk .jmh .runner .options .{ OptionsBuilder , CommandLineOptions }
11
11
import org .openjdk .jmh .annotations ._
12
12
import org .openjdk .jmh .results .format ._
13
13
import java .util .concurrent .TimeUnit
@@ -22,37 +22,47 @@ import dotty.tools.io.AbstractFile
22
22
object Bench {
23
23
val COMPILE_OPTS_FILE = " compile.txt"
24
24
25
+ def printUsage () =
26
+ println(" Usage (from SBT): scala3-bench/jmh:run <JMH arguments> -- <scalac arguments>" )
27
+ println(" Display JMH help: scala3-bench/jmh:run -h" )
28
+ println(" Our default JMH options: -wi 30 -i 20 -f 3 -tu ms -bm AverageTime -jvmArgs \" -Xms2G -Xmx2G\" " )
29
+
25
30
def main (args : Array [String ]): Unit = {
26
31
if (args.isEmpty) {
27
- println(" Missing <args>" )
32
+ println(" Missing arguments." )
33
+ printUsage()
28
34
return
29
35
}
30
- val (intArgs, args1) = args.span(x => try { x.toInt; true } catch { case _ : Throwable => false } )
31
36
32
- val warmup = if (intArgs.length > 0 ) intArgs(0 ).toInt else 30
33
- val iterations = if (intArgs.length > 1 ) intArgs(1 ).toInt else 20
34
- val forks = if (intArgs.length > 2 ) intArgs(2 ).toInt else 1
37
+ val (jmhArgs, _scalacArgs) = args.span(_ != " --" )
38
+ val scalacArgs = _scalacArgs.drop(1 )
35
39
40
+ storeCompileOptions(scalacArgs)
36
41
37
- import File .{ separator => sep }
42
+ val jmhCliOps = new CommandLineOptions (jmhArgs:_* )
43
+ val jmhOps = new OptionsBuilder ().parent(jmhCliOps)
44
+
45
+ // set our own default options
46
+ if ! jmhCliOps.shouldFailOnError().hasValue() then jmhOps.shouldFailOnError(true )
47
+ if ! jmhCliOps.getWarmupIterations().hasValue() then jmhOps.warmupIterations(30 )
48
+ if ! jmhCliOps.getMeasurementIterations().hasValue() then jmhOps.measurementIterations(20 )
49
+ if ! jmhCliOps.getForkCount().hasValue() then jmhOps.forks(1 )
50
+ if jmhCliOps.getBenchModes().isEmpty() then jmhOps.mode(Mode .AverageTime )
51
+ if ! jmhCliOps.getTimeUnit().hasValue() then jmhOps.timeUnit(TimeUnit .MILLISECONDS )
52
+ if ! jmhCliOps.getJvmArgs().hasValue() then jmhOps.jvmArgs(" -Xms2G" , " -Xmx2G" )
53
+
54
+ val runner = new Runner (jmhOps.build())
55
+
56
+ if jmhCliOps.shouldHelp() then
57
+ printUsage()
58
+ println(" Following is the JMH options documentation." )
59
+ println(" -------------------------------------------" )
60
+ return jmhCliOps.showHelp()
61
+ if jmhCliOps.shouldList() then return runner.list()
62
+ if jmhCliOps.shouldListWithParams() then return runner.listWithParams(jmhCliOps)
63
+ if jmhCliOps.shouldListProfilers() then return jmhCliOps.listProfilers()
64
+ if jmhCliOps.shouldListResultFormats() then return jmhCliOps.listResultFormats()
38
65
39
- val args2 = args1.map { arg =>
40
- if ((arg.endsWith(" .scala" ) || arg.endsWith(" .java" )) && ! (new File (arg)).isAbsolute) " .." + sep + arg
41
- else arg
42
- }
43
- storeCompileOptions(args2)
44
-
45
- val opts = new OptionsBuilder ()
46
- .shouldFailOnError(true )
47
- .jvmArgs(" -Xms2G" , " -Xmx2G" )
48
- .mode(Mode .AverageTime )
49
- .timeUnit(TimeUnit .MILLISECONDS )
50
- .warmupIterations(warmup)
51
- .measurementIterations(iterations)
52
- .forks(forks)
53
- .build
54
-
55
- val runner = new Runner (opts) // full access to all JMH features, you can also provide a custom output Format here
56
66
runner.run() // actually run the benchmarks
57
67
58
68
removeCompileOptions
@@ -61,13 +71,17 @@ object Bench {
61
71
def removeCompileOptions : Unit = new File (COMPILE_OPTS_FILE ).delete()
62
72
63
73
def storeCompileOptions (args : Array [String ]): Unit = {
74
+ import File .{ separator => sep }
75
+
64
76
val standard_libs = System .getProperty(" BENCH_CLASS_PATH" )
65
77
val compiler_libs = System .getProperty(" BENCH_COMPILER_CLASS_PATH" )
66
78
67
79
val libs = if (args.contains(" -with-compiler" )) compiler_libs else standard_libs
68
- var argsNorm = args.filter(_ != " -with-compiler" )
80
+ var argsNorm = args.filter(_ != " -with-compiler" ).map { arg =>
81
+ if ((arg.endsWith(" .scala" ) || arg.endsWith(" .java" )) && ! (new File (arg)).isAbsolute) " .." + sep + arg
82
+ else arg
83
+ }
69
84
70
- import File .{ pathSeparator => sep }
71
85
var cpIndex = argsNorm.indexOf(" -classpath" )
72
86
if (cpIndex == - 1 ) cpIndex = argsNorm.indexOf(" -cp" )
73
87
if (cpIndex != - 1 ) argsNorm(cpIndex + 1 ) = argsNorm(cpIndex + 1 ) + sep + libs
0 commit comments