test[protocol]: 试极端大的对象序列化和反序列化情况,极端大的对象指的是字段多,对象大,方法大

This commit is contained in:
godotg
2022-11-14 20:34:43 +08:00
parent 418a5a7983
commit 3ea0490067
2 changed files with 7514 additions and 66 deletions
@@ -41,7 +41,7 @@ import java.util.concurrent.Executors;
*/
public class SpeedTest {
private static int benchmark = 10_0000;
public static int benchmark = 10_0000;
/**
* 单线程性能测试
@@ -99,7 +99,7 @@ public class SpeedTest {
System.setProperty("io.netty.buffer.checkAccessible", "false");
System.setProperty("io.netty.buffer.checkBounds", "false");
ByteBuf buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 100, 1_0000);
ByteBuf buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 100, 10_0000);
// 序列化和反序列化简单对象
long startTime = System.currentTimeMillis();
@@ -133,6 +133,17 @@ public class SpeedTest {
}
System.out.println(StringUtils.format("[zfoo] [复杂对象] [thread:{}] [size:{}] [time:{}]", Thread.currentThread().getName(), buffer.writerIndex(), System.currentTimeMillis() - startTime));
// 序列化和反序列化极端大的对象
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
buffer.clear();
ProtocolManager.write(buffer, VeryBigObject.veryBigObject);
var packet = ProtocolManager.read(buffer);
}
System.out.println(StringUtils.format("[zfoo] [超大对象] [thread:{}] [size:{}] [time:{}]", Thread.currentThread().getName(), buffer.writerIndex(), System.currentTimeMillis() - startTime));
}
@Ignore
@@ -141,7 +152,7 @@ public class SpeedTest {
try {
var kryo = kryos.get();
var output = new Output(1024 * 8);
var output = new Output(10_0000);
var input = new Input(output.getBuffer());
// 序列化和反序列化简单对象
@@ -175,6 +186,16 @@ public class SpeedTest {
var mess = kryo.readObject(input, ComplexObject.class);
}
System.out.println(StringUtils.format("[kryo] [复杂对象] [thread:{}] [size:{}] [time:{}]", Thread.currentThread().getName(), output.position(), System.currentTimeMillis() - startTime));
// 序列化和反序列化极端大的对象
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
input.reset();
output.reset();
kryo.writeObject(output, VeryBigObject.veryBigObject);
var mess = kryo.readObject(input, ComplexObject.class);
}
System.out.println(StringUtils.format("[kryo] [超大对象] [thread:{}] [size:{}] [time:{}]", Thread.currentThread().getName(), output.position(), System.currentTimeMillis() - startTime));
} catch (Exception e) {
System.err.println(e.getMessage());
System.err.println("JDK17 运行kryo会报错,等kryo修复bug");
@@ -264,14 +285,15 @@ public class SpeedTest {
countdown.await();
}
private static final int threadNum = Runtime.getRuntime().availableProcessors() - 1;
private static final ExecutorService[] executors = new ExecutorService[threadNum];
public static final int threadNum = Runtime.getRuntime().availableProcessors() - 1;
public static final ExecutorService[] executors = new ExecutorService[threadNum];
// kryo协议注册
private static final ThreadLocal<Kryo> kryos = new ThreadLocal<>() {
public static final ThreadLocal<Kryo> kryos = new ThreadLocal<>() {
@Override
protected Kryo initialValue() {
var kryo = new Kryo();
kryo.register(VeryBigObject.class);
kryo.register(ComplexObject.class);
kryo.register(NormalObject.class);
kryo.register(SimpleObject.class);
@@ -320,7 +342,7 @@ public class SpeedTest {
// op.getGenerateLanguages().add(CodeLanguage.Protobuf);
// zfoo协议注册(其实就是:将Set里面的协议号和对应的类注册好,这样子就可以根据协议号知道是反序列化为哪个类)
ProtocolManager.initProtocolAuto(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class), op);
ProtocolManager.initProtocolAuto(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class, VeryBigObject.class), op);
for (int i = 0; i < executors.length; i++) {
executors[i] = Executors.newSingleThreadExecutor();
@@ -329,68 +351,68 @@ public class SpeedTest {
// -------------------------------------------以下为测试用例---------------------------------------------------------------
// 简单类型
private static final byte byteValue = 99;
private static final short shortValue = 9999;
private static final int intValue = 99999999;
private static final long longValue = 9999999999999999L;
private static final float floatValue = 99999999.9F;
private static final double doubleValue = 99999999.9D;
private static final char charValue = 'c';
private static final String charValueString = "c";
private static final String stringValue = "hello";
public static final byte byteValue = 99;
public static final short shortValue = 9999;
public static final int intValue = 99999999;
public static final long longValue = 9999999999999999L;
public static final float floatValue = 99999999.9F;
public static final double doubleValue = 99999999.9D;
public static final char charValue = 'c';
public static final String charValueString = "c";
public static final String stringValue = "hello";
// 数组类型
private static final boolean[] booleanArray = new boolean[]{true, false, true, false, true};
private static final byte[] byteArray = new byte[]{Byte.MIN_VALUE, -99, 0, 99, Byte.MAX_VALUE};
private static final short[] shortArray = new short[]{Short.MIN_VALUE, -99, 0, 99, Short.MAX_VALUE};
private static final int[] intArray = new int[]{Integer.MIN_VALUE, -99999999, -99, 0, 99, 99999999, Integer.MAX_VALUE};
private static final int[] intArray1 = new int[]{Integer.MIN_VALUE, -99999999, -99, 0, 99, 99999999, Integer.MAX_VALUE - 1};
private static final int[] intArray2 = new int[]{Integer.MIN_VALUE, -99999999, -99, 0, 99, 99999999, Integer.MAX_VALUE - 2};
private static final long[] longArray = new long[]{Long.MIN_VALUE, -9999999999999999L, -99999999L, -99L, 0L, 99L, 99999999L, 9999999999999999L, Long.MAX_VALUE};
private static final float[] floatArray = new float[]{Float.MIN_VALUE, -99999999.9F, -99.9F, 0F, 99.9F, 99999999.9F, Float.MAX_VALUE};
private static final double[] doubleArray = new double[]{Double.MIN_VALUE, -99999999.9F, -99.9D, 0D, 99.9D, 99999999.9F, Double.MAX_VALUE};
private static final char[] charArray = new char[]{'a', 'b', 'c', 'd', 'e'};
private static final String[] stringArray = new String[]{"a", "b", "c", "d", "e"};
public static final boolean[] booleanArray = new boolean[]{true, false, true, false, true};
public static final byte[] byteArray = new byte[]{Byte.MIN_VALUE, -99, 0, 99, Byte.MAX_VALUE};
public static final short[] shortArray = new short[]{Short.MIN_VALUE, -99, 0, 99, Short.MAX_VALUE};
public static final int[] intArray = new int[]{Integer.MIN_VALUE, -99999999, -99, 0, 99, 99999999, Integer.MAX_VALUE};
public static final int[] intArray1 = new int[]{Integer.MIN_VALUE, -99999999, -99, 0, 99, 99999999, Integer.MAX_VALUE - 1};
public static final int[] intArray2 = new int[]{Integer.MIN_VALUE, -99999999, -99, 0, 99, 99999999, Integer.MAX_VALUE - 2};
public static final long[] longArray = new long[]{Long.MIN_VALUE, -9999999999999999L, -99999999L, -99L, 0L, 99L, 99999999L, 9999999999999999L, Long.MAX_VALUE};
public static final float[] floatArray = new float[]{Float.MIN_VALUE, -99999999.9F, -99.9F, 0F, 99.9F, 99999999.9F, Float.MAX_VALUE};
public static final double[] doubleArray = new double[]{Double.MIN_VALUE, -99999999.9F, -99.9D, 0D, 99.9D, 99999999.9F, Double.MAX_VALUE};
public static final char[] charArray = new char[]{'a', 'b', 'c', 'd', 'e'};
public static final String[] stringArray = new String[]{"a", "b", "c", "d", "e"};
private static final ObjectA objectA = new ObjectA();
private static final ObjectB objectB = new ObjectB();
private static final Map<Integer, String> mapWithInteger = new HashMap<>(Map.of(Integer.MIN_VALUE, "a", -99, "b", 0, "c", 99, "d", Integer.MAX_VALUE, "e"));
private static final List<Integer> listWithInteger = new ArrayList<>(ArrayUtils.toList(intArray));
private static final List<Integer> listWithInteger1 = new ArrayList<>(ArrayUtils.toList(intArray1));
private static final List<Integer> listWithInteger2 = new ArrayList<>(ArrayUtils.toList(intArray2));
private static final List<ObjectA> listWithObject = new ArrayList<>(List.of(objectA, objectA, objectA));
private static final List<List<ObjectA>> listListWithObject = new ArrayList<>(List.of(listWithObject, listWithObject, listWithObject));
private static final List<List<Integer>> listListWithInteger = new ArrayList<>(List.of(listWithInteger, listWithInteger, listWithInteger));
private static final List<List<List<Integer>>> listListListWithInteger = new ArrayList<>(List.of(listListWithInteger, listListWithInteger, listListWithInteger));
private static final List<String> listWithString = new ArrayList<>(ArrayUtils.toList(stringArray));
private static final Set<Integer> setWithInteger = new HashSet<>(ArrayUtils.toList(intArray));
private static final Set<Set<List<Integer>>> setSetListWithInteger = new HashSet<>(Set.of(new HashSet<>(Set.of(listWithInteger)), new HashSet<>(Set.of(listWithInteger1)), new HashSet<>(Set.of(listWithInteger2))));
private static final Set<Set<ObjectA>> setSetWithObject = new HashSet<>(Set.of(new HashSet<>(Set.of(objectA))));
private static final Set<String> setWithString = new HashSet<>(ArrayUtils.toList(stringArray));
private static final Map<Integer, ObjectA> mapWithObject = new HashMap<>(Map.of(1, objectA, 2, objectA, 3, objectA));
private static final Map<ObjectA, List<Integer>> mapWithList = new HashMap<>(Map.of(objectA, listWithInteger));
private static final Map<List<List<ObjectA>>, List<List<List<Integer>>>> mapWithListList = new HashMap<>(Map.of(new ArrayList<>(List.of(listWithObject, listWithObject, listWithObject)), listListListWithInteger));
private static final List<Map<Integer, String>> listMap = new ArrayList<>(List.of(mapWithInteger, mapWithInteger, mapWithInteger));
private static final Set<Map<Integer, String>> setMapWithInteger = new HashSet<>(Set.of(mapWithInteger));
private static final Map<List<Map<Integer, String>>, Set<Map<Integer, String>>> mapListSet = new HashMap<>(Map.of(listMap, setMapWithInteger));
private static final Byte[] byteBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(byteArray), Byte.class);
private static final Short[] shortBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(shortArray), Short.class);
private static final Integer[] integerArray = ArrayUtils.listToArray(ArrayUtils.toList(intArray), Integer.class);
private static final Long[] longBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(longArray), Long.class);
private static final List<Long> listWithLong = ArrayUtils.toList(longArray);
private static final Float[] floatBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(floatArray), Float.class);
private static final List<Float> listWithFloat = ArrayUtils.toList(floatArray);
private static final Double[] doubleBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(doubleArray), Double.class);
private static final List<Double> listWithDouble = ArrayUtils.toList(doubleArray);
private static final Boolean[] booleanBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(booleanArray), Boolean.class);
private static final List<Boolean> listWithBoolean = ArrayUtils.toList(booleanArray);
private static final Character[] charBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(charArray), Character.class);
private static final ComplexObject complexObject = new ComplexObject();
private static final NormalObject normalObject = new NormalObject();
private static final SimpleObject simpleObject = new SimpleObject();
private static ProtobufObject.ProtobufComplexObject protobufComplexObject = null;
private static ProtobufObject.ProtobufNormalObject protobufNormalObject = null;
private static ProtobufObject.ProtobufSimpleObject protobufSimpleObject = null;
public static final ObjectA objectA = new ObjectA();
public static final ObjectB objectB = new ObjectB();
public static final Map<Integer, String> mapWithInteger = new HashMap<>(Map.of(Integer.MIN_VALUE, "a", -99, "b", 0, "c", 99, "d", Integer.MAX_VALUE, "e"));
public static final List<Integer> listWithInteger = new ArrayList<>(ArrayUtils.toList(intArray));
public static final List<Integer> listWithInteger1 = new ArrayList<>(ArrayUtils.toList(intArray1));
public static final List<Integer> listWithInteger2 = new ArrayList<>(ArrayUtils.toList(intArray2));
public static final List<ObjectA> listWithObject = new ArrayList<>(List.of(objectA, objectA, objectA));
public static final List<List<ObjectA>> listListWithObject = new ArrayList<>(List.of(listWithObject, listWithObject, listWithObject));
public static final List<List<Integer>> listListWithInteger = new ArrayList<>(List.of(listWithInteger, listWithInteger, listWithInteger));
public static final List<List<List<Integer>>> listListListWithInteger = new ArrayList<>(List.of(listListWithInteger, listListWithInteger, listListWithInteger));
public static final List<String> listWithString = new ArrayList<>(ArrayUtils.toList(stringArray));
public static final Set<Integer> setWithInteger = new HashSet<>(ArrayUtils.toList(intArray));
public static final Set<Set<List<Integer>>> setSetListWithInteger = new HashSet<>(Set.of(new HashSet<>(Set.of(listWithInteger)), new HashSet<>(Set.of(listWithInteger1)), new HashSet<>(Set.of(listWithInteger2))));
public static final Set<Set<ObjectA>> setSetWithObject = new HashSet<>(Set.of(new HashSet<>(Set.of(objectA))));
public static final Set<String> setWithString = new HashSet<>(ArrayUtils.toList(stringArray));
public static final Map<Integer, ObjectA> mapWithObject = new HashMap<>(Map.of(1, objectA, 2, objectA, 3, objectA));
public static final Map<ObjectA, List<Integer>> mapWithList = new HashMap<>(Map.of(objectA, listWithInteger));
public static final Map<List<List<ObjectA>>, List<List<List<Integer>>>> mapWithListList = new HashMap<>(Map.of(new ArrayList<>(List.of(listWithObject, listWithObject, listWithObject)), listListListWithInteger));
public static final List<Map<Integer, String>> listMap = new ArrayList<>(List.of(mapWithInteger, mapWithInteger, mapWithInteger));
public static final Set<Map<Integer, String>> setMapWithInteger = new HashSet<>(Set.of(mapWithInteger));
public static final Map<List<Map<Integer, String>>, Set<Map<Integer, String>>> mapListSet = new HashMap<>(Map.of(listMap, setMapWithInteger));
public static final Byte[] byteBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(byteArray), Byte.class);
public static final Short[] shortBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(shortArray), Short.class);
public static final Integer[] integerArray = ArrayUtils.listToArray(ArrayUtils.toList(intArray), Integer.class);
public static final Long[] longBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(longArray), Long.class);
public static final List<Long> listWithLong = ArrayUtils.toList(longArray);
public static final Float[] floatBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(floatArray), Float.class);
public static final List<Float> listWithFloat = ArrayUtils.toList(floatArray);
public static final Double[] doubleBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(doubleArray), Double.class);
public static final List<Double> listWithDouble = ArrayUtils.toList(doubleArray);
public static final Boolean[] booleanBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(booleanArray), Boolean.class);
public static final List<Boolean> listWithBoolean = ArrayUtils.toList(booleanArray);
public static final Character[] charBoxArray = ArrayUtils.listToArray(ArrayUtils.toList(charArray), Character.class);
public static final ComplexObject complexObject = new ComplexObject();
public static final NormalObject normalObject = new NormalObject();
public static final SimpleObject simpleObject = new SimpleObject();
public static ProtobufObject.ProtobufComplexObject protobufComplexObject = null;
public static ProtobufObject.ProtobufNormalObject protobufNormalObject = null;
public static ProtobufObject.ProtobufSimpleObject protobufSimpleObject = null;
static {
objectA.setA(Integer.MAX_VALUE);
File diff suppressed because it is too large Load Diff