test[protocol]: test

This commit is contained in:
godotg
2023-09-09 10:44:45 +08:00
parent c51c819987
commit b1f637ffad
21 changed files with 344 additions and 210 deletions
+3
View File
@@ -197,6 +197,9 @@
<configuration>
<threadCount>8</threadCount>
<argLine>-Dfile.encoding=${file.encoding}</argLine>
<includes>
<include>*/*Test.java</include>
</includes>
</configuration>
</plugin>
@@ -45,7 +45,7 @@ import java.util.concurrent.Executors;
* @version 3.0
*/
@Ignore
public class SpeedTest {
public class BenchmarkTesting {
public static int benchmark = 10_0000;
@@ -13,20 +13,11 @@
package com.zfoo.protocol.buffer;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.buffer.model.BigPacket;
import com.zfoo.protocol.util.StringUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledHeapByteBuf;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Arrays;
import java.util.Set;
/**
* @author godotg
* @version 3.0
@@ -123,69 +114,4 @@ public class ByteBufUtilsTest {
Assert.assertEquals(ByteBufUtils.readCharBox(byteBuf), Character.valueOf(Character.MIN_VALUE));
}
@Ignore
@Test
public void readLongSpeedTest() {
var startTime = System.currentTimeMillis();
long[] values = new long[]{Long.MIN_VALUE, -9999999999999999L, -9999999999999999L, -99999999, -9999, -100, -2, -1
, 0, 1, 2, 100, 9999, 99999999, 9999999999999999L, Long.MAX_VALUE};
ByteBuf buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 100, 1_0000);
for (int i = 0; i < Integer.MAX_VALUE; i++) {
buffer.clear();
for (long value : values) {
ByteBufUtils.writeLong(buffer, value);
ByteBufUtils.readLong(buffer);
}
}
System.out.println(System.currentTimeMillis() - startTime);
}
/**
* 测试所有int值,运行时间太长,放弃测试
*/
@Ignore
@Test
public void readIntTest() {
ByteBuf byteBuf = Unpooled.buffer();
for (int value = Integer.MIN_VALUE; value < Integer.MAX_VALUE; value++) {
byteBuf.clear();
ByteBufUtils.writeInt(byteBuf, value);
int result = ByteBufUtils.readInt(byteBuf);
Assert.assertEquals(result, value);
}
}
@Ignore
@Test
public void readLongTest() {
ByteBuf byteBuf = Unpooled.buffer();
for (long value = Long.MIN_VALUE; value < Long.MAX_VALUE; value++) {
byteBuf.clear();
ByteBufUtils.writeLong(byteBuf, value);
Assert.assertEquals(ByteBufUtils.readLong(byteBuf), value);
}
}
@Ignore
@Test
public void bigDataTest() {
ProtocolManager.initProtocol(Set.of(BigPacket.class));
var bigPact = new BigPacket();
Arrays.fill(bigPact.a, 99);
var buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 100, 100_0000);
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10_0000; i++) {
buffer.clear();
ProtocolManager.write(buffer, bigPact);
var newPacket = ProtocolManager.read(buffer);
}
System.out.println(StringUtils.format("[zfoo][size:{}] [time:{}]", buffer.writerIndex(), System.currentTimeMillis() - startTime));
}
}
@@ -0,0 +1,101 @@
/*
* Copyright (C) 2020 The zfoo Authors
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.protocol.buffer;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.buffer.model.BigPacket;
import com.zfoo.protocol.util.StringUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled;
import io.netty.buffer.UnpooledHeapByteBuf;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Arrays;
import java.util.Set;
/**
* @author godotg
* @version 3.0
*/
@Ignore
public class ByteBufUtilsTesting {
@Ignore
@Test
public void readLongSpeedTest() {
var startTime = System.currentTimeMillis();
long[] values = new long[]{Long.MIN_VALUE, -9999999999999999L, -9999999999999999L, -99999999, -9999, -100, -2, -1
, 0, 1, 2, 100, 9999, 99999999, 9999999999999999L, Long.MAX_VALUE};
ByteBuf buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 100, 1_0000);
for (int i = 0; i < Integer.MAX_VALUE; i++) {
buffer.clear();
for (long value : values) {
ByteBufUtils.writeLong(buffer, value);
ByteBufUtils.readLong(buffer);
}
}
System.out.println(System.currentTimeMillis() - startTime);
}
/**
* 测试所有int值,运行时间太长,放弃测试
*/
@Ignore
@Test
public void readIntTest() {
ByteBuf byteBuf = Unpooled.buffer();
for (int value = Integer.MIN_VALUE; value < Integer.MAX_VALUE; value++) {
byteBuf.clear();
ByteBufUtils.writeInt(byteBuf, value);
int result = ByteBufUtils.readInt(byteBuf);
Assert.assertEquals(result, value);
}
}
@Ignore
@Test
public void readLongTest() {
ByteBuf byteBuf = Unpooled.buffer();
for (long value = Long.MIN_VALUE; value < Long.MAX_VALUE; value++) {
byteBuf.clear();
ByteBufUtils.writeLong(byteBuf, value);
Assert.assertEquals(ByteBufUtils.readLong(byteBuf), value);
}
}
@Ignore
@Test
public void bigDataTest() {
ProtocolManager.initProtocol(Set.of(BigPacket.class));
var bigPact = new BigPacket();
Arrays.fill(bigPact.a, 99);
var buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 100, 100_0000);
long startTime = System.currentTimeMillis();
for (int i = 0; i < 10_0000; i++) {
buffer.clear();
ProtocolManager.write(buffer, bigPact);
var newPacket = ProtocolManager.read(buffer);
}
System.out.println(StringUtils.format("[zfoo][size:{}] [time:{}]", buffer.writerIndex(), System.currentTimeMillis() - startTime));
}
}
@@ -25,7 +25,7 @@ import java.util.concurrent.CountDownLatch;
* @version 3.0
*/
@Ignore
public class ConcurrentTest {
public class ConcurrentTesting {
private static final int EXECUTOR_SIZE = Runtime.getRuntime().availableProcessors();
@@ -28,7 +28,7 @@ import java.util.concurrent.atomic.AtomicInteger;
* @version 3.0
*/
@Ignore
public class ConcurrentFileChannelMapTest {
public class ConcurrentFileChannelMapTesting {
private static final int EXECUTOR_SIZE = Runtime.getRuntime().availableProcessors();
@@ -28,7 +28,7 @@ import java.util.concurrent.atomic.AtomicInteger;
* @version 3.0
*/
@Ignore
public class ConcurrentHeapMapTest {
public class ConcurrentHeapMapTesting {
private static final int EXECUTOR_SIZE = Runtime.getRuntime().availableProcessors();
@@ -26,7 +26,7 @@ import java.util.Set;
* @version 3.0
*/
@Ignore
public class FileChannelMapTest {
public class FileChannelMapTesting {
@Test
public void test() {
@@ -25,7 +25,7 @@ import java.util.Set;
* @version 3.0
*/
@Ignore
public class FileHeapMapTest {
public class FileHeapMapTesting {
@Test
public void test() {
@@ -22,7 +22,7 @@ import org.junit.Test;
* @version 3.0
*/
@Ignore
public class HeapMapTest {
public class HeapMapTesting {
@Test
public void test() {
@@ -26,13 +26,14 @@ import org.junit.Test;
import java.io.IOException;
import java.util.Set;
import static com.zfoo.protocol.SpeedTest.complexObject;
import static com.zfoo.protocol.SpeedTest.normalObject;
import static com.zfoo.protocol.BenchmarkTesting.complexObject;
import static com.zfoo.protocol.BenchmarkTesting.normalObject;
/**
* @author godotg
*/
public class CompatiblityTest {
@Ignore
public class CompatibilityTesting {
/**
* EN: The order of the bytecode-enhanced Map traversal order will be different, so the order of the serialized content will change.
@@ -40,7 +41,6 @@ public class CompatiblityTest {
* <p>
* CN: 字节码增强的Map遍历顺序会出现不一样所以序列化的内容顺序会改变可以看到不相同的字节并不是连续的
*/
@Ignore
@Test
public void compatiblityTest() throws IOException {
ProtocolManager.initProtocolAuto(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class, EmptyObject.class, VeryBigObject.class), GenerateOperation.NO_OPERATION);
@@ -26,7 +26,7 @@ import java.util.*;
* 细致比较性能
*/
@Ignore
public class FieldSpeedTest {
public class FieldSpeedTesting {
public static int benchmark = 100000;
public static IntObject intObject = new IntObject();
public static IntegerObject integerObject = new IntegerObject();
@@ -24,7 +24,7 @@ import java.util.Set;
* @author godotg
*/
@Ignore
public class GenerateTest {
public class GenerateTesting {
@Test
public void generateAllProtocols() {
@@ -20,101 +20,13 @@ import org.junit.Test;
import java.lang.reflect.Modifier;
import java.lang.reflect.*;
/*
测试javassist生成的类和普通用new关键字创建出来的类之间的区别<hr/>
测试发现两者从访问方法和访问变量的速度几乎没有什么区别,可以用javassist代理其它的类,不会影响速度<hr/>
*/
public class JavassistTest {
public static final int INTERVAL_TIME = 1000;
public static final int A_CONSTANT = 99999;
public static final int B_CONSTANT = 888888888;
public static final int C_CONSTANT = 7777777;
public static final int INT_CONSTANT = 999999;
/**
* 直接访问public的访问速度
*/
public void testA() {
A a = new A();
long start = System.currentTimeMillis();
long count = 0;
while (System.currentTimeMillis() - start <= INTERVAL_TIME) {
a.a = A_CONSTANT;
a.b = B_CONSTANT;
a.c = C_CONSTANT;
count++;
}
System.out.println(count);
}
/**
* 用get和set方法访问成员变量的速度
*/
public void testB() {
B b = new B();
long start = System.currentTimeMillis();
long count = 0;
while (System.currentTimeMillis() - start <= INTERVAL_TIME) {
b.setA(A_CONSTANT);
b.setB(B_CONSTANT);
b.setC(C_CONSTANT);
count++;
}
System.out.println(count);
}
/*
通过反射直接操作属性访问变量的速度
*/
public void testC() throws NoSuchFieldException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Field aField = C.class.getDeclaredField("a");
aField.setAccessible(true);
Field bField = C.class.getDeclaredField("b");
bField.setAccessible(true);
Field cField = C.class.getDeclaredField("c");
cField.setAccessible(true);
Method aSetMethod = C.class.getDeclaredMethod("setA", int.class);
aSetMethod.setAccessible(true);
Method bSetMethod = C.class.getDeclaredMethod("setB", int.class);
bSetMethod.setAccessible(true);
Method cSetMethod = C.class.getDeclaredMethod("setC", int.class);
cSetMethod.setAccessible(true);
Constructor<?> constructor = C.class.getDeclaredConstructor(null);
constructor.setAccessible(true);
C c = (C) constructor.newInstance(null);
long start = System.currentTimeMillis();
long count = 0;
while (System.currentTimeMillis() - start <= INTERVAL_TIME) {
//C c = C.class.newInstance();
aField.set(c, A_CONSTANT);
bField.set(c, B_CONSTANT);
cField.set(c, C_CONSTANT);
// aSetMethod.invoke(c, A_CONSTANT);
// bSetMethod.invoke(c, B_CONSTANT);
// cSetMethod.invoke(c, C_CONSTANT);
count++;
}
System.out.println(count);
}
/*
通过javassist访问成员变量的速度
*/
@Ignore
@Test
public void testD() throws NotFoundException, CannotCompileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
ClassPool classPool = ClassPool.getDefault();
@@ -182,38 +94,15 @@ public class JavassistTest {
ctClass.addMethod(cSetMethod);
Class<?> clazz = ctClass.toClass(IDGet.class);
Constructor<?> constructor = clazz.getConstructor(null);
IDGet d = (IDGet) constructor.newInstance(null);
Constructor<?> constructor = clazz.getConstructor();
IDGet d = (IDGet) constructor.newInstance();
long start = System.currentTimeMillis();
long count = 0;
while (System.currentTimeMillis() - start <= INTERVAL_TIME) {
d.setA(A_CONSTANT);
d.setB(B_CONSTANT);
d.setC(C_CONSTANT);
count++;
}
System.out.println(count);
// d.setA(A_CONSTANT);
// System.out.println(d.getA());
// d.setB(B_CONSTANT);
// System.out.println(d.getB());
// d.setC(C_CONSTANT);
// System.out.println(d.getC());
d.setA(A_CONSTANT);
d.setB(B_CONSTANT);
d.setC(C_CONSTANT);
}
@Ignore
@Test
public void testAll() throws NoSuchMethodException, IllegalAccessException, InstantiationException, CannotCompileException, NotFoundException, InvocationTargetException, NoSuchFieldException {
testA();
testB();
testC();
testD();
}
}
@@ -0,0 +1,215 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.protocol.javassist;
import javassist.*;
import org.junit.Ignore;
import org.junit.Test;
import java.lang.reflect.Modifier;
import java.lang.reflect.*;
/*
测试javassist生成的类和普通用new关键字创建出来的类之间的区别<hr/>
测试发现两者从访问方法和访问变量的速度几乎没有什么区别,可以用javassist代理其它的类,不会影响速度<hr/>
*/
@Ignore
public class JavassistTesting {
public static final int INTERVAL_TIME = 1000;
public static final int A_CONSTANT = 99999;
public static final int B_CONSTANT = 888888888;
public static final int C_CONSTANT = 7777777;
/**
* 直接访问public的访问速度
*/
public void testA() {
A a = new A();
long start = System.currentTimeMillis();
long count = 0;
while (System.currentTimeMillis() - start <= INTERVAL_TIME) {
a.a = A_CONSTANT;
a.b = B_CONSTANT;
a.c = C_CONSTANT;
count++;
}
System.out.println(count);
}
/**
* 用get和set方法访问成员变量的速度
*/
public void testB() {
B b = new B();
long start = System.currentTimeMillis();
long count = 0;
while (System.currentTimeMillis() - start <= INTERVAL_TIME) {
b.setA(A_CONSTANT);
b.setB(B_CONSTANT);
b.setC(C_CONSTANT);
count++;
}
System.out.println(count);
}
/*
通过反射直接操作属性访问变量的速度
*/
public void testC() throws NoSuchFieldException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Field aField = C.class.getDeclaredField("a");
aField.setAccessible(true);
Field bField = C.class.getDeclaredField("b");
bField.setAccessible(true);
Field cField = C.class.getDeclaredField("c");
cField.setAccessible(true);
Method aSetMethod = C.class.getDeclaredMethod("setA", int.class);
aSetMethod.setAccessible(true);
Method bSetMethod = C.class.getDeclaredMethod("setB", int.class);
bSetMethod.setAccessible(true);
Method cSetMethod = C.class.getDeclaredMethod("setC", int.class);
cSetMethod.setAccessible(true);
Constructor<?> constructor = C.class.getDeclaredConstructor();
constructor.setAccessible(true);
C c = (C) constructor.newInstance();
long start = System.currentTimeMillis();
long count = 0;
while (System.currentTimeMillis() - start <= INTERVAL_TIME) {
//C c = C.class.newInstance();
aField.set(c, A_CONSTANT);
bField.set(c, B_CONSTANT);
cField.set(c, C_CONSTANT);
// aSetMethod.invoke(c, A_CONSTANT);
// bSetMethod.invoke(c, B_CONSTANT);
// cSetMethod.invoke(c, C_CONSTANT);
count++;
}
System.out.println(count);
}
/*
通过javassist访问成员变量的速度
*/
@Test
public void testD() throws NotFoundException, CannotCompileException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
ClassPool classPool = ClassPool.getDefault();
CtClass ctClass = classPool.makeClass("com.zfoo.protocol.javassist.DGetClass");
ctClass.addInterface(classPool.get(IDGet.class.getCanonicalName()));
CtField a = new CtField(classPool.get(int.class.getCanonicalName()), "a", ctClass);
a.setModifiers(Modifier.PRIVATE);
ctClass.addField(a);
CtField b = new CtField(classPool.get(int.class.getCanonicalName()), "b", ctClass);
b.setModifiers(Modifier.PRIVATE);
ctClass.addField(b);
CtField c = new CtField(classPool.get(int.class.getCanonicalName()), "c", ctClass);
c.setModifiers(Modifier.PRIVATE);
ctClass.addField(c);
CtConstructor ctConstructor = new CtConstructor(null, ctClass);
ctConstructor.setBody("{}");
ctClass.addConstructor(ctConstructor);
CtMethod aGetMethod = new CtMethod(classPool.get(int.class.getCanonicalName()), "getA", null, ctClass);
aGetMethod.setModifiers(Modifier.PUBLIC);
StringBuilder aGetBuilder = new StringBuilder();
aGetBuilder.append("{return this.a;}");
aGetMethod.setBody(aGetBuilder.toString());
ctClass.addMethod(aGetMethod);
CtMethod aSetMethod = new CtMethod(classPool.get(void.class.getCanonicalName()), "setA", classPool.get(new String[]{int.class.getCanonicalName()}), ctClass);
aGetMethod.setModifiers(Modifier.PUBLIC);
StringBuilder aSetBuilder = new StringBuilder();
aSetBuilder.append("{this.a=$1;}");
aSetMethod.setBody(aSetBuilder.toString());
ctClass.addMethod(aSetMethod);
CtMethod bGetMethod = new CtMethod(classPool.get(int.class.getCanonicalName()), "getB", null, ctClass);
bGetMethod.setModifiers(Modifier.PUBLIC);
StringBuilder bGetBuilder = new StringBuilder();
bGetBuilder.append("{return this.b;}");
bGetMethod.setBody(bGetBuilder.toString());
ctClass.addMethod(bGetMethod);
CtMethod bSetMethod = new CtMethod(classPool.get(void.class.getCanonicalName()), "setB", classPool.get(new String[]{int.class.getCanonicalName()}), ctClass);
aGetMethod.setModifiers(Modifier.PUBLIC);
StringBuilder bSetBuilder = new StringBuilder();
bSetBuilder.append("{this.b=$1;}");
bSetMethod.setBody(bSetBuilder.toString());
ctClass.addMethod(bSetMethod);
CtMethod cGetMethod = new CtMethod(classPool.get(int.class.getCanonicalName()), "getC", null, ctClass);
cGetMethod.setModifiers(Modifier.PUBLIC);
StringBuilder cGetBuilder = new StringBuilder();
cGetBuilder.append("{return this.c;}");
cGetMethod.setBody(cGetBuilder.toString());
ctClass.addMethod(cGetMethod);
CtMethod cSetMethod = new CtMethod(classPool.get(void.class.getCanonicalName()), "setC", classPool.get(new String[]{int.class.getCanonicalName()}), ctClass);
aGetMethod.setModifiers(Modifier.PUBLIC);
StringBuilder cSetBuilder = new StringBuilder();
cSetBuilder.append("{this.c=$1;}");
cSetMethod.setBody(cSetBuilder.toString());
ctClass.addMethod(cSetMethod);
Class<?> clazz = ctClass.toClass(IDGet.class);
Constructor<?> constructor = clazz.getConstructor();
IDGet d = (IDGet) constructor.newInstance();
long start = System.currentTimeMillis();
long count = 0;
while (System.currentTimeMillis() - start <= INTERVAL_TIME) {
d.setA(A_CONSTANT);
d.setB(B_CONSTANT);
d.setC(C_CONSTANT);
count++;
}
System.out.println(count);
// d.setA(A_CONSTANT);
// System.out.println(d.getA());
// d.setB(B_CONSTANT);
// System.out.println(d.getB());
// d.setC(C_CONSTANT);
// System.out.println(d.getC());
}
@Test
public void testAll() throws NoSuchMethodException, IllegalAccessException, InstantiationException, CannotCompileException, NotFoundException, InvocationTargetException, NoSuchFieldException {
testA();
testB();
testC();
testD();
}
}
@@ -31,7 +31,7 @@ import java.util.Set;
* @version 3.0
*/
@Ignore
public class JProtobufTest {
public class JProtobufTesting {
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"));
@@ -26,7 +26,7 @@ import java.io.IOException;
* @version 3.0
*/
@Ignore
public class ProtobufTagTest {
public class ProtobufTagTesting {
@Test
public void test() throws IOException {
@@ -27,7 +27,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.zfoo.protocol.SpeedTest.*;
import static com.zfoo.protocol.BenchmarkTesting.*;
/**
* 主要来测试极端大的对象序列化和反序列化情况,极端大的对象指的是字段多,对象大,方法大
@@ -24,11 +24,11 @@ import java.io.IOException;
* @version 3.0
*/
@Ignore
public class ClassUtilsTest {
public class ClassUtilsTesting {
@Test
public void getClassPath() {
System.out.println(ClassUtils.getClassAbsPath(ClassUtilsTest.class));
System.out.println(ClassUtils.getClassAbsPath(ClassUtilsTesting.class));
}
@Test
@@ -25,7 +25,7 @@ import java.util.List;
* @version 3.0
*/
@Ignore
public class FileUtilTest {
public class FileUtilTesting {
@Test
public void absPathTest() {
@@ -29,7 +29,7 @@ import java.util.function.Predicate;
@Ignore
public class ReflectUtilTest {
public class ReflectUtilTesting {
@Test
public void testGetFieldsByAnnotation() {