Skip to content

Instantly share code, notes, and snippets.

@cubedtear
Created November 5, 2016 15:30
Show Gist options
  • Save cubedtear/001bd88a615920301908054050834417 to your computer and use it in GitHub Desktop.
Save cubedtear/001bd88a615920301908054050834417 to your computer and use it in GitHub Desktop.
BDS vs BDSv2 tester class
package io.github.cubedtear.jcubit.bds;
import com.google.common.base.Strings;
import io.github.cubedtear.jcubit.timing.Profiler;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
/**
* @author Aritz Lopez
*/
@Category(BDSv2.class)
public class BDSv2Test {
private static final DecimalFormat DF = new DecimalFormat("0.######");
private final int INT_VALUE = Integer.MAX_VALUE / 2;
private final long LONG_VALUE = Integer.MAX_VALUE * 52L;
private final String STRING_VALUE = "h\u00aall\u00f2";
private final byte[] BYTE_ARRAY = {Byte.MIN_VALUE, 0, Byte.MAX_VALUE};
private final short[] SHORT_ARRAY = {Short.MIN_VALUE, 0, Short.MAX_VALUE};
private final char[] CHAR_ARRAY = {Character.MIN_VALUE, Character.MAX_VALUE, Character.MAX_SURROGATE};
private final int[] INT_ARRAY = {Integer.MIN_VALUE, 0, Integer.MAX_VALUE};
private final long[] LONG_ARRAY = {Long.MIN_VALUE, 0, Long.MAX_VALUE};
private final float[] FLOAT_ARRAY = {Float.MIN_VALUE, Float.MIN_NORMAL, 0, Float.MAX_VALUE};
private final double[] DOUBLE_ARRAY = {Double.MIN_VALUE, Double.MIN_NORMAL, 0.0, Double.MAX_VALUE};
private final String[] STRING_ARRAY = {"", " ", Strings.repeat("X", 10)};
@Test
public void testCompareBDSSpeeds() throws Exception {
final int iterations = 1000;
// Use jmc (Java Mission Control) to read the output of the "Profile" run configuration
Profiler p = Profiler.getInstance("");
for (int j = 0; j < iterations * 5; j++) {
long[] bdsv2 = new long[]{0, 0, 0, 0};
long[] bds = new long[]{0, 0, 0, 0};
//for (int i = 0; i < iterations; i++) {
runBDS(false, p);
runBDSv2(false, p);
bdsv2[0] += p.getSectionTime("bds2-build");
bdsv2[1] += p.getSectionTime("bds2-serial");
bdsv2[2] += p.getSectionTime("bds2-deserial");
bdsv2[3] += p.getSectionTime("bds2-assert");
bds[0] += p.getSectionTime("bds-build");
bds[1] += p.getSectionTime("bds-serial");
bds[2] += p.getSectionTime("bds-deserial");
bds[3] += p.getSectionTime("bds-assert");
System.gc();
//}
System.out.print((j + 1) + ";" + DF.format((bds[0] + bds[1] + bds[2] + bds[3]) / ((double) iterations * 4)) + ";");
System.out.println(DF.format((bdsv2[0] + bdsv2[1] + bdsv2[2] + bdsv2[3]) / ((double) iterations * 4)));
}
}
public static void main(String[] args) throws Exception {
long times = 0;
int iter = 10000;
for (int i = 0; i < iter; i++) {
times += new BDSv2Test().runBDSv2(false, null);
}
//System.out.println((times / (double) iter) / 1e9);
String str = "\ud841\uDF0E\uD841\uDF31\uD841\uDF79\uD843\uDC53\uD843\uDC78\uD843\uDC96\uD843\uDCCF\uD843\uDCD5\uD843\uDD15\uD843\uDD7C\uD843\uDD7F\uD843\uDE0E\uD843\uDE0F\uD843\uDE77\uD843\uDE9D\uD843\uDEA2\uD843\uDED7\uD843\uDEF9\uD843\uDEFA\uD843\uDF2D\uD843\uDF2E\uD843\uDF4C\uD843\uDFB4\uD843\uDFBC\uD843\uDFEA\uD844\uDC5C\uD844\uDC6F\uD844\uDC75\uD844\uDC76\uD844\uDC7B\uD844\uDCC1\uD844\uDCC9\uD844\uDDD9\uD848\uDCC7\uD849\uDFB5\uD84A\uDED5\uD84A\uDF43\uD84A\uDFCA\uD84B\uDC51\uD84B\uDC55\uD84B\uDCC2\uD84B\uDD08\uD84B\uDD4C\uD84B\uDD67\uD84B\uDEB3\uD84F\uDCB7\uD851\uDCD3\uD853\uDDB8\uD853\uDDEA\uD854\uDD2B\uD858\uDE58\uD859\uDFCC\uD85A\uDDF2\uD85A\uDDFA\uD85E\uDE3E\uD860\uDD5D\uD860\uDE07\uD860\uDEE2\uD863\uDCCA\uD863\uDCCD\uD863\uDCD2\uD867\uDD98";
Profiler p = Profiler.getInstance("");
testString(str, p);
testString(Strings.repeat("a", str.length()), p);
}
private static void testString(String str, Profiler p) {
p.startSection("Custom");
final int length = str.length();
final char buffer[] = new char[length];
str.getChars(0, length, buffer, 0);
final byte b[] = new byte[length * 2];
for (int j = 0; j < length; j++) {
b[j * 2] = (byte) (buffer[j] & 0xFF);
b[j * 2 + 1] = (byte) (buffer[j] >> 8);
}
p.endSection();
p.startSection("Custom-reconstruct");
char[] reconstruct = new char[b.length / 2];
for (int j = 0; j < b.length / 2; j++) {
reconstruct[j] = (char) ((b[j * 2 + 1] & 0xFF) << 8 | b[j * 2] & 0xFF);
}
String s = new String(reconstruct);
if (!s.equals(str)) throw new AssertionError("Custom failed");
p.endSection();
p.startSection("UTF8");
byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
p.endSection();
p.startSection("UTF8-Reconstruct");
String ss = new String(bytes, StandardCharsets.UTF_8);
if (!ss.equals(str)) throw new AssertionError("UTF8 failed");
p.endSection();
System.out.println("Custom: " + (p.getSectionTime("custom") + p.getSectionTime("custom-reconstruct")));
System.out.println("UTF8: " + (p.getSectionTime("utf8") + p.getSectionTime("utf8-reconstruct")));
System.out.println("Custom size: " + b.length);
System.out.println("UTF8 size: " + bytes.length);
}
private long runBDSv2(boolean asserts, Profiler p) throws SerializationException, IOException {
if (p != null) p.startSection("bds2-build");
BDSv2 top = new BDSv2();
top.addString("", "");
BDSv2 nested0 = new BDSv2();
nested0.addByte("byte", (byte) 26);
nested0.addShort("short", (short) 84);
nested0.addChar("char", '\u00fc');
nested0.addInt("int", INT_VALUE);
nested0.addLong("long", LONG_VALUE);
nested0.addFloat("float", -0.5f);
nested0.addDouble("double", -536.244);
nested0.addString("string", STRING_VALUE);
nested0.addBytes("bytes", BYTE_ARRAY);
nested0.addShorts("shorts", SHORT_ARRAY);
nested0.addChars("chars", CHAR_ARRAY);
nested0.addInts("ints", INT_ARRAY);
nested0.addLongs("longs", LONG_ARRAY);
nested0.addFloats("floats", FLOAT_ARRAY);
nested0.addDoubles("doubles", DOUBLE_ARRAY);
nested0.addStrings("strings", STRING_ARRAY);
BDSv2 nested1 = new BDSv2();
nested1.addByte("works", (byte) 42);
top.addBDSs("bds", new BDSv2[]{nested0, nested1});
top.addInt("nice", -51000548);
long initial = System.nanoTime();
if (p != null) {
p.endSection();
p.startSection("bds2-serial");
}
byte[] serialized = top.write();
if (p != null) {
p.endSection();
p.startSection("bds2-deserial");
}
BDSv2 parsedTop = BDSv2.parse(serialized);
if (p != null) {
p.endSection();
p.startSection("bds2-assert");
}
long result = System.nanoTime() - initial;
BDSv2 parsed0 = parsedTop.getBDSs("bds")[0];
BDSv2 parsed1 = parsedTop.getBDSs("bds")[1];
if (asserts) {
assertEquals(-51000548, (int) parsedTop.getInt("nice"));
assertEquals((byte) 26, (byte) parsed0.getByte("byte"));
assertEquals((short) 84, (short) parsed0.getShort("short"));
assertEquals('\u00fc', (char) parsed0.getChar("char"));
assertEquals(INT_VALUE, (int) parsed0.getInt("int"));
assertEquals(LONG_VALUE, (long) parsed0.getLong("long"));
assertEquals(-0.5f, parsed0.getFloat("float"), -0.5f / 10e3f);
assertEquals(-536.244, parsed0.getDouble("double"), -536.244 / 10e6);
assertEquals(STRING_VALUE, parsed0.getString("string"));
assertArrayEquals(BYTE_ARRAY, parsed0.getBytes("bytes"));
assertArrayEquals(SHORT_ARRAY, parsed0.getShorts("shorts"));
assertArrayEquals(CHAR_ARRAY, parsed0.getChars("chars"));
assertArrayEquals(INT_ARRAY, parsed0.getInts("ints"));
assertArrayEquals(LONG_ARRAY, parsed0.getLongs("longs"));
assertArrayEquals(FLOAT_ARRAY, parsed0.getFloats("floats"), 1e-5f);
assertArrayEquals(DOUBLE_ARRAY, parsed0.getDoubles("doubles"), 1e-6f);
assertArrayEquals(STRING_ARRAY, parsed0.getStrings("strings"));
assertEquals((byte) 42, (byte) parsed1.getByte("works"));
}
if (p != null) p.endSection();
return result;
}
private long runBDS(boolean asserts, Profiler p) {
if (p != null) p.startSection("bds-build");
BDS top = new BDS("");
BDS nested0 = new BDS("");
nested0.addByte("byte", (byte) 26);
nested0.addShort("short", (short) 84);
nested0.addChar("char", '\u00fc');
nested0.addInt("int", INT_VALUE);
nested0.addLong("long", LONG_VALUE);
nested0.addFloat("float", -0.5f);
nested0.addDouble("double", -536.244);
nested0.addString("string", STRING_VALUE);
nested0.addBytes("bytes", BYTE_ARRAY);
nested0.addShorts("shorts", SHORT_ARRAY);
nested0.addChars("chars", CHAR_ARRAY);
nested0.addInts("ints", INT_ARRAY);
nested0.addLongs("longs", LONG_ARRAY);
nested0.addFloats("floats", FLOAT_ARRAY);
nested0.addDoubles("doubles", DOUBLE_ARRAY);
nested0.addStrings("strings", STRING_ARRAY);
BDS nested1 = new BDS("");
nested1.addByte("works", (byte) 42);
top.addBDSs("bds", new BDS[]{nested0, nested1});
top.addInt("nice", -51000548);
long initial = System.nanoTime();
if (p != null) {
p.endSection();
p.startSection("bds-serial");
}
byte[] serialized = top.write();
if (p != null) {
p.endSection();
p.startSection("bds-deserial");
}
BDS parsedTop = BDS.load(serialized);
if (p != null) {
p.endSection();
p.startSection("bds-assert");
}
long result = System.nanoTime() - initial;
BDS parsed0 = parsedTop.getBDSArray("bds")[0];
BDS parsed1 = parsedTop.getBDSArray("bds")[1];
if (asserts) {
assertEquals(-51000548, (int) parsedTop.getInt("nice"));
assertEquals((byte) 26, (byte) parsed0.getByte("byte"));
assertEquals((short) 84, (short) parsed0.getShort("short"));
assertEquals('\u00fc', (char) parsed0.getChar("char"));
assertEquals(INT_VALUE, (int) parsed0.getInt("int"));
assertEquals(LONG_VALUE, (long) parsed0.getLong("long"));
assertEquals(-0.5f, parsed0.getFloat("float"), -0.5f / 10e3f);
assertEquals(-536.244, parsed0.getDouble("double"), -536.244 / 10e6);
assertEquals(STRING_VALUE, parsed0.getString("string"));
assertArrayEquals(BYTE_ARRAY, parsed0.getByteArray("bytes"));
assertArrayEquals(SHORT_ARRAY, parsed0.getShortArray("shorts"));
assertArrayEquals(CHAR_ARRAY, parsed0.getCharArray("chars"));
assertArrayEquals(INT_ARRAY, parsed0.getIntArray("ints"));
assertArrayEquals(LONG_ARRAY, parsed0.getLongArray("longs"));
assertArrayEquals(FLOAT_ARRAY, parsed0.getFloatArray("floats"), 1e-5f);
assertArrayEquals(DOUBLE_ARRAY, parsed0.getDoubleArray("doubles"), 1e-6f);
assertArrayEquals(STRING_ARRAY, parsed0.getStringArray("strings"));
assertEquals((byte) 42, (byte) parsed1.getByte("works"));
}
if (p != null) p.endSection();
return result;
}
@Test
public void testBDSv2() throws Exception {
System.out.println("BDSv2 Elapsed time: " + DF.format(runBDSv2(true, null) / 1e9));
}
@Test
public void testBDS() throws Exception {
System.out.println("BDS Elapsed time: " + DF.format(runBDS(true, null) / 1e9));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment