11package org .javacs ;
22
3+ import java .io .IOException ;
4+ import java .nio .file .Files ;
35import java .nio .file .Paths ;
46import java .time .Instant ;
57import java .util .Collections ;
911import java .util .logging .Logger ;
1012import org .javacs .completion .PruneMethodBodies ;
1113import org .openjdk .jmh .annotations .*;
14+ import org .openjdk .jmh .infra .Blackhole ;
1215
1316@ Warmup (iterations = 5 , time = 1 , timeUnit = TimeUnit .SECONDS )
1417@ Measurement (iterations = 5 , time = 1 , timeUnit = TimeUnit .SECONDS )
@@ -17,19 +20,35 @@ public class BenchmarkPruner {
1720
1821 @ State (Scope .Benchmark )
1922 public static class CompilerState {
20- public SourceFileObject file = file (false );
21- public SourceFileObject pruned = file (true );
22- public JavaCompilerService compiler = createCompiler ();
23+ public java .nio .file .Path file ;
24+ public String plainContents ;
25+ public String prunedContents ;
26+ public JavaCompilerService compiler ;
27+ private long version ;
2328
24- private SourceFileObject file (boolean prune ) {
25- var file = Paths .get ("src/main/java/org/javacs/InferConfig.java" ).normalize ();
26- if (prune ) {
27- var task = compiler .parse (file );
28- var contents = new PruneMethodBodies (task .task ).scan (task .root , 11222L ).toString ();
29- return new SourceFileObject (file , contents , Instant .now ());
30- } else {
31- return new SourceFileObject (file );
32- }
29+ @ Setup (org .openjdk .jmh .annotations .Level .Trial )
30+ public void setup () throws IOException {
31+ FileStore .reset ();
32+ quietBenchmarkLogging ();
33+
34+ file = Paths .get ("src/main/java/org/javacs/InferConfig.java" ).normalize ();
35+ plainContents = Files .readString (file );
36+ compiler = createCompiler ();
37+
38+ var task = compiler .parse (source (plainContents ));
39+ prunedContents = new PruneMethodBodies (task .task ).scan (task .root , 11222L ).toString ();
40+ }
41+
42+ public SourceFileObject plainSource () {
43+ return source (plainContents );
44+ }
45+
46+ public SourceFileObject prunedSource () {
47+ return source (prunedContents );
48+ }
49+
50+ private SourceFileObject source (String contents ) {
51+ return new SourceFileObject (file , contents , Instant .ofEpochMilli (++version ));
3352 }
3453
3554 private static JavaCompilerService createCompiler () {
@@ -40,21 +59,32 @@ private static JavaCompilerService createCompiler() {
4059 var classPath = new InferConfig (workspaceRoot ).classPath ();
4160 return new JavaCompilerService (classPath , Collections .emptySet (), Collections .emptySet ());
4261 }
62+
63+ private static void quietBenchmarkLogging () {
64+ Main .setRootFormat ();
65+ Logger .getLogger ("" ).setLevel (java .util .logging .Level .WARNING );
66+ Logger .getLogger ("main" ).setLevel (java .util .logging .Level .WARNING );
67+ }
4368 }
4469
4570 @ Benchmark
46- public void parsePlain (CompilerState state ) {
47- Parser .parseJavaFileObject (state .file );
71+ public void parsePlain (CompilerState state , Blackhole blackhole ) {
72+ var parse = Parser .parseJavaFileObject (state .plainSource ());
73+ blackhole .consume (parse .root );
4874 }
4975
5076 @ Benchmark
51- public void compilePruned (CompilerState state ) {
52- state .compiler .compile (List .of (state .pruned )).close ();
77+ public void compilePruned (CompilerState state , Blackhole blackhole ) {
78+ try (var compile = state .compiler .compile (List .of (state .prunedSource ()))) {
79+ blackhole .consume (compile .root ());
80+ }
5381 }
5482
5583 @ Benchmark
56- public void compilePlain (CompilerState state ) {
57- state .compiler .compile (List .of (state .file )).close ();
84+ public void compilePlain (CompilerState state , Blackhole blackhole ) {
85+ try (var compile = state .compiler .compile (List .of (state .plainSource ()))) {
86+ blackhole .consume (compile .root ());
87+ }
5888 }
5989
6090 private static final Logger LOG = Logger .getLogger ("main" );
0 commit comments