Skip to content

Instantly share code, notes, and snippets.

@franz1981
Created March 15, 2023 09:58
Show Gist options
  • Save franz1981/e46823dbaeb576c1a3344683b2319db8 to your computer and use it in GitHub Desktop.
Save franz1981/e46823dbaeb576c1a3344683b2319db8 to your computer and use it in GitHub Desktop.
//DEPS org.openjdk.jmh:jmh-generator-annprocess:1.36
// to run with javagagent add the following:
// `--javaagent=ap-loader@maxandersen=start,event=cpu,file=profile.html`
package red.hat.puzzles;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
@State(Scope.Benchmark)
@Warmup(iterations = 10, time = 400, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 200, timeUnit = TimeUnit.MILLISECONDS)
@Fork(2)
public class AutoCloseableBenchmark {
public interface A {
}
public interface B {
}
public interface C {
}
public interface D {
}
public interface E {
}
public interface F {
}
public interface G {
}
private enum ManySecondarySupers implements A, B, C, D, E, F, G {
Instance
}
private Object o;
@Setup
public void polluteTypeProfile(Blackhole bh) {
o = ManySecondarySupers.Instance;
}
public static boolean close(Object o) {
if (o instanceof AutoCloseable) {
try {
((AutoCloseable) o).close();
return true;
} catch (Exception ignore) {
return false;
}
} else {
return false;
}
}
@Benchmark
public boolean closeNotAutoCloseable() {
return close(o);
}
}
@franz1981
Copy link
Author

run it with

$ jbang -m org.openjdk.jmh.Main https://gist.github.com/franz1981/e46823dbaeb576c1a3344683b2319db8 -prof "async:output=flamegraph;dir=/tmp;libPath=<YOUR PATH TO AP HERE>/libasyncProfiler.so"

And have fun!

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment