Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package com.github.srvaroa.jmh;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import java.util.concurrent.TimeUnit;
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 4)
@Fork(value = 5, jvmArgsAppend = {"-XX:-UseBiasedLocking", "-XX:BiasedLockingStartupDelay=0"})
public class BiasedLockingBenchmark {
int unsafeCounter = 0;
Object withIdHash;
Object withoutIdHash;
@Setup
public void setup() {
withIdHash = new Object();
withoutIdHash = new Object() {
@Override
public int hashCode() {
return 1;
}
};
withIdHash.hashCode();
withoutIdHash.hashCode();
}
@Benchmark
public void withIdHash(Blackhole bh) {
synchronized(withIdHash) {
bh.consume(unsafeCounter++);
}
}
@Benchmark
public void withoutIdHash(Blackhole bh) {
synchronized(withoutIdHash) {
bh.consume(unsafeCounter++);
}
}
@Benchmark
@Threads(2)
public void withoutIdHashContended(Blackhole bh) {
synchronized(withoutIdHash) {
bh.consume(unsafeCounter++);
}
}
@Benchmark
@Threads(2)
public void withIdHashContended(Blackhole bh) {
synchronized(withoutIdHash) {
bh.consume(unsafeCounter++);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment