@@ -12,9 +12,20 @@ import org.openjdk.jmh.annotations.Mode._
12
12
import org .openjdk .jmh .annotations ._
13
13
14
14
import scala .collection .JavaConverters ._
15
+ import scala .tools .benchmark .BenchmarkDriver
16
+
17
+ trait BaseBenchmarkDriver {
18
+ def source : String
19
+ def extraArgs : String
20
+ def corpusVersion : String
21
+ def depsClasspath : String
22
+ def tempDir : File
23
+ def corpusSourcePath : Path
24
+ def compilerArgs : Array [String ]
25
+ }
15
26
16
27
@ State (Scope .Benchmark )
17
- class ScalacBenchmark {
28
+ class ScalacBenchmark extends BenchmarkDriver {
18
29
@ Param (value = Array ())
19
30
var source : String = _
20
31
@@ -26,53 +37,20 @@ class ScalacBenchmark {
26
37
@ Param (value = Array (" latest" ))
27
38
var corpusVersion : String = _
28
39
29
- var driver : Driver = _
30
-
31
40
var depsClasspath : String = _
32
41
33
- def compileImpl (): Unit = {
34
- val compilerArgs =
35
- if (source.startsWith(" @" )) Array (source)
36
- else {
37
- import scala .collection .JavaConverters ._
38
- val allFiles = Files .walk(findSourceDir, FileVisitOption .FOLLOW_LINKS ).collect(Collectors .toList[Path ]).asScala.toList
39
- allFiles.filter(f => {
40
- val name = f.getFileName.toString
41
- name.endsWith(" .scala" ) || name.endsWith(" .java" )
42
- }).map(_.toAbsolutePath.normalize.toString).toArray
43
- }
44
-
45
- // MainClass is copy-pasted from compiler for source compatibility with 2.10.x - 2.13.x
46
- class MainClass extends Driver with EvalLoop {
47
- def resident (compiler : Global ): Unit = loop { line =>
48
- val command = new CompilerCommand (line split " \\ s+" toList, new Settings (scalacError))
49
- compiler.reporter.reset()
50
- new compiler.Run () compile command.files
51
- }
52
-
53
- override def newCompiler (): Global = Global (settings, reporter)
54
-
55
- override protected def processSettingsHook (): Boolean = {
56
- if (source == " scala" )
57
- settings.sourcepath.value = Paths .get(s " ../corpus/ $source/ $corpusVersion/library " ).toAbsolutePath.normalize.toString
58
- else
59
- settings.usejavacp.value = true
60
- settings.outdir.value = tempDir.getAbsolutePath
61
- settings.nowarn.value = true
62
- if (depsClasspath != null )
63
- settings.processArgumentString(s " -cp $depsClasspath" )
64
- if (extraArgs != null && extraArgs != " " )
65
- settings.processArgumentString(extraArgs)
66
- true
67
- }
42
+ def compilerArgs =
43
+ if (source.startsWith(" @" )) Array (source)
44
+ else {
45
+ import scala .collection .JavaConverters ._
46
+ val allFiles = Files .walk(findSourceDir).collect(Collectors .toList[Path ]).asScala.toList
47
+ allFiles.filter(f => {
48
+ val name = f.getFileName.toString
49
+ name.endsWith(" .scala" ) || name.endsWith(" .java" )
50
+ }).map(_.toAbsolutePath.normalize.toString).toArray
68
51
}
69
- val driver = new MainClass
70
-
71
- driver.process(compilerArgs)
72
- assert(! driver.reporter.hasErrors)
73
- }
74
52
75
- private var tempDir : File = null
53
+ var tempDir : File = null
76
54
77
55
// Executed once per fork
78
56
@ Setup (Level .Trial ) def initTemp (): Unit = {
@@ -95,7 +73,7 @@ class ScalacBenchmark {
95
73
})
96
74
}
97
75
98
- private def corpusSourcePath = Paths .get(s " ../corpus/ $source/ $corpusVersion" )
76
+ def corpusSourcePath : Path = Paths .get(s " ../corpus/ $source/ $corpusVersion" )
99
77
100
78
@ Setup (Level .Trial ) def initDepsClasspath (): Unit = {
101
79
val depsDir = Paths .get(ConfigFactory .load.getString(" deps.localdir" ))
@@ -158,6 +136,8 @@ class ColdScalacBenchmark extends ScalacBenchmark {
158
136
@ OutputTimeUnit (TimeUnit .MILLISECONDS )
159
137
@ Warmup (iterations = 0 )
160
138
@ Measurement (iterations = 1 , time = 30 , timeUnit = TimeUnit .SECONDS )
139
+ // @Fork triggers match error in dotty, see https://github.com/lampepfl/dotty/issues/2704
140
+ // Comment out `@Fork` to run compilation/test with Dotty or wait for that issue to be fixed.
161
141
@ Fork (value = 3 , jvmArgs = Array (" -Xms2G" , " -Xmx2G" ))
162
142
class WarmScalacBenchmark extends ScalacBenchmark {
163
143
@ Benchmark
0 commit comments