Skip to content

Instantly share code, notes, and snippets.

View andrebrait's full-sized avatar

Andre Brait andrebrait

View GitHub Profile
@State(Scope.Thread)
public static class MyState {
public int a = 1;
public int b = 2;
}
@Benchmark
public int testMethod(MyState state) {
int sum = state.a + state.b;
return sum;
@Benchmark
public int testMethod() {
int sum = 3;
return sum;
}
@Benchmark
public void testMethod(Blackhole blackhole) {
int a = 1;
int b = 2;
int sum = a + b;
blackhole.consume(sum);
}
@Benchmark
public int testMethod() {
int a = 1;
int b = 2;
int sum = a + b;
return sum;
}
@Benchmark
public void testMethod() {
int a = 1;
int b = 2;
int sum = a + b;
}
REMEMBER: The numbers below are just data. To gain reusable insights, you need to follow up on
why the numbers are the way they are. Use profilers (see -prof, -lprof), design factorial
experiments, perform baseline and negative tests that provide experimental control, make sure
the benchmarking environment is safe on JVM/OS/HW level, ask for reviews from the domain experts.
Do not assume the numbers tell you what you want them to tell.
Benchmark (listSize) Mode Cnt Score Error Units
MyBenchmark.atomicLong 1000000 avgt 5 0.026 ± 0.001 s/op
MyBenchmark.atomicLong 10000000 avgt 5 0.263 ± 0.003 s/op
MyBenchmark.atomicLong 100000000 avgt 5 2.504 ± 0.466 s/op
package com.avenuecode.snippet;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
java -jar target/benchmarks.jar
mvn clean install
package com.avenuecode.snippet;
import org.openjdk.jmh.annotations.Benchmark;
public class MyBenchmark {
@Benchmark
public void testMethod() {
// This is a demo/sample template for building your JMH benchmarks. Edit as needed.
// Put your benchmark code here.