Skip to content

Commit

Permalink
Add a microbenchmark for type inference / dataflow (#617)
Browse files Browse the repository at this point in the history
This will help us to identify performance issues in our dataflow analysis that could improve NullAway's performance on large methods. The benchmark was generated with a script.
  • Loading branch information
msridhar authored Jul 1, 2022
1 parent d9bc2dc commit da92a36
Show file tree
Hide file tree
Showing 6 changed files with 684 additions and 0 deletions.
1 change: 1 addition & 0 deletions jmh/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ jmh {
// for more examples see https://github.com/melix/jmh-gradle-plugin/blob/master/README.adoc#configuration-options
// iterations = 5
// fork = 5
// includes = ['DFlowMicro']
}

// don't run test task on pre-JDK-11 VMs
Expand Down
24 changes: 24 additions & 0 deletions jmh/src/jmh/java/com/uber/nullaway/jmh/DFlowMicroBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.uber.nullaway.jmh;

import java.io.IOException;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;

@State(Scope.Benchmark)
public class DFlowMicroBenchmark {

private DataFlowMicroBenchmarkCompiler compiler;

@Setup
public void setup() throws IOException {
compiler = new DataFlowMicroBenchmarkCompiler();
}

@Benchmark
public void compile(Blackhole bh) {
bh.consume(compiler.compile());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.uber.nullaway.jmh;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class DataFlowMicroBenchmarkCompiler {

private final NullawayJavac nullawayJavac;

public DataFlowMicroBenchmarkCompiler() throws IOException {
nullawayJavac = NullawayJavac.createFromSourceString("DFlowBench", SOURCE, "com.uber");
}

public boolean compile() {
return nullawayJavac.compile();
}

private static final String SOURCE;

static {
// For larger benchmarks, we pass file paths to NullawayJavac, based on JVM properties computed
// in build.gradle. Here, to avoid creating and passing yet another JVM property, we just store
// the benchmark source code in a resource file and then load it eagerly into a String via the
// classloader
ClassLoader classLoader = DataFlowMicroBenchmarkCompiler.class.getClassLoader();
try (InputStream inputStream = classLoader.getResourceAsStream("DFlowBench.java")) {
SOURCE = readFromInputStream(inputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static String readFromInputStream(InputStream inputStream) throws IOException {
StringBuilder result = new StringBuilder();
try (BufferedReader br =
new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
result.append(line).append("\n");
}
}
return result.toString();
}
}
20 changes: 20 additions & 0 deletions jmh/src/main/java/com/uber/nullaway/jmh/NullawayJavac.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ public static NullawayJavac create(
compilationUnits, annotatedPackages, classpath, extraErrorProneArgs, extraProcessorPath);
}

/**
* Create a NullawayJavac object to compile a single source file given as a String. This only
* supports cases where no additional classpath, Error Prone arguments, or processor path
* arguments need to be specified.
*
* @param className name of the class to compile
* @param source source code of the class to compile
* @param annotatedPackages argument to pass for "-XepOpt:NullAway:AnnotatedPackages" option
* @throws IOException if a temporary output directory cannot be created
*/
public static NullawayJavac createFromSourceString(
String className, String source, String annotatedPackages) throws IOException {
return new NullawayJavac(
Collections.singletonList(new JavaSourceFromString(className, source)),
annotatedPackages,
null,
Collections.emptyList(),
"");
}

/**
* Configures compilation with javac and NullAway.
*
Expand Down
Loading

0 comments on commit da92a36

Please sign in to comment.