Last active
October 15, 2020 11:09
-
-
Save srvaroa/aa1f6c273ea6ae215bbf43e481f7689e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(withIdHash) { | |
bh.consume(unsafeCounter++); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
withIdHashContended and withoutIdHashContended are exeactly same