changeset 665:dd3505ea9f9e

benchmarks
author Devel 1
date Fri, 17 Nov 2017 12:32:14 +0100
parents ac9b0c4c5065
children 003105670d98
files stress-tester-benchmark/src/main/java/com/passus/st/utils/DataHelperBenchmark.java stress-tester-benchmark/src/main/java/com/passus/st/utils/DataUtilsBenchmark.java stress-tester-benchmark/src/main/java/com/passus/st/utils/MultiByteUtilsBenchmark.java
diffstat 3 files changed, 480 insertions(+), 108 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester-benchmark/src/main/java/com/passus/st/utils/DataHelperBenchmark.java	Fri Nov 17 12:32:14 2017 +0100
@@ -0,0 +1,421 @@
+package com.passus.st.utils;
+
+import com.passus.data.DataHelper;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.nio.IntBuffer;
+import java.util.Arrays;
+import java.util.concurrent.TimeUnit;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+@State(Scope.Thread)
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+public class DataHelperBenchmark {
+
+    private final byte[] BYTES = {1, 2, 3, 45, 7, 6, 5, 4, 3, 2, 1, 1, 33, 33, 44, 44};
+    private final IntBuffer ib;
+    private final DataHelper mbuLe = DataHelper.get(ByteOrder.LITTLE_ENDIAN);
+    private final DataHelper mbuBe = DataHelper.get(ByteOrder.BIG_ENDIAN);
+    private final int I = 0x12345678;
+    private final long L = 0x1234567890abcdefL;
+    private final byte[] DST = new byte[8];
+
+    public DataHelperBenchmark() {
+        ByteBuffer bb = ByteBuffer.wrap(BYTES);
+        ib = bb.asIntBuffer();
+    }
+
+    public int ibSequence() {
+        ib.mark();
+        int v = ib.get() + ib.get() + ib.get() + ib.get();
+        ib.reset();
+        return v;
+    }
+
+    @Benchmark
+    public int ib() {
+        ib.mark();
+        int v = ib.get(0) + ib.get(1) + ib.get(2) + ib.get(3);
+        ib.reset();
+        return v;
+    }
+
+    @Benchmark
+    public int testMbuGetLe() {
+        return mbuLe.getInt4(BYTES, 0) + mbuLe.getInt4(BYTES, 4) + mbuLe.getInt4(BYTES, 8) + mbuLe.getInt4(BYTES, 12);
+    }
+
+    @Benchmark
+    public int testMbuGetBe() {
+        return mbuBe.getInt4(BYTES, 0) + mbuBe.getInt4(BYTES, 4) + mbuBe.getInt4(BYTES, 8) + mbuBe.getInt4(BYTES, 12);
+    }
+
+    @Benchmark
+    public Object testMbuPutIntLe() {
+        mbuLe.writeInt4(BYTES, 0, I);
+        mbuLe.writeInt4(BYTES, 4, I);
+        mbuLe.writeInt4(BYTES, 8, I);
+        mbuLe.writeInt4(BYTES, 12, I);
+        return mbuLe;
+    }
+
+    @Benchmark
+    public Object testMbuPutIntBe() {
+        mbuBe.writeInt4(BYTES, 0, I);
+        mbuBe.writeInt4(BYTES, 4, I);
+        mbuBe.writeInt4(BYTES, 8, I);
+        mbuBe.writeInt4(BYTES, 12, I);
+        return mbuBe;
+    }
+
+    @Benchmark
+    public Object testMbuPutLongLe() {
+        mbuLe.writeLong8(BYTES, 0, L);
+        mbuLe.writeLong8(BYTES, 8, L);
+        return mbuLe;
+    }
+
+    @Benchmark
+    public Object testMbuPutLongBe() {
+        mbuBe.writeLong8(BYTES, 0, L);
+        mbuBe.writeLong8(BYTES, 8, L);
+        return mbuBe;
+    }
+
+    @Benchmark
+    public int testWriteBEV1() {
+        writeV1BE(DST, 0, 6, L);
+        return DST[5];
+    }
+
+    @Benchmark
+    public int testWriteBEV3() {
+        writeV3BE(DST, 0, 6, L);
+        return DST[5];
+    }
+
+    @Benchmark
+    public int testWriteBEV4() {
+        writeV4BE(DST, 0, 6, L);
+        return DST[5];
+    }
+
+    @Benchmark
+    public int testWriteLEV1() {
+        writeV1LE(DST, 0, 6, L);
+        return DST[5];
+    }
+
+    @Benchmark
+    public int testWriteLEV3() {
+        writeV3LE(DST, 0, 6, L);
+        return DST[5];
+    }
+
+    @Benchmark
+    public int testWriteLEV3b() {
+        writeV3bLE(DST, 0, 6, L);
+        return DST[5];
+    }
+
+    @Benchmark
+    public int testWriteLEV4() {
+        writeV4LE(DST, 0, 6, L);
+        return DST[5];
+    }
+
+    @Benchmark
+    public int testWriteLEV4b() {
+        writeV4bLE(DST, 0, 6, L);
+        return DST[5];
+    }
+
+    @Benchmark
+    public int testWriteIntBEV1() {
+        writeIntV1BE(DST, 0, 6, I);
+        return DST[5];
+    }
+
+    @Benchmark
+    public int testWriteIntLEV1() {
+        writeIntV1LE(DST, 0, 6, I);
+        return DST[5];
+    }
+
+    @Benchmark
+    public int testWriteIntBEV3() {
+        writeIntV3BE(DST, 0, 6, I);
+        return DST[5];
+    }
+
+    public static void main(String[] args) throws Exception {
+        Options opt = new OptionsBuilder()
+                .include(DataHelperBenchmark.class.getSimpleName() + ".*")
+                .warmupIterations(6)
+                .measurementIterations(6)
+                .forks(1)
+                .build();
+
+//        new Runner(opt).run();
+        DataHelperBenchmark bench = new DataHelperBenchmark();
+        bench.writeIntV1BE(bench.DST, 2, 3, bench.I);
+        bench.print();
+        bench.writeIntV3BE(bench.DST, 2, 3, bench.I);
+        bench.print();
+        bench.writeIntV1LE(bench.DST, 2, 3, bench.I);
+        bench.print();
+        bench.writeIntV3bLE(bench.DST, 2, 3, bench.I);
+        bench.print();
+        System.out.println("---------------");
+
+        bench.writeV1BE(bench.DST, 0, 6, bench.L);
+        bench.print();
+        bench.writeV3BE(bench.DST, 0, 6, bench.L);
+        bench.print();
+        bench.writeV4BE(bench.DST, 0, 6, bench.L);
+        bench.print();
+        bench.writeV1LE(bench.DST, 0, 6, bench.L);
+        bench.print();
+        bench.writeV3LE(bench.DST, 0, 6, bench.L);
+        bench.print();
+        bench.writeV3bLE(bench.DST, 0, 6, bench.L);
+        bench.print();
+        bench.writeV4LE(bench.DST, 0, 6, bench.L);
+        bench.print();
+        bench.writeV4bLE(bench.DST, 0, 6, bench.L);
+        bench.print();
+    }
+
+    private void writeV1BE(byte[] dst, int off, int bytes, long val) {
+        for (int i = bytes - 1; i >= 0; i--) {
+            dst[off + i] = (byte) (val & 0xFF);
+            val >>= 8;
+        }
+    }
+
+    private void writeV1LE(byte[] dst, int off, int bytes, long val) {
+        for (int i = 0; i < bytes; i++) {
+            dst[off + i] = (byte) (val & 0xFF);
+            val >>= 8;
+        }
+    }
+
+    private void writeIntV1BE(byte[] dst, int off, int bytes, int val) {
+        for (int i = bytes - 1; i >= 0; i--) {
+            dst[off + i] = (byte) (val & 0xFF);
+            val >>= 8;
+        }
+    }
+
+    private void writeIntV1LE(byte[] dst, int off, int bytes, int val) {
+        for (int i = 0; i < bytes; i++) {
+            dst[off + i] = (byte) (val & 0xFF);
+            val >>= 8;
+        }
+    }
+
+    private void writeV3BE(byte[] dst, int off, int bytes, long val) {
+        switch (bytes) {
+            case 8:
+                dst[off + 7] = (byte) val;
+                val >>= 8;
+            case 7:
+                dst[off + 6] = (byte) val;
+                val >>= 8;
+            case 6:
+                dst[off + 5] = (byte) val;
+                val >>= 8;
+            case 5:
+                dst[off + 4] = (byte) val;
+                val >>= 8;
+            case 4:
+                dst[off + 3] = (byte) val;
+                val >>= 8;
+            case 3:
+                dst[off + 2] = (byte) val;
+                val >>= 8;
+            case 2:
+                dst[off + 1] = (byte) val;
+                val >>= 8;
+            case 1:
+                dst[off] = (byte) val;
+        }
+    }
+
+    private void writeIntV3BE(byte[] dst, int off, int bytes, int val) {
+        switch (bytes) {
+            case 4:
+                dst[off + 3] = (byte) val;
+                val >>= 8;
+            case 3:
+                dst[off + 2] = (byte) val;
+                val >>= 8;
+            case 2:
+                dst[off + 1] = (byte) val;
+                val >>= 8;
+            case 1:
+                dst[off] = (byte) val;
+        }
+    }
+
+    private void writeV3LE(byte[] dst, int off, int bytes, long val) {
+        switch (bytes) {
+            case 8:
+                dst[off++] = (byte) val;
+                val >>= 8;
+            case 7:
+                dst[off++] = (byte) val;
+                val >>= 8;
+            case 6:
+                dst[off++] = (byte) val;
+                val >>= 8;
+            case 5:
+                dst[off++] = (byte) val;
+                val >>= 8;
+            case 4:
+                dst[off++] = (byte) val;
+                val >>= 8;
+            case 3:
+                dst[off++] = (byte) val;
+                val >>= 8;
+            case 2:
+                dst[off++] = (byte) val;
+                val >>= 8;
+            case 1:
+                dst[off] = (byte) val;
+        }
+    }
+
+    private void writeV3bLE(byte[] dst, int off, int bytes, long val) {
+        off = off + bytes - 8;
+        switch (bytes) {
+            case 8:
+                dst[off] = (byte) val;
+                val >>= 8;
+            case 7:
+                dst[off + 1] = (byte) val;
+                val >>= 8;
+            case 6:
+                dst[off + 2] = (byte) val;
+                val >>= 8;
+            case 5:
+                dst[off + 3] = (byte) val;
+                val >>= 8;
+            case 4:
+                dst[off + 4] = (byte) val;
+                val >>= 8;
+            case 3:
+                dst[off + 5] = (byte) val;
+                val >>= 8;
+            case 2:
+                dst[off + 6] = (byte) val;
+                val >>= 8;
+            case 1:
+                dst[off + 7] = (byte) val;
+        }
+    }
+
+    private void writeIntV3bLE(byte[] dst, int off, int bytes, int val) {
+        off = off + bytes - 4;
+        switch (bytes) {
+            case 4:
+                dst[off] = (byte) val;
+                val >>= 8;
+            case 3:
+                dst[off + 1] = (byte) val;
+                val >>= 8;
+            case 2:
+                dst[off + 2] = (byte) val;
+                val >>= 8;
+            case 1:
+                dst[off + 3] = (byte) val;
+        }
+    }
+
+    private void writeV4BE(byte[] dst, int off, int bytes, long val) {
+        int shift = 8 * (8 - bytes);
+        switch (bytes) {
+            case 8:
+                dst[off + 7] = (byte) val;
+            case 7:
+                dst[off + 6] = (byte) (val >> (8 - shift));
+            case 6:
+                dst[off + 5] = (byte) (val >> (16 - shift));
+            case 5:
+                dst[off + 4] = (byte) (val >> (24 - shift));
+            case 4:
+                dst[off + 3] = (byte) (val >> (32 - shift));
+            case 3:
+                dst[off + 2] = (byte) (val >> (40 - shift));
+            case 2:
+                dst[off + 1] = (byte) (val >> (48 - shift));
+            case 1:
+                dst[off] = (byte) (val >> (56 - shift));
+        }
+    }
+
+    private void writeV4LE(byte[] dst, int off, int bytes, long val) {
+        int shift = 8 * (8 - bytes);
+        switch (bytes) {
+            case 8:
+                dst[off++] = (byte) val;
+            case 7:
+                dst[off++] = (byte) (val >> (8 - shift));
+            case 6:
+                dst[off++] = (byte) (val >> (16 - shift));
+            case 5:
+                dst[off++] = (byte) (val >> (24 - shift));
+            case 4:
+                dst[off++] = (byte) (val >> (32 - shift));
+            case 3:
+                dst[off++] = (byte) (val >> (40 - shift));
+            case 2:
+                dst[off++] = (byte) (val >> (48 - shift));
+            case 1:
+                dst[off] = (byte) (val >> (56 - shift));
+        }
+    }
+
+    private void writeV4bLE(byte[] dst, int off, int bytes, long val) {
+        int shift = 8 * (8 - bytes);
+        off = off + bytes - 8;
+        switch (bytes) {
+            case 8:
+                dst[off] = (byte) val;
+            case 7:
+                dst[off + 1] = (byte) (val >> (8 - shift));
+            case 6:
+                dst[off + 2] = (byte) (val >> (16 - shift));
+            case 5:
+                dst[off + 3] = (byte) (val >> (24 - shift));
+            case 4:
+                dst[off + 4] = (byte) (val >> (32 - shift));
+            case 3:
+                dst[off + 5] = (byte) (val >> (40 - shift));
+            case 2:
+                dst[off + 6] = (byte) (val >> (48 - shift));
+            case 1:
+                dst[off + 7] = (byte) (val >> (56 - shift));
+        }
+    }
+
+    private void print() {
+        for (byte b : DST) {
+            System.out.format("%02x ", b);
+        }
+        System.out.println("");
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester-benchmark/src/main/java/com/passus/st/utils/DataUtilsBenchmark.java	Fri Nov 17 12:32:14 2017 +0100
@@ -0,0 +1,59 @@
+package com.passus.st.utils;
+
+import static com.passus.data.DataUtils.countBytes;
+import java.util.concurrent.TimeUnit;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OperationsPerInvocation;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.runner.Runner;
+import org.openjdk.jmh.runner.options.Options;
+import org.openjdk.jmh.runner.options.OptionsBuilder;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+@State(Scope.Thread)
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+public class DataUtilsBenchmark {
+
+    @Benchmark
+    @OperationsPerInvocation(17)
+    public int testCountBytes() {
+        int sum = 0;
+        sum += countBytes(0x00000000_00000000L);
+        sum += countBytes(0x00000000_00000001L);
+        sum += countBytes(0x00000000_000000ffL);
+        sum += countBytes(0x00000000_00000100L);
+        sum += countBytes(0x00000000_0000ffffL);
+        sum += countBytes(0x00000000_00010000L);
+        sum += countBytes(0x00000000_00ffffffL);
+        sum += countBytes(0x00000000_01000000L);
+        sum += countBytes(0x00000000_ffffffffL);
+        sum += countBytes(0x00000001_00000000L);
+        sum += countBytes(0x000000ff_ffffffffL);
+        sum += countBytes(0x00000100_00000000L);
+        sum += countBytes(0x0000ffff_ffffffffL);
+        sum += countBytes(0x00010000_00000000L);
+        sum += countBytes(0x00ffffff_ffffffffL);
+        sum += countBytes(0x01000000_00000000L);
+        sum += countBytes(0xffffffff_ffffffffL);
+        return sum;
+    }
+
+    public static void main(String[] args) throws Exception {
+        Options opt = new OptionsBuilder()
+                .include(DataUtilsBenchmark.class.getSimpleName() + ".*")
+                .warmupIterations(5)
+                .measurementIterations(5)
+                .forks(1)
+                .build();
+
+        new Runner(opt).run();
+    }
+}
--- a/stress-tester-benchmark/src/main/java/com/passus/st/utils/MultiByteUtilsBenchmark.java	Thu Nov 16 14:49:03 2017 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,108 +0,0 @@
-package com.passus.st.utils;
-
-import com.passus.data.DataHelper;
-import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
-import java.nio.IntBuffer;
-import java.util.concurrent.TimeUnit;
-import org.openjdk.jmh.annotations.Benchmark;
-import org.openjdk.jmh.annotations.BenchmarkMode;
-import org.openjdk.jmh.annotations.Mode;
-import org.openjdk.jmh.annotations.OutputTimeUnit;
-import org.openjdk.jmh.annotations.Scope;
-import org.openjdk.jmh.annotations.State;
-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;
-
-/**
- *
- * @author mikolaj.podbielski
- */
-@State(Scope.Thread)
-@BenchmarkMode(Mode.AverageTime)
-@OutputTimeUnit(TimeUnit.NANOSECONDS)
-public class MultiByteUtilsBenchmark {
-
-    private final byte[] BYTES = {1, 2, 3, 45, 7, 6, 5, 4, 3, 2, 1, 1, 33, 33, 44, 44};
-    private final IntBuffer ib;
-    private final DataHelper mbuLe = DataHelper.get(ByteOrder.LITTLE_ENDIAN);
-    private final DataHelper mbuBe = DataHelper.get(ByteOrder.BIG_ENDIAN);
-    private final int I = 0x12345678;
-    private final long L = 0x1234567890abcdefL;
-
-    public MultiByteUtilsBenchmark() {
-        ByteBuffer bb = ByteBuffer.wrap(BYTES);
-        ib = bb.asIntBuffer();
-    }
-
-    /*@Benchmark
-    public int testIbSequence() {
-        ib.mark();
-        int v = ib.get() + ib.get() + ib.get() + ib.get();
-        ib.reset();
-        return v;
-    }
-
-    @Benchmark
-    public int testIb() {
-        ib.mark();
-        int v = ib.get(0) + ib.get(1) + ib.get(2) + ib.get(3);
-        ib.reset();
-        return v;
-    }
-
-    @Benchmark
-    public int testMbuGetLe() {
-        return mbuLe.loadI(BYTES, 0) + mbuLe.loadI(BYTES, 4) + mbuLe.loadI(BYTES, 8) + mbuLe.loadI(BYTES, 12);
-    }
-
-    @Benchmark
-    public int testMbuGetBe() {
-        return mbuBe.loadI(BYTES, 0) + mbuBe.loadI(BYTES, 4) + mbuBe.loadI(BYTES, 8) + mbuBe.loadI(BYTES, 12);
-    }
-
-    @Benchmark
-    public Object testMbuPutIntLe() {
-        mbuLe.put(BYTES, 0, I);
-        mbuLe.put(BYTES, 4, I);
-        mbuLe.put(BYTES, 8, I);
-        mbuLe.put(BYTES, 12, I);
-        return mbuLe;
-    }
-
-    @Benchmark
-    public Object testMbuPutIntBe() {
-        mbuBe.put(BYTES, 0, I);
-        mbuBe.put(BYTES, 4, I);
-        mbuBe.put(BYTES, 8, I);
-        mbuBe.put(BYTES, 12, I);
-        return mbuBe;
-    }
-
-    @Benchmark
-    public Object testMbuPutLongLe() {
-        mbuLe.put(BYTES, 0, L);
-        mbuLe.put(BYTES, 8, L);
-        return mbuLe;
-    }
-
-    @Benchmark
-    public Object testMbuPutLongBe() {
-        mbuBe.put(BYTES, 0, L);
-        mbuBe.put(BYTES, 8, L);
-        return mbuBe;
-    }
-*/
-    public static void main(String[] args) throws RunnerException {
-        Options opt = new OptionsBuilder()
-                .include(MultiByteUtilsBenchmark.class.getSimpleName() + ".*")
-                .warmupIterations(5)
-                .measurementIterations(5)
-                .forks(1)
-                .build();
-
-        new Runner(opt).run();
-    }
-}