test[protocol]: enhance test

This commit is contained in:
godotg
2023-09-03 11:27:04 +08:00
parent 58a9c186f6
commit 62e6184cbd
8 changed files with 109 additions and 59 deletions
+1
View File
@@ -1,6 +1,7 @@
#ifndef ZFOO_LIST_TEST_H
#define ZFOO_LIST_TEST_H
#include <cstdint>
#include "list.h"
namespace list_test {
@@ -45,6 +45,7 @@ import java.util.concurrent.Executors;
* @author godotg
* @version 3.0
*/
@Ignore
public class SpeedTest {
public static int benchmark = 10_0000;
@@ -59,7 +60,6 @@ public class SpeedTest {
* 常规对象,zfoo包体大小430kryo包体大小483protobuf包体大小793
* 复杂对象,zfoo包体大小2216kryo包体大小2528protobuf包体大小5091
*/
@Ignore
@Test
public void singleThreadBenchmarks() {
while (true) {
@@ -82,7 +82,6 @@ public class SpeedTest {
/**
* 多线程性能测试
*/
@Ignore
@Test
public void multipleThreadBenchmarks() throws InterruptedException {
while (true) {
@@ -102,7 +101,6 @@ public class SpeedTest {
}
}
@Ignore
@Test
public void zfooTest() {
// netty的ByteBuf做了更多的安全检测,java自带的ByteBuffer并没有做安全检测,为了公平,把不需要的检测去掉
@@ -146,7 +144,6 @@ public class SpeedTest {
System.out.println(StringUtils.format("[zfoo] [复杂对象] [thread:{}] [size:{}] [time:{}]", Thread.currentThread().getName(), buffer.writerIndex(), System.currentTimeMillis() - startTime));
}
@Ignore
@Test
public void furyTest() {
var buffer = MemoryBuffer.newHeapBuffer(1_0000);
@@ -183,7 +180,6 @@ public class SpeedTest {
System.out.println(StringUtils.format("[fury] [复杂对象] [thread:{}] [size:{}] [time:{}]", Thread.currentThread().getName(), buffer.writerIndex(), System.currentTimeMillis() - startTime));
}
@Ignore
@Test
public void kryoTest() {
try {
@@ -229,7 +225,6 @@ public class SpeedTest {
}
}
@Ignore
@Test
public void protobufTest() {
try {
@@ -273,7 +268,6 @@ public class SpeedTest {
}
}
@Ignore
@Test
public void zfooMultipleThreadTest() throws InterruptedException {
var countdown = new CountDownLatch(threadNum);
@@ -286,7 +280,6 @@ public class SpeedTest {
countdown.await();
}
@Ignore
@Test
public void furyMultipleThreadTest() throws InterruptedException {
var countdown = new CountDownLatch(threadNum);
@@ -299,7 +292,6 @@ public class SpeedTest {
countdown.await();
}
@Ignore
@Test
public void kryoMultipleThreadTest() throws InterruptedException {
var countdown = new CountDownLatch(threadNum);
@@ -312,7 +304,6 @@ public class SpeedTest {
countdown.await();
}
@Ignore
@Test
public void protobufMultipleThreadTest() throws InterruptedException {
var countdown = new CountDownLatch(threadNum);
@@ -383,7 +374,7 @@ public class SpeedTest {
// op.getGenerateLanguages().add(CodeLanguage.Protobuf);
// zfoo协议注册(其实就是:将Set里面的协议号和对应的类注册好,这样子就可以根据协议号知道是反序列化为哪个类)
ProtocolManager.initProtocolAuto(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class, VeryBigObject.class), op);
ProtocolManager.initProtocolAuto(Set.of(ComplexObject.class, NormalObject.class, SimpleObject.class, EmptyObject.class, VeryBigObject.class), op);
for (int i = 0; i < executors.length; i++) {
executors[i] = Executors.newSingleThreadExecutor();
@@ -686,24 +677,4 @@ public class SpeedTest {
}
/**
* 简单和复杂对象的序列化和反序列化测试,这个其实是基于ProtoManager.initProtocol初始化协议后执行的
*/
@Test
public void cmEnhanceMessTest() {
var buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 100, 1_0000);
// 简单对象序列化和反序列化测试
// 序列化:把normalObject序列化一下写到buffer中
ProtocolManager.write(buffer, normalObject);
// 反序列化:从buffer中反序列化为协议包
var packet = ProtocolManager.read(buffer);
buffer.clear();
// 复杂对象序列化和反序列化测试
ProtocolManager.write(buffer, complexObject);
packet = ProtocolManager.read(buffer);
buffer.clear();
}
}
@@ -0,0 +1,78 @@
/*
* 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.compatiblity;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.util.ClassUtils;
import com.zfoo.protocol.util.IOUtils;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.UnpooledHeapByteBuf;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import static com.zfoo.protocol.SpeedTest.complexObject;
import static com.zfoo.protocol.SpeedTest.normalObject;
/**
* @author godotg
*/
public class CompatiblityTest {
/**
* 字节码增强的Map变量顺序会出现不一样,所以序列化的内容顺序会改变,关闭字节码增强就会发现序列化内容相同
* @throws IOException
*/
@Ignore
@Test
public void compatiblityTest() throws IOException {
var bytes = IOUtils.toByteArray(ClassUtils.getFileFromClassPath("ComplexObject.bytes"));
var buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 100, 1_0000);
buffer.writeBytes(bytes);
var packet = ProtocolManager.read(buffer);
var newBuffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 100, 1_0000);
ProtocolManager.write(newBuffer, packet);
buffer.resetReaderIndex();
newBuffer.resetReaderIndex();
for (int i = 0; i < buffer.writerIndex(); i++) {
var a = buffer.readByte();
var b = newBuffer.readByte();
if (a != b) {
System.out.println("协议兼容错误" + i);
}
}
}
@Test
public void enhanceNormalTest() {
var buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 100, 1_0000);
// 简单对象序列化和反序列化测试
// 序列化:把normalObject序列化一下写到buffer中
ProtocolManager.write(buffer, normalObject);
// 反序列化:从buffer中反序列化为协议包
var packet = ProtocolManager.read(buffer);
buffer.clear();
// 复杂对象序列化和反序列化测试
ProtocolManager.write(buffer, complexObject);
packet = ProtocolManager.read(buffer);
buffer.clear();
}
}
@@ -20,10 +20,9 @@ import com.zfoo.protocol.registration.anno.Protocol;
import java.util.*;
@Protocol(note = "复杂的对象,包括了各种复杂的结构,数组,List,Set,Map")
@Protocol(id = 100, note = "复杂的对象,包括了各种复杂的结构,数组,List,Set,Map")
public class ComplexObject implements IPacket {
public static final transient short PROTOCOL_ID = 100;
@Note("byte类型,最简单的整形")
private byte a;
@@ -99,11 +98,6 @@ public class ComplexObject implements IPacket {
@Compatible(order = 2)
private ObjectA myObject;
@Override
public short protocolId() {
return PROTOCOL_ID;
}
public byte getA() {
return a;
}
@@ -0,0 +1,21 @@
/*
* 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.packet;
import com.zfoo.protocol.IPacket;
/**
* @author godotg
*/
public class EmptyObject implements IPacket {
}
@@ -14,6 +14,7 @@
package com.zfoo.protocol.packet;
import com.zfoo.protocol.IPacket;
import com.zfoo.protocol.registration.anno.Protocol;
import java.util.List;
import java.util.Map;
@@ -23,10 +24,9 @@ import java.util.Set;
* @author godotg
* @version 3.0
*/
@Protocol(id = 101)
public class NormalObject implements IPacket {
public static final transient short PROTOCOL_ID = 101;
private byte a;
private byte[] aaa;
@@ -58,11 +58,6 @@ public class NormalObject implements IPacket {
private Set<Integer> s;
private Set<String> ssss;
@Override
public short protocolId() {
return PROTOCOL_ID;
}
public byte getA() {
return a;
}
@@ -14,6 +14,7 @@
package com.zfoo.protocol.packet;
import com.zfoo.protocol.IPacket;
import com.zfoo.protocol.registration.anno.Protocol;
import java.util.Map;
import java.util.Objects;
@@ -22,21 +23,15 @@ import java.util.Objects;
* @author godotg
* @version 3.0
*/
@Protocol(id = 102)
public class ObjectA implements IPacket {
public static final transient short PROTOCOL_ID = 102;
private int a;
private Map<Integer, String> m;
private ObjectB objectB;
@Override
public short protocolId() {
return PROTOCOL_ID;
}
public int getA() {
return a;
}
@@ -14,6 +14,7 @@
package com.zfoo.protocol.packet;
import com.zfoo.protocol.IPacket;
import com.zfoo.protocol.registration.anno.Protocol;
import java.util.Objects;
@@ -21,17 +22,11 @@ import java.util.Objects;
* @author godotg
* @version 3.0
*/
@Protocol(id = 103)
public class ObjectB implements IPacket {
public static final transient short PROTOCOL_ID = 103;
private boolean flag;
@Override
public short protocolId() {
return PROTOCOL_ID;
}
public boolean isFlag() {
return flag;
}