Skip to content

Instantly share code, notes, and snippets.

@vladimirdolzhenko
Created January 16, 2017 14:07
Show Gist options
  • Save vladimirdolzhenko/006448ed04c3a62160339cef34193cc4 to your computer and use it in GitHub Desktop.
Save vladimirdolzhenko/006448ed04c3a62160339cef34193cc4 to your computer and use it in GitHub Desktop.
/*
Benchmark (doTrolling) Mode Cnt Score Error Units
BiasedTrollingTest.string false avgt 5 6.672 ± 1.246 ns/op
BiasedTrollingTest.string true avgt 5 22.096 ± 4.520 ns/op
*/
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
/**
* Note: Has to be run with -XX:BiasedLockingStartupDelay=0
* <p>
* (red) hat tip @shipilev
*
* @author [email protected]
* @since 2017-01-16
*/
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Fork(1)
@Warmup(iterations = 1, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 5000, timeUnit = TimeUnit.MILLISECONDS)
@Threads(1)
@State(Scope.Benchmark)
public class BiasedTrollingTest {
StringBuffer buffer;
@Param( {"false", "true"})
boolean doTrolling;
@Setup
public void setup() throws InterruptedException {
buffer = new StringBuffer();
final int count = 100;
if (doTrolling) {
final StringBuffer[] trololos = new StringBuffer[count];
for (int i = 0; i < count; i++) {
trololos[i] = new StringBuffer();
}
ExecutorService executor = Executors.newFixedThreadPool(4);
for (int t = 0; t < 4; t++) {
executor.submit(() -> {
final ThreadLocalRandom random = ThreadLocalRandom.current();
for (int c = 0; c < 10_000; c++) {
trololos[random.nextInt(count)].append('c');
}
});
}
executor.shutdown();
executor.awaitTermination(1, TimeUnit.DAYS);
}
}
@Benchmark
public String string() {
return buffer.toString();
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(BiasedTrollingTest.class.getSimpleName())
.build();
new Runner(opt).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment