Skip to content

Instantly share code, notes, and snippets.

@vladimirdolzhenko
Last active February 14, 2016 09:24
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 vladimirdolzhenko/2fbd7b2d5dae9a58f806 to your computer and use it in GitHub Desktop.
Save vladimirdolzhenko/2fbd7b2d5dae9a58f806 to your computer and use it in GitHub Desktop.
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(3)
@Warmup(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@BenchmarkMode(Mode.AverageTime)
@Threads(4)
@State(Scope.Benchmark)
public class CopyPerfTest {
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;
public ThreadState() {
this.unsafe = fetchInstance();
memory2k = unsafe.allocateMemory(maxSize);
}
}
@State(Scope.Benchmark)
public static class BenchmarkState {
private Unsafe unsafe;
private final long memory2k;
public BenchmarkState() {
unsafe = fetchInstance();
memory2k = unsafe.allocateMemory( maxSize);
}
public void copy(long memory2k, int srcOffset, int offset, int size){
unsafe.copyMemory(this.memory2k + offset, memory2k + srcOffset, size);
}
public int readByte(int offset){
return unsafe.getByte( memory2k + offset);
}
public int readInt(int offset){
return unsafe.getInt( memory2k + offset);
}
public long readLong(int offset){
return unsafe.getLong( memory2k + offset);
}
public void putLong(int offset, long value){
unsafe.putLong( memory2k + offset, value);
}
public void putInt(int offset, int value){
unsafe.putInt( memory2k + offset, value);
}
}
@Benchmark
public void copyMemory4Bytes(BenchmarkState state, ThreadState threadState, Blackhole bh){
state.copy(threadState.memory2k, 0, 0, 4);
}
@Benchmark
public void copyMemory4BytesOffset3(BenchmarkState state, ThreadState threadState, Blackhole bh){
state.copy(threadState.memory2k, 0, 3, 4);
}
@Benchmark
public void copyMemory8Bytes(BenchmarkState state, ThreadState threadState, Blackhole bh){
state.copy(threadState.memory2k, 0, 0, 8);
}
@Benchmark
public void copyMemory8BytesOffset1(BenchmarkState state, ThreadState threadState, Blackhole bh){
state.copy(threadState.memory2k, 0, 1, 8);
}
@Benchmark
public void copyMemory8BytesOffset5(BenchmarkState state, ThreadState threadState, Blackhole bh){
state.copy(threadState.memory2k, 0, 5, 8);
}
@Benchmark
public void copyMemory8BytesOffset3(BenchmarkState state, ThreadState threadState, Blackhole bh){
state.copy(threadState.memory2k, 0, 3, 8);
}
@Benchmark
public void readByteOffset0(BenchmarkState state, Blackhole bh){
bh.consume(state.readByte(0));
}
@Benchmark
public void readByteOffset3(BenchmarkState state, Blackhole bh){
bh.consume(state.readByte(3));
}
@Benchmark
public void readIntOffset0(BenchmarkState state, Blackhole bh){
bh.consume(state.readInt(0));
}
@Benchmark
public void readIntOffset3(BenchmarkState state, Blackhole bh){
bh.consume(state.readInt(3));
}
@Benchmark
public void readLongOffset0(BenchmarkState state, Blackhole bh){
bh.consume(state.readLong(0));
}
@Benchmark
public void readLongOffset3(BenchmarkState state, Blackhole bh){
bh.consume(state.readLong(3));
}
@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(CopyPerfTest.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