Skip to content

Instantly share code, notes, and snippets.

@vladimirdolzhenko
Created February 19, 2016 14:51
Show Gist options
  • Save vladimirdolzhenko/782e790b7fb3c7e19862 to your computer and use it in GitHub Desktop.
Save vladimirdolzhenko/782e790b7fb3c7e19862 to your computer and use it in GitHub Desktop.
package com.markit.n6platform.cache.internal;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import sun.misc.Unsafe;
import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;
/**
* @author vladimir.dolzhenko
* @since 2016-02-05
*/
@Fork(2)
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@BenchmarkMode(Mode.AverageTime)
@Threads(1)
@State(Scope.Benchmark)
public class CopyMemoryGetLongPerfTest {
static final int maxSize = 2 << 10;
public static Unsafe fetchInstance() {
try {
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
return (Unsafe) theUnsafe.get(null);
} catch (Throwable e){
throw new RuntimeException(e);
}
}
@State( Scope.Thread )
public static class ThreadState {
private Unsafe unsafe;
final long memory2k;
final byte[] bytes = new byte[64];
public ThreadState() {
this.unsafe = fetchInstance();
memory2k = unsafe.allocateMemory(maxSize);
}
}
@State(Scope.Benchmark)
public static class BenchmarkState {
private final int byteArrayOffset;
private Unsafe unsafe;
private final long memory2k;
public BenchmarkState() {
unsafe = fetchInstance();
memory2k = unsafe.allocateMemory(maxSize);
byteArrayOffset = unsafe.arrayBaseOffset(byte[].class);
}
public void copy(long memory2k, int srcOffset, int offset, int size){
unsafe.copyMemory(this.memory2k + offset, memory2k + srcOffset, size);
}
public void copy(long memory, int srcOffset, byte[] bytes, int size){
unsafe.copyMemory(null, memory + srcOffset, bytes, byteArrayOffset, size);
}
public long readLong(int offset){
return unsafe.getLong( memory2k + offset);
}
}
@Benchmark
public void copyMemory8Bytes(BenchmarkState state, ThreadState threadState, Blackhole bh){
final int length = 8;
state.copy(state.memory2k, 0, threadState.bytes, length);
}
@Benchmark
public void copyMemory16Bytes(BenchmarkState state, ThreadState threadState, Blackhole bh){
final int length = 16;
state.copy(state.memory2k, 0, threadState.bytes, length);
}
@Benchmark
public void readLongOffset0(BenchmarkState state, Blackhole bh){
bh.consume(state.readLong(0));
}
@Benchmark
public void read2LongOffset0(BenchmarkState state, Blackhole bh){
final long v1 = state.readLong(0 << 3);
final long v2 = state.readLong(1 << 3);
bh.consume(v1);
bh.consume(v2);
}
@Benchmark
public void read8LongOffset0(BenchmarkState state, Blackhole bh){
final long v1 = state.readLong(0 << 3);
final long v2 = state.readLong(1 << 3);
final long v3 = state.readLong(2 << 3);
final long v4 = state.readLong(3 << 3);
final long v5 = state.readLong(4 << 3);
final long v6 = state.readLong(5 << 3);
final long v7 = state.readLong(6 << 3);
final long v8 = state.readLong(7 << 3);
bh.consume(v1);
bh.consume(v2);
bh.consume(v3);
bh.consume(v4);
bh.consume(v5);
bh.consume(v6);
bh.consume(v7);
bh.consume(v8);
}
// @Benchmark
public void readLongOffset3(BenchmarkState state, Blackhole bh){
bh.consume(state.readLong(3));
}
// @Benchmark
public void readLongOffset64(BenchmarkState state, Blackhole bh){
bh.consume(state.readLong(64));
}
// @Benchmark
public void readLongOffset61(BenchmarkState state, Blackhole bh){
bh.consume(state.readLong(61));
}
// @Benchmark
public void readLongOffset60(BenchmarkState state, Blackhole bh){
bh.consume(state.readLong(60));
}
// @Benchmark
public void readLongOffset69(BenchmarkState state, Blackhole bh){
bh.consume(state.readLong(69));
}
// @Benchmark
public void readLongOffset256(BenchmarkState state, Blackhole bh){
bh.consume(state.readLong(256));
}
// @Benchmark
public void copyMemory27Bytes(BenchmarkState state, ThreadState threadState, Blackhole bh){
state.copy(threadState.memory2k, 0, 0, 27);
}
// @Benchmark
public void copyMemory32Bytes(BenchmarkState state, ThreadState threadState, Blackhole bh){
state.copy(threadState.memory2k, 0, 0, 32);
}
// @Benchmark
public void copyMemory32BytesOffset3(BenchmarkState state, ThreadState threadState, Blackhole bh){
state.copy(threadState.memory2k, 0, 3, 32);
}
// @Benchmark
public void copyMemory253Bytes(BenchmarkState state, ThreadState threadState, Blackhole bh){
state.copy(threadState.memory2k, 0, 0, 253);
}
// @Benchmark
public void copyMemory253BytesOffset3(BenchmarkState state, ThreadState threadState, Blackhole bh){
state.copy(threadState.memory2k, 0, 3, 253);
}
// @Benchmark
public void copyMemory256Bytes(BenchmarkState state, ThreadState threadState, Blackhole bh){
state.copy(threadState.memory2k, 0, 0, 256);
}
// @Benchmark
public void copyMemory256BytesOffset7(BenchmarkState state, ThreadState threadState, Blackhole bh){
state.copy(threadState.memory2k, 0, 7, 256);
}
public static void main(String[] args) throws Throwable {
Options opt = new OptionsBuilder()
.include(CopyMemoryGetLongPerfTest.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