Skip to content

Instantly share code, notes, and snippets.

@ecki
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecki/8350a276caa444a62dce to your computer and use it in GitHub Desktop.
Save ecki/8350a276caa444a62dce to your computer and use it in GitHub Desktop.
JMH Test for apache commons pool-277 (atomic max statistic)
C:\ws\asf\jmh-testmax>java -jar target\benchmarks.jar -t 4
# VM invoker: C:\Program Files\Java\jdk1.8.0_40\jre\bin\java.exe
# VM options: <none>
# Warmup: 2 iterations, 1 s each
# Measurement: 5 iterations, 3 s each
# Timeout: 10 min per iteration
# Threads: 4 threads, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: net.eckenfels.jmh.testmax.MyBenchmark.atomicMax
# Run progress: 0,00% complete, ETA 00:00:34
# Fork: 1 of 1
# Warmup Iteration 1: 186268565,308 ops/s
# Warmup Iteration 2: 183186849,219 ops/s
Iteration 1: 192825846,427 ops/s
Iteration 2: 191575653,100 ops/s
Iteration 3: 187957668,732 ops/s
Iteration 4: 189527803,562 ops/s
Iteration 5: Max was -1 29999
187239202,639 ops/s
Result: 189825234,892 ▒(99.9%) 9098336,043 ops/s [Average]
Statistics: (min, avg, max) = (187239202,639, 189825234,892, 192825846,427), stdev = 2362808,977
Confidence interval (99.9%): [180726898,849, 198923570,935]
# VM invoker: C:\Program Files\Java\jdk1.8.0_40\jre\bin\java.exe
# VM options: <none>
# Warmup: 2 iterations, 1 s each
# Measurement: 5 iterations, 3 s each
# Timeout: 10 min per iteration
# Threads: 4 threads, will synchronize iterations
# Benchmark mode: Throughput, ops/time
# Benchmark: net.eckenfels.jmh.testmax.MyBenchmark.synchronizedMax
# Run progress: 50,00% complete, ETA 00:00:18
# Fork: 1 of 1
# Warmup Iteration 1: 4774026,313 ops/s
# Warmup Iteration 2: 4983825,743 ops/s
Iteration 1: 4845567,894 ops/s
Iteration 2: 4905443,279 ops/s
Iteration 3: 4855380,006 ops/s
Iteration 4: 4870187,428 ops/s
Iteration 5: Max was 29999 -1
4842180,324 ops/s
Result: 4863751,786 ▒(99.9%) 99030,946 ops/s [Average]
Statistics: (min, avg, max) = (4842180,324, 4863751,786, 4905443,279), stdev = 25718,022
Confidence interval (99.9%): [4764720,840, 4962782,732]
# Run complete. Total time: 00:00:37
Benchmark Mode Samples Score Score error Units
n.e.j.t.MyBenchmark.atomicMax thrpt 5 189825234,892 9098336,043 ops/s
n.e.j.t.MyBenchmark.synchronizedMax thrpt 5 4863751,786 99030,946 ops/s
package net.eckenfels.jmh.testmax;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
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.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.annotations.Warmup;
@State(Scope.Benchmark)
@Measurement(iterations=5,time=3,timeUnit=TimeUnit.SECONDS)
@Warmup(iterations=2,time=1,timeUnit=TimeUnit.SECONDS)
@Fork(1)
public class MyBenchmark
{
@State(Scope.Thread)
public static class Seed {
Random rnd = new Random(123l);
}
long currentMax = -1L;
AtomicLong atomMax = new AtomicLong(-1L);
@Benchmark
public void synchronizedMax(Seed seed)
{
syncMax(seed.rnd.nextInt(30000));
}
@Benchmark
public void atomicMax(Seed seed)
{
atomicMax(seed.rnd.nextInt(30000));
}
private void atomicMax(int nextInt) {
long c;
do {
c = atomMax.get();
} while(nextInt > c && !atomMax.compareAndSet(c, (long)nextInt));
}
private synchronized void syncMax(int nextInt) {
if (nextInt > currentMax) {
currentMax = nextInt;
}
}
@TearDown
public void printResult() {
System.out.println("Max was " + currentMax + " " + atomMax);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment