|
1 | 1 | package scala.tools.nsc;
|
2 | 2 |
|
| 3 | +import org.openjdk.jmh.annotations.*; |
| 4 | + |
| 5 | +import javax.tools.JavaCompiler; |
| 6 | +import javax.tools.ToolProvider; |
| 7 | +import java.io.File; |
3 | 8 | import java.io.IOException;
|
4 | 9 | import java.nio.charset.Charset;
|
| 10 | +import java.nio.file.FileVisitOption; |
5 | 11 | import java.nio.file.Files;
|
6 | 12 | import java.nio.file.Path;
|
7 |
| -import javax.tools.JavaCompiler; |
8 |
| -import javax.tools.ToolProvider; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Collections; |
| 16 | +import java.util.List; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | +import java.util.stream.Collectors; |
| 19 | +import java.util.stream.Stream; |
9 | 20 |
|
10 |
| -import org.openjdk.jmh.annotations.Benchmark; |
11 |
| -import org.openjdk.jmh.annotations.Scope; |
12 |
| -import org.openjdk.jmh.annotations.Setup; |
13 |
| -import org.openjdk.jmh.annotations.State; |
| 21 | +import static org.openjdk.jmh.annotations.Mode.SampleTime; |
14 | 22 |
|
| 23 | +@BenchmarkMode(SampleTime) |
| 24 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 25 | +@Warmup(iterations = 10, time = 10, timeUnit = TimeUnit.SECONDS) |
| 26 | +@Measurement(iterations = 10, time = 10, timeUnit = TimeUnit.SECONDS) |
| 27 | +@Fork(value = 3, jvmArgs = {"-Xms2G", "-Xmx2G"}) |
15 | 28 | @State(Scope.Thread)
|
16 | 29 | public class JavacBenchmark {
|
17 |
| - // Prepare source somehow. |
18 |
| - String source = "public class Test { public Test() { System.out.println(\"world\"); } }"; |
| 30 | + @Param(value = {}) |
| 31 | + String source; |
| 32 | + |
| 33 | + // This parameter is set by ScalacBenchmarkRunner / UploadingRunner based on the Scala version. |
| 34 | + // When running the benchmark directly the "latest" symlink is used. |
| 35 | + @Param(value = {"latest"}) |
| 36 | + String corpusVersion; |
| 37 | + |
19 | 38 | private JavaCompiler compiler;
|
20 | 39 | private Path path;
|
21 | 40 |
|
| 41 | + private Path corpusSourcePath() { |
| 42 | + return Paths.get("..", "corpus", source, corpusVersion); }; |
| 43 | + |
| 44 | + private Path findSourceDir() { |
| 45 | + Path path = corpusSourcePath(); |
| 46 | + return Files.exists(path) ? path : Paths.get(source); |
| 47 | + } |
| 48 | + |
| 49 | + private List<String> sourceFiles() { |
| 50 | + if (source.startsWith("@")) return Collections.emptyList(); |
| 51 | + else { |
| 52 | + List<Path> allFiles = walk(findSourceDir()).collect(Collectors.<Path>toList()); |
| 53 | + return allFiles.stream().filter(f -> { |
| 54 | + String name = f.getFileName().toString(); |
| 55 | + return name.endsWith(".scala") || name.endsWith(".java"); |
| 56 | + }).map(x -> x.toAbsolutePath().normalize().toString()).collect(Collectors.toList()); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private Stream<Path> walk(Path dir) { |
| 61 | + try { |
| 62 | + return Files.walk(dir, FileVisitOption.FOLLOW_LINKS); |
| 63 | + } catch (IOException e) { |
| 64 | + throw new RuntimeException(e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
22 | 68 | @Setup
|
23 | 69 | public void setup() throws IOException {
|
24 |
| - Path temp = Files.createTempDirectory("JavacBenchmark"); |
25 |
| - path = temp.resolve("Test.java"); |
26 | 70 | compiler = ToolProvider.getSystemJavaCompiler();
|
27 |
| - Files.write(path, source.getBytes(Charset.forName("UTF-8"))); |
28 | 71 | }
|
29 | 72 |
|
30 | 73 | @Benchmark
|
31 | 74 | public int bench() {
|
32 | 75 | // Compile source file.
|
33 |
| - int run = compiler.run(null, null, null, path.toAbsolutePath().toString()); |
| 76 | + List<String> args = new ArrayList(); |
| 77 | + args.add("-d"); |
| 78 | + args.add(tempDir.getAbsolutePath()); |
| 79 | + args.addAll(sourceFiles()); |
| 80 | + int run = compiler.run(null, null, null, args.toArray(new String[]{})); |
34 | 81 | assert run == 0;
|
35 | 82 | return run;
|
36 | 83 | }
|
| 84 | + |
| 85 | + |
| 86 | + private File tempDir = null; |
| 87 | + |
| 88 | + // Executed once per fork |
| 89 | + @Setup(Level.Trial) public void initTemp() { |
| 90 | + File tempFile = null; |
| 91 | + try { |
| 92 | + tempFile = File.createTempFile("output", ""); |
| 93 | + } catch (IOException e) { |
| 94 | + throw new RuntimeException(e); |
| 95 | + } |
| 96 | + tempFile.delete(); |
| 97 | + tempFile.mkdir(); |
| 98 | + tempDir = tempFile; |
| 99 | + } |
| 100 | + @TearDown(Level.Trial) public void clearTemp() { |
| 101 | + BenchmarkUtils.deleteRecursive(tempDir.toPath()); |
| 102 | + } |
| 103 | + |
37 | 104 | }
|
0 commit comments