This commit is contained in:
大屌哥
2023-03-05 23:56:35 +08:00
34 changed files with 15948 additions and 2303 deletions
+31
View File
@@ -0,0 +1,31 @@
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: Java CI with Maven
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 11
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B package --file pom.xml
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -11,7 +10,7 @@
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.monitor.model;
package com.zfoo.monitor;
import com.zfoo.monitor.util.OSUtils;
import com.zfoo.protocol.util.IOUtils;
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -11,7 +10,7 @@
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.monitor.model;
package com.zfoo.monitor;
/**
* @author godotg
@@ -1,4 +1,16 @@
package com.zfoo.monitor.model;
/*
* 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.monitor;
import com.zfoo.protocol.util.IOUtils;
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -11,7 +10,7 @@
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.monitor.model;
package com.zfoo.monitor;
import com.zfoo.monitor.util.OSUtils;
import com.zfoo.protocol.util.IOUtils;
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -11,7 +10,7 @@
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.monitor.model;
package com.zfoo.monitor;
import com.zfoo.monitor.util.OSUtils;
import com.zfoo.protocol.collection.CollectionUtils;
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -11,7 +10,7 @@
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.monitor.model;
package com.zfoo.monitor;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.scheduler.util.TimeUtils;
@@ -1,4 +1,16 @@
package com.zfoo.monitor.model;
/*
* 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.monitor;
public class SystemInfo {
@@ -1,6 +1,5 @@
/*
* 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
*
@@ -11,7 +10,7 @@
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.zfoo.monitor.model;
package com.zfoo.monitor;
import com.zfoo.monitor.util.OSUtils;
import com.zfoo.protocol.util.StringUtils;
@@ -1,6 +1,6 @@
package com.zfoo.monitor.util;
import com.zfoo.monitor.model.JvmMemory;
import com.zfoo.monitor.JvmMemory;
import java.lang.management.*;
import java.util.ArrayList;
@@ -13,7 +13,7 @@
package com.zfoo.monitor.util;
import com.zfoo.monitor.model.*;
import com.zfoo.monitor.*;
import com.zfoo.protocol.util.IOUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.scheduler.util.TimeUtils;
@@ -33,7 +33,9 @@ import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Task线程池一半是用来接收客户都安的请求做一些cpu密集型任务,尽量避免做一些阻塞操作,IO密集型任务可以放在Event线程池去做
* EN: The Task thread pool is generally used to process customer requests, do some CPU-intensive tasks, and try to avoid some blocking operations;
* IO-intensive tasks can be executed in the Event thread pool
* CN: Task线程池一般是用来处理客户的请求,做一些cpu密集型任务,尽量避免做一些阻塞操作;IO密集型任务可以放在Event线程池去做
*
* @author godotg
* @version 3.0
@@ -42,11 +44,13 @@ public final class TaskBus {
private static final Logger logger = LoggerFactory.getLogger(TaskBus.class);
// 线程池的大小,也可以通过provider thread配置指定
// EN: The size of the thread pool can also be specified through the provider thread configuration
// CN: 线程池的大小,也可以通过provider thread配置指定
public static final int EXECUTOR_SIZE;
/**
* 使用不同的线程池,让线程池之间实现隔离,互不影响
* EN: Use different thread pools to achieve isolation between thread pools without affecting each other
* CN: 使用不同的线程池,让线程池之间实现隔离,互不影响
*/
private static final ExecutorService[] executors;
@@ -117,7 +121,7 @@ public final class TaskBus {
if (uid <= 0) {
execute((int) session.getSid(), task);
} else {
execute(uid, task);
execute((int) uid, task);
}
} else {
execute(attachment.taskExecutorHash(), task);
@@ -92,7 +92,7 @@ public class MongodbAccessor implements IAccessor {
var result = collection.bulkWrite(batchList, new BulkWriteOptions().ordered(false));
if (result.getModifiedCount() != entities.size()) {
logger.error("在数据库[{}]的批量更新操作中需要更新的数量[{}]和最终更新的数量[{}]不相同"
logger.warn("在数据库[{}]的批量更新操作中需要更新的数量[{}]和最终更新的数量[{}]不相同(大部分原因都是因为需要更新的文档和数据库的文档相同)"
, entityClazz.getSimpleName(), entities.size(), result.getModifiedCount());
}
} catch (Throwable t) {
+3 -3
View File
@@ -151,9 +151,9 @@ public class EntityCaches<PK extends Comparable<PK>, E extends IEntity<PK>> impl
} else {
var pnodeThread = ThreadUtils.findThread(pnodeThreadId);
if (pnodeThread == null) {
logger.warn("[{}][id:{}]有并发写风险,第一次更新的线程[id:{}],第2次更新的线程[id:{}]", entity.getClass().getSimpleName(), entity.id(), pnodeThreadId, currentThreadId);
logger.warn("[{}][id:{}]有并发写风险,第一次更新的线程[threadId:{}],第2次更新的线程[threadId:{}]", entity.getClass().getSimpleName(), entity.id(), pnodeThreadId, currentThreadId);
} else {
logger.warn("[{}][id:{}]有并发写风险,第一次更新的线程[id:{}][name:{}],第2次更新的线程[id:{}][name:{}]"
logger.warn("[{}][id:{}]有并发写风险,第一次更新的线程[threadId:{}][threadName:{}],第2次更新的线程[threadId:{}][threadName:{}]"
, entity.getClass().getSimpleName(), entity.id(), pnodeThreadId, pnodeThread.getName(), currentThreadId, Thread.currentThread().getName());
}
}
@@ -229,7 +229,7 @@ public class EntityCaches<PK extends Comparable<PK>, E extends IEntity<PK>> impl
continue;
}
logger.error("在数据库[{}]的批量更新操作中需要更新的数量[{}]和最终更新的数量[{}]不相同,开始执行容错操作"
logger.warn("在数据库[{}]的批量更新操作中需要更新的数量[{}]和最终更新的数量[{}]不相同,开始执行容错操作(大部分原因都是因为需要更新的文档和数据库的文档相同)"
, entityDef.getClazz().getSimpleName(), currentUpdateList.size(), result.getModifiedCount());
persistAllAndCompare(currentUpdateList);
} catch (Throwable t) {
@@ -120,11 +120,13 @@ public abstract class GenerateGdUtils {
var includeSubProtocol = includeSubProtocol(registration);
var classNote = GenerateProtocolNote.classNote(protocolId, CodeLanguage.GdScript);
var fieldDefinition = fieldDefinition(registration);
var toStringMethod = toStringMethod(registration);
var writeObject = writeObject(registration);
var readObject = readObject(registration);
var protocolTemplate = StringUtils.bytesToString(IOUtils.toByteArray(ClassUtils.getFileFromClassPath("gdscript/ProtocolTemplate.gd")));
protocolTemplate = StringUtils.format(protocolTemplate, protocolId, includeSubProtocol, classNote, fieldDefinition.trim(), protocolClazzName, writeObject.trim(), readObject.trim());
protocolTemplate = StringUtils.format(protocolTemplate, protocolId, protocolClazzName, includeSubProtocol, classNote, fieldDefinition.trim(),
StringUtils.EMPTY_JSON, toStringMethod, writeObject.trim(), readObject.trim());
var outputPath = StringUtils.format("{}/{}/{}.gd", protocolOutputPath, GenerateProtocolPath.getProtocolPath(protocolId), protocolClazzName);
FileUtils.writeStringToFile(new File(outputPath), protocolTemplate, true);
@@ -169,6 +171,23 @@ public abstract class GenerateGdUtils {
return gdBuilder.toString();
}
private static String toStringMethod(ProtocolRegistration registration) {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
var gdBuilder = new StringBuilder();
var sequencedFields = ReflectionUtils.notStaticAndTransientFields(registration.getConstructor().getDeclaringClass());
for (int i = 0; i < sequencedFields.size(); i++) {
var field = sequencedFields.get(i);
IFieldRegistration fieldRegistration = fieldRegistrations[GenerateProtocolFile.indexOf(fields, field)];
var fieldName = field.getName();
gdBuilder.append(TAB_ASCII).append(StringUtils.format("map[\"{}\"] = {}", fieldName, fieldName));
if (i != sequencedFields.size() - 1) {
gdBuilder.append(LS);
}
}
return gdBuilder.toString();
}
private static String writeObject(ProtocolRegistration registration) {
var fields = registration.getFields();
var fieldRegistrations = registration.getFieldRegistrations();
@@ -1,10 +1,16 @@
const PROTOCOL_ID = {}
const PROTOCOL_CLASS_NAME = "{}"
{}
{}
{}
func get_class() -> String:
return "{}"
func map() -> Dictionary:
var map = {}
{}
return map
func _to_string() -> String:
return JSON.stringify(map())
static func write(buffer, packet):
if (buffer.writePacketFlag(packet)):
@@ -428,12 +428,20 @@ func writePacketArray(array, protocolId):
protocolRegistration.write(self, element)
func readPacketArray(protocolId):
var array = []
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
var array = Array([], typeof(protocolRegistration), StringName("RefCounted"), protocolRegistration)
var size = readInt()
if (size > 0):
var protocolRegistration = ProtocolManager.getProtocol(protocolId)
for index in range(size):
array.append(protocolRegistration.read(self))
#var a = array.get_typed_class_name()
#var b = array.get_typed_script()
#var c = array.get_typed_builtin()
#var typeArray: Array[ObjectA] = []
#var aa = typeArray.get_typed_class_name()
#var bb = typeArray.get_typed_script()
#var cc = typeArray.get_typed_builtin()
return array
func writeIntIntMap(map):
@@ -206,7 +206,7 @@ public class SpeedTest {
protobufNormalObject.writeTo(codedOutputStream);
length = codedOutputStream.getTotalBytesWritten();
var codeInput = CodedInputStream.newInstance(buffer, 0, length);
var mess = ProtobufObject.ProtobufSimpleObject.parseFrom(codeInput);
var mess = ProtobufObject.ProtobufNormalObject.parseFrom(codeInput);
}
System.out.println(StringUtils.format("[protobuf] [常规对象] [thread:{}] [size:{}] [time:{}]", Thread.currentThread().getName(), length, System.currentTimeMillis() - startTime));
@@ -217,7 +217,7 @@ public class SpeedTest {
protobufComplexObject.writeTo(codedOutputStream);
length = codedOutputStream.getTotalBytesWritten();
var codeInput = CodedInputStream.newInstance(buffer, 0, length);
var mess = ProtobufObject.ProtobufSimpleObject.parseFrom(codeInput);
var mess = ProtobufObject.ProtobufComplexObject.parseFrom(codeInput);
}
System.out.println(StringUtils.format("[protobuf] [复杂对象] [thread:{}] [size:{}] [time:{}]", Thread.currentThread().getName(), length, System.currentTimeMillis() - startTime));
} catch (Exception e) {
@@ -3,255 +3,612 @@ package com.zfoo.protocol.field;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.google.protobuf.ByteString;
import com.google.protobuf.CodedInputStream;
import com.google.protobuf.CodedOutputStream;
import com.zfoo.protocol.ProtocolManager;
import com.zfoo.protocol.field.packet.FieldProtobufObject;
import com.zfoo.protocol.field.packet.FloatObject;
import com.zfoo.protocol.field.packet.IntObject;
import com.zfoo.protocol.field.packet.IntegerObject;
import com.zfoo.protocol.collection.ArrayUtils;
import com.zfoo.protocol.field.packet.*;
import com.zfoo.protocol.generate.GenerateOperation;
import com.zfoo.protocol.util.StringUtils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.UnpooledHeapByteBuf;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
/**
* 测试各种不同特征的数据类型的序列化和反序列化时间
* 细致比较性能
*/
@Ignore
public class FieldSpeedTest {
public static int benchmark = 100000;
public static IntObject intObject=new IntObject();
public static IntegerObject integerObject=new IntegerObject();
public static FloatObject floatObject=new FloatObject();
public static IntObject intObject = new IntObject();
public static IntegerObject integerObject = new IntegerObject();
public static FloatObject floatObject = new FloatObject();
public static BytesObject bytesObject = new BytesObject();
public static StringObject stringObject = new StringObject();
public static ListIntegerObject listIntegerObject = new ListIntegerObject();
public static SetObject setObject = new SetObject();
public static MapObject mapObject = new MapObject();
public static InnerObjectObject innerObjectObject = new InnerObjectObject();
public static FieldProtobufObject.IntObject protobufIntObject = null;
public static FieldProtobufObject.FloatObject protobufFloatObject = null;
public static FieldProtobufObject.BytesObject protobufBytesObject = null;
public static FieldProtobufObject.StringObject protobufStringObject = null;
public static FieldProtobufObject.ListIntegerObject protobufListIntegerObject = null;
public static FieldProtobufObject.MapObject protobufMapObject = null;
public static FieldProtobufObject.InnerObjectObject protobufInnerObjectObject = null;
public static long zfooSerializationTime;
public static long zfooDeserializationTime;
public static long kryoSerializationTime;
public static long kryoDeserializationTime;
public static long protobufSerializationTime;
public static long protobufDeserializationTime;
public static long zfooSerializationSize;
public static long kryoSerializationSize;
public static long protobufSerializationSize;
@Test
public void testIntObject() throws IOException {
ByteBuf buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 10000_0000, 100000000);
benchmark=benchmark*10;
benchmark = benchmark * 10;
long startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
ProtocolManager.write(buffer,intObject);
for (int i = 0; i < benchmark; i++) {
ProtocolManager.write(buffer, intObject);
}
zfooSerializationTime=System.currentTimeMillis() - startTime;
zfooSerializationTime = System.currentTimeMillis() - startTime;
zfooSerializationSize = buffer.writerIndex();
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
var packet = ProtocolManager.read(buffer);
}
zfooDeserializationTime=System.currentTimeMillis() - startTime;
zfooDeserializationTime = System.currentTimeMillis() - startTime;
buffer.release();
var kryo = kryos.get();
var output = new Output(10000_0000);
var input = new Input(output.getBuffer());
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
kryo.writeObject(output, intObject);
}
kryoSerializationTime=System.currentTimeMillis() - startTime;
kryoSerializationTime = System.currentTimeMillis() - startTime;
kryoSerializationSize = output.position();
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
var mess = kryo.readObject(input, IntObject.class);
}
kryoDeserializationTime=System.currentTimeMillis() - startTime;
kryoDeserializationTime = System.currentTimeMillis() - startTime;
output.close();
var protobufBuff=new byte[10000_0000];
var protobufBuff = new byte[10000_0000];
var codedOutputStream = CodedOutputStream.newInstance(protobufBuff);
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
protobufIntObject.writeTo(codedOutputStream);
}
protobufSerializationTime=System.currentTimeMillis() - startTime;
var length=codedOutputStream.getTotalBytesWritten();
var singleLength = length/benchmark;
var l=0;
protobufSerializationTime = System.currentTimeMillis() - startTime;
var length = codedOutputStream.getTotalBytesWritten();
protobufSerializationSize = length;
var singleLength = length / benchmark;
var l = 0;
//在循环中去掉创建CodedInputStream的代码保证比较耗时公平性
var codeInputs=new CodedInputStream[benchmark];
for(int i=0;i<benchmark;i++)
{
codeInputs[i]=CodedInputStream.newInstance(protobufBuff, l+singleLength, singleLength);
var codeInputs = new CodedInputStream[benchmark];
for (int i = 0; i < benchmark; i++) {
codeInputs[i] = CodedInputStream.newInstance(protobufBuff, l + singleLength, singleLength);
}
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
var mess = FieldProtobufObject.IntObject.parseFrom(codeInputs[i]);
}
protobufDeserializationTime=System.currentTimeMillis() - startTime;
System.out.println(StringUtils.format("序列化 [zfoo] [int类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), zfooSerializationTime));
System.out.println(StringUtils.format("序列化 [kryo] [int类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), kryoSerializationTime));
System.out.println(StringUtils.format("序列化 [protobuf] [int类型] [thread:{}] [time:{}]", Thread.currentThread().getName(),protobufSerializationTime));
protobufDeserializationTime = System.currentTimeMillis() - startTime;
System.out.println(StringUtils.format("序列化 [zfoo] [int类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), zfooSerializationTime, zfooSerializationSize));
System.out.println(StringUtils.format("序列化 [kryo] [int类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), kryoSerializationTime, kryoSerializationSize));
System.out.println(StringUtils.format("序列化 [protobuf] [int类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), protobufSerializationTime, protobufSerializationSize));
System.out.println(StringUtils.format("反序列化 [zfoo] [int类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), zfooDeserializationTime));
System.out.println(StringUtils.format("反序列化 [kryo] [int类型] [thread:{}] [time:{}]", Thread.currentThread().getName(),kryoDeserializationTime));
System.out.println(StringUtils.format("反序列化 [kryo] [int类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), kryoDeserializationTime));
System.out.println(StringUtils.format("反序列化 [protobuf] [int类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), protobufDeserializationTime));
}
@Test
public void testIntegerObject() throws IOException {
ByteBuf buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 10000_0000, 100000000);
benchmark=benchmark*10;
benchmark = benchmark * 10;
long startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
ProtocolManager.write(buffer,integerObject);
for (int i = 0; i < benchmark; i++) {
ProtocolManager.write(buffer, integerObject);
}
zfooSerializationTime=System.currentTimeMillis() - startTime;
zfooSerializationTime = System.currentTimeMillis() - startTime;
zfooSerializationSize = buffer.writerIndex();
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
var packet = ProtocolManager.read(buffer);
}
zfooDeserializationTime=System.currentTimeMillis() - startTime;
zfooDeserializationTime = System.currentTimeMillis() - startTime;
buffer.release();
var kryo = kryos.get();
var output = new Output(10000_0000);
var input = new Input(output.getBuffer());
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
kryo.writeObject(output, integerObject);
}
kryoSerializationTime=System.currentTimeMillis() - startTime;
kryoSerializationTime = System.currentTimeMillis() - startTime;
kryoSerializationSize = output.position();
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
var mess = kryo.readObject(input, IntegerObject.class);
}
kryoDeserializationTime=System.currentTimeMillis() - startTime;
kryoDeserializationTime = System.currentTimeMillis() - startTime;
output.close();
var protobufBuff=new byte[10000_0000];
var protobufBuff = new byte[10000_0000];
var codedOutputStream = CodedOutputStream.newInstance(protobufBuff);
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
protobufIntObject.writeTo(codedOutputStream);
}
protobufSerializationTime=System.currentTimeMillis() - startTime;
var length=codedOutputStream.getTotalBytesWritten();
var singleLength = length/benchmark;
var l=0;
protobufSerializationTime = System.currentTimeMillis() - startTime;
var length = codedOutputStream.getTotalBytesWritten();
protobufSerializationSize = length;
var singleLength = length / benchmark;
var l = 0;
//在循环中去掉创建CodedInputStream的代码保证比较耗时公平性
var codeInputs=new CodedInputStream[benchmark];
for(int i=0;i<benchmark;i++)
{
codeInputs[i]=CodedInputStream.newInstance(protobufBuff, l+singleLength, singleLength);
var codeInputs = new CodedInputStream[benchmark];
for (int i = 0; i < benchmark; i++) {
codeInputs[i] = CodedInputStream.newInstance(protobufBuff, l + singleLength, singleLength);
}
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
var mess = FieldProtobufObject.IntObject.parseFrom(codeInputs[i]);
}
protobufDeserializationTime=System.currentTimeMillis() - startTime;
System.out.println(StringUtils.format("序列化 [zfoo] [Integer类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), zfooSerializationTime));
System.out.println(StringUtils.format("序列化 [kryo] [Integer类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), kryoSerializationTime));
System.out.println(StringUtils.format("序列化 [protobuf] [int类型] [thread:{}] [time:{}]", Thread.currentThread().getName(),protobufSerializationTime));
protobufDeserializationTime = System.currentTimeMillis() - startTime;
System.out.println(StringUtils.format("序列化 [zfoo] [Integer类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), zfooSerializationTime, zfooSerializationSize));
System.out.println(StringUtils.format("序列化 [kryo] [Integer类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), kryoSerializationTime, kryoSerializationSize));
System.out.println(StringUtils.format("序列化 [protobuf] [int类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), protobufSerializationTime, protobufSerializationSize));
System.out.println(StringUtils.format("反序列化 [zfoo] [Integer类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), zfooDeserializationTime));
System.out.println(StringUtils.format("反序列化 [kryo] [Integer类型] [thread:{}] [time:{}]", Thread.currentThread().getName(),kryoDeserializationTime));
System.out.println(StringUtils.format("反序列化 [kryo] [Integer类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), kryoDeserializationTime));
System.out.println(StringUtils.format("反序列化 [protobuf] [int类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), protobufDeserializationTime));
}
@Test
public void testFloatObject() throws IOException {
ByteBuf buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 10000_0000, 100000000);
benchmark=benchmark*10;
benchmark = benchmark * 10;
long startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
ProtocolManager.write(buffer,floatObject);
for (int i = 0; i < benchmark; i++) {
ProtocolManager.write(buffer, floatObject);
}
zfooSerializationTime=System.currentTimeMillis() - startTime;
zfooSerializationTime = System.currentTimeMillis() - startTime;
zfooSerializationSize = buffer.writerIndex();
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
var packet = ProtocolManager.read(buffer);
}
zfooDeserializationTime=System.currentTimeMillis() - startTime;
zfooDeserializationTime = System.currentTimeMillis() - startTime;
buffer.release();
var kryo = kryos.get();
var output = new Output(10000_0000);
var input = new Input(output.getBuffer());
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
kryo.writeObject(output, floatObject);
}
kryoSerializationTime=System.currentTimeMillis() - startTime;
kryoSerializationTime = System.currentTimeMillis() - startTime;
kryoSerializationSize = output.position();
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
var mess = kryo.readObject(input, FloatObject.class);
}
kryoDeserializationTime=System.currentTimeMillis() - startTime;
kryoDeserializationTime = System.currentTimeMillis() - startTime;
output.close();
var protobufBuff=new byte[10000_0000];
var protobufBuff = new byte[10000_0000];
var codedOutputStream = CodedOutputStream.newInstance(protobufBuff);
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
protobufFloatObject.writeTo(codedOutputStream);
}
protobufSerializationTime=System.currentTimeMillis() - startTime;
var length=codedOutputStream.getTotalBytesWritten();
var singleLength = length/benchmark;
var l=0;
protobufSerializationTime = System.currentTimeMillis() - startTime;
var length = codedOutputStream.getTotalBytesWritten();
protobufSerializationSize = length;
var singleLength = length / benchmark;
var l = 0;
//在循环中去掉创建CodedInputStream的代码保证比较耗时公平性
var codeInputs=new CodedInputStream[benchmark];
for(int i=0;i<benchmark;i++)
{
codeInputs[i]=CodedInputStream.newInstance(protobufBuff, l+singleLength, singleLength);
var codeInputs = new CodedInputStream[benchmark];
for (int i = 0; i < benchmark; i++) {
codeInputs[i] = CodedInputStream.newInstance(protobufBuff, l + singleLength, singleLength);
}
startTime = System.currentTimeMillis();
for(int i=0;i<benchmark;i++)
{
for (int i = 0; i < benchmark; i++) {
var mess = FieldProtobufObject.FloatObject.parseFrom(codeInputs[i]);
}
protobufDeserializationTime=System.currentTimeMillis() - startTime;
System.out.println(StringUtils.format("序列化 [zfoo] [float类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), zfooSerializationTime));
System.out.println(StringUtils.format("序列化 [kryo] [float类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), kryoSerializationTime));
System.out.println(StringUtils.format("序列化 [protobuf] [float类型] [thread:{}] [time:{}]", Thread.currentThread().getName(),protobufSerializationTime));
protobufDeserializationTime = System.currentTimeMillis() - startTime;
System.out.println(StringUtils.format("序列化 [zfoo] [float类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), zfooSerializationTime, zfooSerializationSize));
System.out.println(StringUtils.format("序列化 [kryo] [float类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), kryoSerializationTime, kryoSerializationSize));
System.out.println(StringUtils.format("序列化 [protobuf] [float类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), protobufSerializationTime, protobufSerializationSize));
System.out.println(StringUtils.format("反序列化 [zfoo] [float类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), zfooDeserializationTime));
System.out.println(StringUtils.format("反序列化 [kryo] [float类型] [thread:{}] [time:{}]", Thread.currentThread().getName(),kryoDeserializationTime));
System.out.println(StringUtils.format("反序列化 [kryo] [float类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), kryoDeserializationTime));
System.out.println(StringUtils.format("反序列化 [protobuf] [float类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), protobufDeserializationTime));
}
public static final int intAValue=-100000000;
public static final int intBValue=-1000000;
public static final int intCValue=-10000;
public static final int intDValue=-100;
public static final int intEValue=0;
public static final int intFValue=100;
public static final int intGValue=10000;
public static final int intHValue=1000000;
public static final int intIValue=100000000;
public static final int intJValue=2147483647;
public static final float floatAValue=(float)-10000000.123;
public static final float floatBValue=(float)-100000.123;
public static final float floatCValue=(float)-1000.123;
public static final float floatDValue=(float)-10.123;
public static final float floatEValue=(float)-0.12345678;
public static final float floatFValue=(float)0.12345678;
public static final float floatGValue=(float)10.123;
public static final float floatHValue=(float)1000.123;
public static final float floatIValue=(float)100000.123;
public static final float floatJValue=(float)10000000.123;
static {
@Test
public void testBytesObject() throws IOException {
ByteBuf buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 10000_0000, 100000000);
benchmark = benchmark * 5;
long startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
ProtocolManager.write(buffer, bytesObject);
}
zfooSerializationTime = System.currentTimeMillis() - startTime;
zfooSerializationSize = buffer.writerIndex();
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var packet = ProtocolManager.read(buffer);
}
zfooDeserializationTime = System.currentTimeMillis() - startTime;
buffer.release();
var kryo = kryos.get();
var output = new Output(10000_0000);
var input = new Input(output.getBuffer());
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
kryo.writeObject(output, bytesObject);
}
kryoSerializationTime = System.currentTimeMillis() - startTime;
kryoSerializationSize = output.position();
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var mess = kryo.readObject(input, BytesObject.class);
}
kryoDeserializationTime = System.currentTimeMillis() - startTime;
output.close();
var protobufBuff = new byte[10000_0000];
var codedOutputStream = CodedOutputStream.newInstance(protobufBuff);
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
protobufBytesObject.writeTo(codedOutputStream);
}
protobufSerializationTime = System.currentTimeMillis() - startTime;
var length = codedOutputStream.getTotalBytesWritten();
protobufSerializationSize = length;
var singleLength = length / benchmark;
var l = 0;
//在循环中去掉创建CodedInputStream的代码保证比较耗时公平性
var codeInputs = new CodedInputStream[benchmark];
for (int i = 0; i < benchmark; i++) {
codeInputs[i] = CodedInputStream.newInstance(protobufBuff, l + singleLength, singleLength);
}
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var mess = FieldProtobufObject.BytesObject.parseFrom(codeInputs[i]);
}
protobufDeserializationTime = System.currentTimeMillis() - startTime;
System.out.println(StringUtils.format("序列化 [zfoo] [byte数组类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), zfooSerializationTime, zfooSerializationSize));
System.out.println(StringUtils.format("序列化 [kryo] [byte数组类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), kryoSerializationTime, kryoSerializationSize));
System.out.println(StringUtils.format("序列化 [protobuf] [byte数组类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), protobufSerializationTime, protobufSerializationSize));
System.out.println(StringUtils.format("反序列化 [zfoo] [byte数组类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), zfooDeserializationTime));
System.out.println(StringUtils.format("反序列化 [kryo] [byte数组类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), kryoDeserializationTime));
System.out.println(StringUtils.format("反序列化 [protobuf] [byte数组类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), protobufDeserializationTime));
}
@Test
public void testStringObject() throws IOException {
ByteBuf buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 10000_0000, 100000000);
benchmark = benchmark * 3;
long startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
ProtocolManager.write(buffer, stringObject);
}
zfooSerializationTime = System.currentTimeMillis() - startTime;
zfooSerializationSize = buffer.writerIndex();
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var packet = ProtocolManager.read(buffer);
}
zfooDeserializationTime = System.currentTimeMillis() - startTime;
buffer.release();
var kryo = kryos.get();
var output = new Output(10000_0000);
var input = new Input(output.getBuffer());
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
kryo.writeObject(output, stringObject);
}
kryoSerializationTime = System.currentTimeMillis() - startTime;
kryoSerializationSize = output.position();
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var mess = kryo.readObject(input, StringObject.class);
}
kryoDeserializationTime = System.currentTimeMillis() - startTime;
output.close();
var protobufBuff = new byte[10000_0000];
var codedOutputStream = CodedOutputStream.newInstance(protobufBuff);
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
protobufStringObject.writeTo(codedOutputStream);
}
protobufSerializationTime = System.currentTimeMillis() - startTime;
var length = codedOutputStream.getTotalBytesWritten();
protobufSerializationSize = length;
var singleLength = length / benchmark;
var l = 0;
//在循环中去掉创建CodedInputStream的代码保证比较耗时公平性
var codeInputs = new CodedInputStream[benchmark];
for (int i = 0; i < benchmark; i++) {
codeInputs[i] = CodedInputStream.newInstance(protobufBuff, l + singleLength, singleLength);
}
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var mess = FieldProtobufObject.StringObject.parseFrom(codeInputs[i]);
}
protobufDeserializationTime = System.currentTimeMillis() - startTime;
System.out.println(StringUtils.format("序列化 [zfoo] [string类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), zfooSerializationTime, zfooSerializationSize));
System.out.println(StringUtils.format("序列化 [kryo] [string类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), kryoSerializationTime, kryoSerializationSize));
System.out.println(StringUtils.format("序列化 [protobuf] [string类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), protobufSerializationTime, protobufSerializationSize));
System.out.println(StringUtils.format("反序列化 [zfoo] [string类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), zfooDeserializationTime));
System.out.println(StringUtils.format("反序列化 [kryo] [string类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), kryoDeserializationTime));
System.out.println(StringUtils.format("反序列化 [protobuf] [string类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), protobufDeserializationTime));
}
@Test
public void testListIntegerObject() throws IOException {
ByteBuf buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 10000_0000, 100000000);
long startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
ProtocolManager.write(buffer, listIntegerObject);
}
zfooSerializationTime = System.currentTimeMillis() - startTime;
zfooSerializationSize = buffer.writerIndex();
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var packet = ProtocolManager.read(buffer);
}
zfooDeserializationTime = System.currentTimeMillis() - startTime;
buffer.release();
var kryo = kryos.get();
var output = new Output(10000_0000);
var input = new Input(output.getBuffer());
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
kryo.writeObject(output, listIntegerObject);
}
kryoSerializationTime = System.currentTimeMillis() - startTime;
kryoSerializationSize = output.position();
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var mess = kryo.readObject(input, ListIntegerObject.class);
}
kryoDeserializationTime = System.currentTimeMillis() - startTime;
output.close();
var protobufBuff = new byte[10000_0000];
var codedOutputStream = CodedOutputStream.newInstance(protobufBuff);
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
protobufListIntegerObject.writeTo(codedOutputStream);
}
protobufSerializationTime = System.currentTimeMillis() - startTime;
var length = codedOutputStream.getTotalBytesWritten();
protobufSerializationSize = length;
var singleLength = length / benchmark;
var l = 0;
//在循环中去掉创建CodedInputStream的代码保证比较耗时公平性
var codeInputs = new CodedInputStream[benchmark];
for (int i = 0; i < benchmark; i++) {
codeInputs[i] = CodedInputStream.newInstance(protobufBuff, l + singleLength, singleLength);
}
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var mess = FieldProtobufObject.ListIntegerObject.parseFrom(codeInputs[i]);
}
protobufDeserializationTime = System.currentTimeMillis() - startTime;
System.out.println(StringUtils.format("序列化 [zfoo] [list类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), zfooSerializationTime, zfooSerializationSize));
System.out.println(StringUtils.format("序列化 [kryo] [list类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), kryoSerializationTime, kryoSerializationSize));
System.out.println(StringUtils.format("序列化 [protobuf] [list类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), protobufSerializationTime, protobufSerializationSize));
System.out.println(StringUtils.format("反序列化 [zfoo] [list类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), zfooDeserializationTime));
System.out.println(StringUtils.format("反序列化 [kryo] [list类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), kryoDeserializationTime));
System.out.println(StringUtils.format("反序列化 [protobuf] [list类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), protobufDeserializationTime));
}
@Test
public void testSetObject() throws IOException {
ByteBuf buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 10000_0000, 100000000);
long startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
ProtocolManager.write(buffer, setObject);
}
zfooSerializationTime = System.currentTimeMillis() - startTime;
zfooSerializationSize = buffer.writerIndex();
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var packet = ProtocolManager.read(buffer);
}
zfooDeserializationTime = System.currentTimeMillis() - startTime;
buffer.release();
var kryo = kryos.get();
var output = new Output(10000_0000);
var input = new Input(output.getBuffer());
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
kryo.writeObject(output, setObject);
}
kryoSerializationTime = System.currentTimeMillis() - startTime;
kryoSerializationSize = output.position();
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var mess = kryo.readObject(input, SetObject.class);
}
kryoDeserializationTime = System.currentTimeMillis() - startTime;
output.close();
System.out.println(StringUtils.format("序列化 [zfoo] [set类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), zfooSerializationTime, zfooSerializationSize));
System.out.println(StringUtils.format("序列化 [kryo] [set类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), kryoSerializationTime, kryoSerializationSize));
System.out.println(StringUtils.format("反序列化 [zfoo] [set类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), zfooDeserializationTime));
System.out.println(StringUtils.format("反序列化 [kryo] [set类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), kryoDeserializationTime));
}
@Test
public void testMapObject() throws IOException {
ByteBuf buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 10000_0000, 100000000);
benchmark = benchmark / 2;
long startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
ProtocolManager.write(buffer, mapObject);
}
zfooSerializationTime = System.currentTimeMillis() - startTime;
zfooSerializationSize = buffer.writerIndex();
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var packet = ProtocolManager.read(buffer);
}
zfooDeserializationTime = System.currentTimeMillis() - startTime;
buffer.release();
var kryo = kryos.get();
var output = new Output(10000_0000);
var input = new Input(output.getBuffer());
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
kryo.writeObject(output, mapObject);
}
kryoSerializationTime = System.currentTimeMillis() - startTime;
kryoSerializationSize = output.position();
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var mess = kryo.readObject(input, MapObject.class);
}
kryoDeserializationTime = System.currentTimeMillis() - startTime;
output.close();
var protobufBuff = new byte[10000_0000];
var codedOutputStream = CodedOutputStream.newInstance(protobufBuff);
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
protobufMapObject.writeTo(codedOutputStream);
}
protobufSerializationTime = System.currentTimeMillis() - startTime;
var length = codedOutputStream.getTotalBytesWritten();
protobufSerializationSize = length;
var singleLength = length / benchmark;
var l = 0;
//在循环中去掉创建CodedInputStream的代码保证比较耗时公平性
var codeInputs = new CodedInputStream[benchmark];
for (int i = 0; i < benchmark; i++) {
codeInputs[i] = CodedInputStream.newInstance(protobufBuff, l + singleLength, singleLength);
}
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var mess = FieldProtobufObject.MapObject.parseFrom(codeInputs[i]);
}
protobufDeserializationTime = System.currentTimeMillis() - startTime;
System.out.println(StringUtils.format("序列化 [zfoo] [map类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), zfooSerializationTime, zfooSerializationSize));
System.out.println(StringUtils.format("序列化 [kryo] [map类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), kryoSerializationTime, kryoSerializationSize));
System.out.println(StringUtils.format("序列化 [protobuf] [map类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), protobufSerializationTime, protobufSerializationSize));
System.out.println(StringUtils.format("反序列化 [zfoo] [map类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), zfooDeserializationTime));
System.out.println(StringUtils.format("反序列化 [kryo] [map类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), kryoDeserializationTime));
System.out.println(StringUtils.format("反序列化 [protobuf] [map类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), protobufDeserializationTime));
}
@Test
public void testInnerObjectObject() throws IOException {
ByteBuf buffer = new UnpooledHeapByteBuf(ByteBufAllocator.DEFAULT, 10000_0000, 100000000);
benchmark = benchmark * 2;
long startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
ProtocolManager.write(buffer, innerObjectObject);
}
zfooSerializationTime = System.currentTimeMillis() - startTime;
zfooSerializationSize = buffer.writerIndex();
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var packet = ProtocolManager.read(buffer);
}
zfooDeserializationTime = System.currentTimeMillis() - startTime;
buffer.release();
var kryo = kryos.get();
var output = new Output(10000_0000);
var input = new Input(output.getBuffer());
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
kryo.writeObject(output, innerObjectObject);
}
kryoSerializationTime = System.currentTimeMillis() - startTime;
kryoSerializationSize = output.position();
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var mess = kryo.readObject(input, InnerObjectObject.class);
}
kryoDeserializationTime = System.currentTimeMillis() - startTime;
output.close();
var protobufBuff = new byte[10000_0000];
var codedOutputStream = CodedOutputStream.newInstance(protobufBuff);
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
protobufInnerObjectObject.writeTo(codedOutputStream);
}
protobufSerializationTime = System.currentTimeMillis() - startTime;
var length = codedOutputStream.getTotalBytesWritten();
protobufSerializationSize = length;
var singleLength = length / benchmark;
var l = 0;
//在循环中去掉创建CodedInputStream的代码保证比较耗时公平性
var codeInputs = new CodedInputStream[benchmark];
for (int i = 0; i < benchmark; i++) {
codeInputs[i] = CodedInputStream.newInstance(protobufBuff, l + singleLength, singleLength);
}
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
var mess = FieldProtobufObject.InnerObjectObject.parseFrom(codeInputs[i]);
}
protobufDeserializationTime = System.currentTimeMillis() - startTime;
System.out.println(StringUtils.format("序列化 [zfoo] [内嵌Object类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), zfooSerializationTime, zfooSerializationSize));
System.out.println(StringUtils.format("序列化 [kryo] [内嵌Object类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), kryoSerializationTime, kryoSerializationSize));
System.out.println(StringUtils.format("序列化 [protobuf] [内嵌Object类型] [thread:{}] [time:{}] [size:{}]", Thread.currentThread().getName(), protobufSerializationTime, protobufSerializationSize));
System.out.println(StringUtils.format("反序列化 [zfoo] [内嵌Object类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), zfooDeserializationTime));
System.out.println(StringUtils.format("反序列化 [kryo] [内嵌Object类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), kryoDeserializationTime));
System.out.println(StringUtils.format("反序列化 [protobuf] [内嵌Object类型] [thread:{}] [time:{}]", Thread.currentThread().getName(), protobufDeserializationTime));
}
public static final int intAValue = -100000000;
public static final int intBValue = -1000000;
public static final int intCValue = -10000;
public static final int intDValue = -100;
public static final int intEValue = 0;
public static final int intFValue = 100;
public static final int intGValue = 10000;
public static final int intHValue = 1000000;
public static final int intIValue = 100000000;
public static final int intJValue = 2147483647;
public static final int intXValue = 1;
public static final float floatAValue = (float) -10000000.123;
public static final float floatBValue = (float) -100000.123;
public static final float floatCValue = (float) -1000.123;
public static final float floatDValue = (float) -10.123;
public static final float floatEValue = (float) -0.12345678;
public static final float floatFValue = (float) 0.12345678;
public static final float floatGValue = (float) 10.123;
public static final float floatHValue = (float) 1000.123;
public static final float floatIValue = (float) 100000.123;
public static final float floatJValue = (float) 10000000.123;
public static final byte[] bytesValue = new byte[]{-128, -90, -60, -30, 0, 30, 60, 90, 120, 127};
public static final String stringAValue = "a";
public static final String stringBValue = "ab";
public static final String stringCValue = "abc";
public static final String stringDValue = "abcd";
public static final String stringEValue = "abcde";
public static final String stringFValue = "abcdef";
public static final String stringGValue = "abcdefg";
public static final String stringHValue = "abcdefgh";
public static final String stringIValue = "abcdefghi";
public static final String stringJValue = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
public static final List<Integer> listValue = new ArrayList<>(ArrayUtils.toList(new int[]{Integer.MIN_VALUE, -10000000, -100000, -1000, -10, 0, 10, 1000, 100000, 10000000, Integer.MAX_VALUE}));
public static final Set<Integer> setValue = new HashSet<>(listValue);
public static final Map<Integer, String> mapValue = new HashMap<>(Map.of(Integer.MIN_VALUE, "a", -10000000, "ab", -100000, "abc", -1000, "abcd", -10, "abcde",
0, "abcdef", 10, "abcdefg", 1000, "abcdefgh", 100000, "abcdefghi", Integer.MAX_VALUE, "abcdefghij"));
public static final InnerObject innerObjectValue = new InnerObject();
@BeforeClass
public static void prepare() {
intObject.setA(intAValue);
intObject.setB(intBValue);
intObject.setC(intCValue);
@@ -262,8 +619,8 @@ public class FieldSpeedTest {
intObject.setH(intHValue);
intObject.setI(intIValue);
intObject.setJ(intJValue);
protobufIntObject= FieldProtobufObject.IntObject.newBuilder().setA(intAValue).setB(intBValue).setC(intCValue).setD(intDValue)
.setE(intEValue).setF(intFValue).setG(intGValue).setH(intHValue).setI(intIValue).setJ(intJValue).build();
protobufIntObject = FieldProtobufObject.IntObject.newBuilder().setA(intAValue).setB(intBValue).setC(intCValue).setD(intDValue)
.setE(intEValue).setF(intFValue).setG(intGValue).setH(intHValue).setI(intIValue).setJ(intJValue).build();
integerObject.setA(intAValue);
integerObject.setB(intBValue);
@@ -286,15 +643,94 @@ public class FieldSpeedTest {
floatObject.setH(floatHValue);
floatObject.setI(floatIValue);
floatObject.setJ(floatJValue);
protobufFloatObject=FieldProtobufObject.FloatObject.newBuilder().setA(floatAValue).setB(floatBValue).setC(floatCValue)
protobufFloatObject = FieldProtobufObject.FloatObject.newBuilder().setA(floatAValue).setB(floatBValue).setC(floatCValue)
.setD(floatDValue).setE(floatEValue).setF(floatFValue).setG(floatGValue).setH(floatHValue).setI(floatIValue).setJ(floatJValue).build();
}
static {
bytesObject.setA(bytesValue);
bytesObject.setB(bytesValue);
bytesObject.setC(bytesValue);
bytesObject.setD(bytesValue);
bytesObject.setE(bytesValue);
bytesObject.setF(bytesValue);
bytesObject.setG(bytesValue);
bytesObject.setH(bytesValue);
bytesObject.setI(bytesValue);
bytesObject.setJ(bytesValue);
protobufBytesObject = FieldProtobufObject.BytesObject.newBuilder().setA(ByteString.copyFrom(bytesValue)).setB(ByteString.copyFrom(bytesValue)).setC(ByteString.copyFrom(bytesValue))
.setD(ByteString.copyFrom(bytesValue)).setE(ByteString.copyFrom(bytesValue)).setF(ByteString.copyFrom(bytesValue)).setG(ByteString.copyFrom(bytesValue)).setH(ByteString.copyFrom(bytesValue)).setI(ByteString.copyFrom(bytesValue)).setJ(ByteString.copyFrom(bytesValue)).build();
stringObject.setA(stringAValue);
stringObject.setB(stringBValue);
stringObject.setC(stringCValue);
stringObject.setD(stringDValue);
stringObject.setE(stringEValue);
stringObject.setF(stringFValue);
stringObject.setG(stringGValue);
stringObject.setH(stringHValue);
stringObject.setI(stringIValue);
stringObject.setJ(stringJValue);
protobufStringObject = FieldProtobufObject.StringObject.newBuilder().setA(stringAValue).setB(stringBValue).setC(stringCValue)
.setD(stringDValue).setE(stringEValue).setF(stringFValue).setG(stringGValue).setH(stringHValue).setI(stringIValue).setJ(stringJValue).build();
listIntegerObject.setA(listValue);
listIntegerObject.setB(listValue);
listIntegerObject.setC(listValue);
listIntegerObject.setD(listValue);
listIntegerObject.setE(listValue);
listIntegerObject.setF(listValue);
listIntegerObject.setG(listValue);
listIntegerObject.setH(listValue);
listIntegerObject.setI(listValue);
listIntegerObject.setJ(listValue);
protobufListIntegerObject = FieldProtobufObject.ListIntegerObject.newBuilder().addAllA(listValue).addAllB(listValue).addAllC(listValue)
.addAllD(listValue).addAllE(listValue).addAllF(listValue).addAllG(listValue).addAllH(listValue).addAllI(listValue).addAllJ(listValue).build();
setObject.setA(setValue);
setObject.setB(setValue);
setObject.setC(setValue);
setObject.setD(setValue);
setObject.setE(setValue);
setObject.setF(setValue);
setObject.setG(setValue);
setObject.setH(setValue);
setObject.setI(setValue);
setObject.setJ(setValue);
mapObject.setA(mapValue);
mapObject.setB(mapValue);
mapObject.setC(mapValue);
mapObject.setD(mapValue);
mapObject.setE(mapValue);
mapObject.setF(mapValue);
mapObject.setG(mapValue);
mapObject.setH(mapValue);
mapObject.setI(mapValue);
mapObject.setJ(mapValue);
protobufMapObject = FieldProtobufObject.MapObject.newBuilder().putAllA(mapValue).putAllB(mapValue).putAllC(mapValue)
.putAllD(mapValue).putAllE(mapValue).putAllF(mapValue).putAllG(mapValue).putAllH(mapValue).putAllI(mapValue).putAllJ(mapValue).build();
innerObjectValue.setX(10000);
innerObjectObject.setA(innerObjectValue);
innerObjectObject.setB(innerObjectValue);
innerObjectObject.setC(innerObjectValue);
innerObjectObject.setD(innerObjectValue);
innerObjectObject.setE(innerObjectValue);
innerObjectObject.setF(innerObjectValue);
innerObjectObject.setG(innerObjectValue);
innerObjectObject.setH(innerObjectValue);
innerObjectObject.setI(innerObjectValue);
innerObjectObject.setJ(innerObjectValue);
var protobufInnerObject = FieldProtobufObject.InnerObject.newBuilder().setX(intXValue).build();
protobufInnerObjectObject = FieldProtobufObject.InnerObjectObject.newBuilder().setA(protobufInnerObject).setB(protobufInnerObject).setC(protobufInnerObject)
.setD(protobufInnerObject).setE(protobufInnerObject).setF(protobufInnerObject).setG(protobufInnerObject).setH(protobufInnerObject).setI(protobufInnerObject).setJ(protobufInnerObject).build();
// zfoo -----------------------------------------------------------------------
System.setProperty("io.netty.buffer.checkAccessible", "false");
System.setProperty("io.netty.buffer.checkBounds", "false");
var op = GenerateOperation.NO_OPERATION;
ProtocolManager.initProtocolAuto(Set.of(IntObject.class,IntegerObject.class,FloatObject.class), op);
ProtocolManager.initProtocolAuto(Set.of(IntObject.class, IntegerObject.class, FloatObject.class, BytesObject.class, StringObject.class, ListIntegerObject.class, SetObject.class, MapObject.class, InnerObjectObject.class), op);
}
public static final ThreadLocal<Kryo> kryos = new ThreadLocal<>() {
@Override
protected Kryo initialValue() {
@@ -302,6 +738,13 @@ public class FieldSpeedTest {
kryo.register(IntObject.class);
kryo.register(IntegerObject.class);
kryo.register(FloatObject.class);
kryo.register(BytesObject.class);
kryo.register(StringObject.class);
kryo.register(ListIntegerObject.class);
kryo.register(SetObject.class);
kryo.register(MapObject.class);
kryo.register(InnerObject.class);
kryo.register(InnerObjectObject.class);
kryo.register(byte[].class);
kryo.register(Byte[].class);
kryo.register(short[].class);
@@ -0,0 +1,130 @@
package com.zfoo.protocol.field.packet;
import com.zfoo.protocol.IPacket;
import java.util.Arrays;
public class BytesObject implements IPacket {
private byte[] a;
private byte[] b;
private byte[] c;
private byte[] d;
private byte[] e;
private byte[] f;
private byte[] g;
private byte[] h;
private byte[] i;
private byte[] j;
public byte[] getA() {
return a;
}
public void setA(byte[] a) {
this.a = a;
}
public byte[] getB() {
return b;
}
public void setB(byte[] b) {
this.b = b;
}
public byte[] getC() {
return c;
}
public void setC(byte[] c) {
this.c = c;
}
public byte[] getD() {
return d;
}
public void setD(byte[] d) {
this.d = d;
}
public byte[] getE() {
return e;
}
public void setE(byte[] e) {
this.e = e;
}
public byte[] getF() {
return f;
}
public void setF(byte[] f) {
this.f = f;
}
public byte[] getG() {
return g;
}
public void setG(byte[] g) {
this.g = g;
}
public byte[] getH() {
return h;
}
public void setH(byte[] h) {
this.h = h;
}
public byte[] getI() {
return i;
}
public void setI(byte[] i) {
this.i = i;
}
public byte[] getJ() {
return j;
}
public void setJ(byte[] j) {
this.j = j;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BytesObject that = (BytesObject) o;
return Arrays.equals(a, that.a) && Arrays.equals(b, that.b) && Arrays.equals(c, that.c) && Arrays.equals(d, that.d) && Arrays.equals(e, that.e) && Arrays.equals(f, that.f) && Arrays.equals(g, that.g) && Arrays.equals(h, that.h) && Arrays.equals(i, that.i) && Arrays.equals(j, that.j);
}
@Override
public int hashCode() {
int result = Arrays.hashCode(a);
result = 31 * result + Arrays.hashCode(b);
result = 31 * result + Arrays.hashCode(c);
result = 31 * result + Arrays.hashCode(d);
result = 31 * result + Arrays.hashCode(e);
result = 31 * result + Arrays.hashCode(f);
result = 31 * result + Arrays.hashCode(g);
result = 31 * result + Arrays.hashCode(h);
result = 31 * result + Arrays.hashCode(i);
result = 31 * result + Arrays.hashCode(j);
return result;
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,30 @@
package com.zfoo.protocol.field.packet;
import com.zfoo.protocol.IPacket;
import java.util.Objects;
public class InnerObject implements IPacket {
private int x;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
InnerObject that = (InnerObject) o;
return x == that.x;
}
@Override
public int hashCode() {
return Objects.hash(x);
}
}
@@ -0,0 +1,120 @@
package com.zfoo.protocol.field.packet;
import com.zfoo.protocol.IPacket;
import java.util.Objects;
public class InnerObjectObject implements IPacket {
private InnerObject a;
private InnerObject b;
private InnerObject c;
private InnerObject d;
private InnerObject e;
private InnerObject f;
private InnerObject g;
private InnerObject h;
private InnerObject i;
private InnerObject j;
public InnerObject getA() {
return a;
}
public void setA(InnerObject a) {
this.a = a;
}
public InnerObject getB() {
return b;
}
public void setB(InnerObject b) {
this.b = b;
}
public InnerObject getC() {
return c;
}
public void setC(InnerObject c) {
this.c = c;
}
public InnerObject getD() {
return d;
}
public void setD(InnerObject d) {
this.d = d;
}
public InnerObject getE() {
return e;
}
public void setE(InnerObject e) {
this.e = e;
}
public InnerObject getF() {
return f;
}
public void setF(InnerObject f) {
this.f = f;
}
public InnerObject getG() {
return g;
}
public void setG(InnerObject g) {
this.g = g;
}
public InnerObject getH() {
return h;
}
public void setH(InnerObject h) {
this.h = h;
}
public InnerObject getI() {
return i;
}
public void setI(InnerObject i) {
this.i = i;
}
public InnerObject getJ() {
return j;
}
public void setJ(InnerObject j) {
this.j = j;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
InnerObjectObject that = (InnerObjectObject) o;
return Objects.equals(a, that.a) && Objects.equals(b, that.b) && Objects.equals(c, that.c) && Objects.equals(d, that.d) && Objects.equals(e, that.e) && Objects.equals(f, that.f) && Objects.equals(g, that.g) && Objects.equals(h, that.h) && Objects.equals(i, that.i) && Objects.equals(j, that.j);
}
@Override
public int hashCode() {
return Objects.hash(a, b, c, d, e, f, g, h, i, j);
}
}
@@ -0,0 +1,121 @@
package com.zfoo.protocol.field.packet;
import com.zfoo.protocol.IPacket;
import java.util.List;
import java.util.Objects;
public class ListIntegerObject implements IPacket {
private List<Integer> a;
private List<Integer> b;
private List<Integer> c;
private List<Integer> d;
private List<Integer> e;
private List<Integer> f;
private List<Integer> g;
private List<Integer> h;
private List<Integer> i;
private List<Integer> j;
public List<Integer> getA() {
return a;
}
public void setA(List<Integer> a) {
this.a = a;
}
public List<Integer> getB() {
return b;
}
public void setB(List<Integer> b) {
this.b = b;
}
public List<Integer> getC() {
return c;
}
public void setC(List<Integer> c) {
this.c = c;
}
public List<Integer> getD() {
return d;
}
public void setD(List<Integer> d) {
this.d = d;
}
public List<Integer> getE() {
return e;
}
public void setE(List<Integer> e) {
this.e = e;
}
public List<Integer> getF() {
return f;
}
public void setF(List<Integer> f) {
this.f = f;
}
public List<Integer> getG() {
return g;
}
public void setG(List<Integer> g) {
this.g = g;
}
public List<Integer> getH() {
return h;
}
public void setH(List<Integer> h) {
this.h = h;
}
public List<Integer> getI() {
return i;
}
public void setI(List<Integer> i) {
this.i = i;
}
public List<Integer> getJ() {
return j;
}
public void setJ(List<Integer> j) {
this.j = j;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ListIntegerObject that = (ListIntegerObject) o;
return Objects.equals(a, that.a) && Objects.equals(b, that.b) && Objects.equals(c, that.c) && Objects.equals(d, that.d) && Objects.equals(e, that.e) && Objects.equals(f, that.f) && Objects.equals(g, that.g) && Objects.equals(h, that.h) && Objects.equals(i, that.i) && Objects.equals(j, that.j);
}
@Override
public int hashCode() {
return Objects.hash(a, b, c, d, e, f, g, h, i, j);
}
}
@@ -0,0 +1,121 @@
package com.zfoo.protocol.field.packet;
import com.zfoo.protocol.IPacket;
import java.util.Map;
import java.util.Objects;
public class MapObject implements IPacket {
private Map<Integer, String> a;
private Map<Integer, String> b;
private Map<Integer, String> c;
private Map<Integer, String> d;
private Map<Integer, String> e;
private Map<Integer, String> f;
private Map<Integer, String> g;
private Map<Integer, String> h;
private Map<Integer, String> i;
private Map<Integer, String> j;
public Map<Integer, String> getA() {
return a;
}
public void setA(Map<Integer, String> a) {
this.a = a;
}
public Map<Integer, String> getB() {
return b;
}
public void setB(Map<Integer, String> b) {
this.b = b;
}
public Map<Integer, String> getC() {
return c;
}
public void setC(Map<Integer, String> c) {
this.c = c;
}
public Map<Integer, String> getD() {
return d;
}
public void setD(Map<Integer, String> d) {
this.d = d;
}
public Map<Integer, String> getE() {
return e;
}
public void setE(Map<Integer, String> e) {
this.e = e;
}
public Map<Integer, String> getF() {
return f;
}
public void setF(Map<Integer, String> f) {
this.f = f;
}
public Map<Integer, String> getG() {
return g;
}
public void setG(Map<Integer, String> g) {
this.g = g;
}
public Map<Integer, String> getH() {
return h;
}
public void setH(Map<Integer, String> h) {
this.h = h;
}
public Map<Integer, String> getI() {
return i;
}
public void setI(Map<Integer, String> i) {
this.i = i;
}
public Map<Integer, String> getJ() {
return j;
}
public void setJ(Map<Integer, String> j) {
this.j = j;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MapObject mapObject = (MapObject) o;
return Objects.equals(a, mapObject.a) && Objects.equals(b, mapObject.b) && Objects.equals(c, mapObject.c) && Objects.equals(d, mapObject.d) && Objects.equals(e, mapObject.e) && Objects.equals(f, mapObject.f) && Objects.equals(g, mapObject.g) && Objects.equals(h, mapObject.h) && Objects.equals(i, mapObject.i) && Objects.equals(j, mapObject.j);
}
@Override
public int hashCode() {
return Objects.hash(a, b, c, d, e, f, g, h, i, j);
}
}
@@ -0,0 +1,121 @@
package com.zfoo.protocol.field.packet;
import com.zfoo.protocol.IPacket;
import java.util.Objects;
import java.util.Set;
public class SetObject implements IPacket {
private Set<Integer> a;
private Set<Integer> b;
private Set<Integer> c;
private Set<Integer> d;
private Set<Integer> e;
private Set<Integer> f;
private Set<Integer> g;
private Set<Integer> h;
private Set<Integer> i;
private Set<Integer> j;
public Set<Integer> getA() {
return a;
}
public void setA(Set<Integer> a) {
this.a = a;
}
public Set<Integer> getB() {
return b;
}
public void setB(Set<Integer> b) {
this.b = b;
}
public Set<Integer> getC() {
return c;
}
public void setC(Set<Integer> c) {
this.c = c;
}
public Set<Integer> getD() {
return d;
}
public void setD(Set<Integer> d) {
this.d = d;
}
public Set<Integer> getE() {
return e;
}
public void setE(Set<Integer> e) {
this.e = e;
}
public Set<Integer> getF() {
return f;
}
public void setF(Set<Integer> f) {
this.f = f;
}
public Set<Integer> getG() {
return g;
}
public void setG(Set<Integer> g) {
this.g = g;
}
public Set<Integer> getH() {
return h;
}
public void setH(Set<Integer> h) {
this.h = h;
}
public Set<Integer> getI() {
return i;
}
public void setI(Set<Integer> i) {
this.i = i;
}
public Set<Integer> getJ() {
return j;
}
public void setJ(Set<Integer> j) {
this.j = j;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SetObject setObject = (SetObject) o;
return Objects.equals(a, setObject.a) && Objects.equals(b, setObject.b) && Objects.equals(c, setObject.c) && Objects.equals(d, setObject.d) && Objects.equals(e, setObject.e) && Objects.equals(f, setObject.f) && Objects.equals(g, setObject.g) && Objects.equals(h, setObject.h) && Objects.equals(i, setObject.i) && Objects.equals(j, setObject.j);
}
@Override
public int hashCode() {
return Objects.hash(a, b, c, d, e, f, g, h, i, j);
}
}
@@ -0,0 +1,120 @@
package com.zfoo.protocol.field.packet;
import com.zfoo.protocol.IPacket;
import java.util.Objects;
public class StringObject implements IPacket {
private String a;
private String b;
private String c;
private String d;
private String e;
private String f;
private String g;
private String h;
private String i;
private String j;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
public String getD() {
return d;
}
public void setD(String d) {
this.d = d;
}
public String getE() {
return e;
}
public void setE(String e) {
this.e = e;
}
public String getF() {
return f;
}
public void setF(String f) {
this.f = f;
}
public String getG() {
return g;
}
public void setG(String g) {
this.g = g;
}
public String getH() {
return h;
}
public void setH(String h) {
this.h = h;
}
public String getI() {
return i;
}
public void setI(String i) {
this.i = i;
}
public String getJ() {
return j;
}
public void setJ(String j) {
this.j = j;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
StringObject that = (StringObject) o;
return Objects.equals(a, that.a) && Objects.equals(b, that.b) && Objects.equals(c, that.c) && Objects.equals(d, that.d) && Objects.equals(e, that.e) && Objects.equals(f, that.f) && Objects.equals(g, that.g) && Objects.equals(h, that.h) && Objects.equals(i, that.i) && Objects.equals(j, that.j);
}
@Override
public int hashCode() {
return Objects.hash(a, b, c, d, e, f, g, h, i, j);
}
}
+89 -20
View File
@@ -4,27 +4,96 @@ option java_package = "com.zfoo.protocol.field.packet";
option java_outer_classname = "FieldProtobufObject";
message IntObject{
int32 a=1;
int32 b=2;
int32 c=3;
int32 d=4;
int32 e=5;
int32 f=6;
int32 g=7;
int32 h=8;
int32 i=9;
int32 j=10;
int32 a = 1;
int32 b = 2;
int32 c = 3;
int32 d = 4;
int32 e = 5;
int32 f = 6;
int32 g = 7;
int32 h = 8;
int32 i = 9;
int32 j = 10;
}
message FloatObject{
float a=1;
float b=2;
float c=3;
float d=4;
float e=5;
float f=6;
float g=7;
float h=8;
float i=9;
float j=10;
float a = 1;
float b = 2;
float c = 3;
float d = 4;
float e = 5;
float f = 6;
float g = 7;
float h = 8;
float i = 9;
float j = 10;
}
message BytesObject{
bytes a = 1;
bytes b = 2;
bytes c = 3;
bytes d = 4;
bytes e = 5;
bytes f = 6;
bytes g = 7;
bytes h = 8;
bytes i = 9;
bytes j = 10;
}
message StringObject{
string a = 1;
string b = 2;
string c = 3;
string d = 4;
string e = 5;
string f = 6;
string g = 7;
string h = 8;
string i = 9;
string j = 10;
}
message ListIntegerObject{
repeated int32 a = 1;
repeated int32 b = 2;
repeated int32 c = 3;
repeated int32 d = 4;
repeated int32 e = 5;
repeated int32 f = 6;
repeated int32 g = 7;
repeated int32 h = 8;
repeated int32 i = 9;
repeated int32 j = 10;
}
message MapObject{
map<int32, string> a = 1;
map<int32, string> b = 2;
map<int32, string> c = 3;
map<int32, string> d = 4;
map<int32, string> e = 5;
map<int32, string> f = 6;
map<int32, string> g = 7;
map<int32, string> h = 8;
map<int32, string> i = 9;
map<int32, string> j = 10;
}
message InnerObject{
int32 x = 1;
}
message InnerObjectObject{
InnerObject a = 1;
InnerObject b = 2;
InnerObject c = 3;
InnerObject d = 4;
InnerObject e = 5;
InnerObject f = 6;
InnerObject g = 7;
InnerObject h = 8;
InnerObject i = 9;
InnerObject j = 10;
}
@@ -68,8 +68,6 @@ public abstract class SchedulerBus {
logger.error("scheduler triggers an error.", e);
}
}, 0, TimeUtils.MILLIS_PER_SECOND, TimeUnit.MILLISECONDS);
executor.scheduleAtFixedRate(TimeUtils::currentTimeMillis, 0, 20, TimeUnit.MILLISECONDS);
}
public static class SchedulerThreadFactory implements ThreadFactory {
@@ -83,7 +83,8 @@ public abstract class TimeUtils {
SchedulerBus.refreshMinTriggerTimestamp();
}
private static volatile long timestamp = System.currentTimeMillis();
// volatile reduces the cache hit ratio of the CPU
private static long timestamp = System.currentTimeMillis();
/**
* 获取精确的时间戳
@@ -94,9 +95,9 @@ public abstract class TimeUtils {
}
/**
* 获取最多只有20ms延迟的粗略时间戳适用于对时间精度要求不高的场景最多只有20ms误差
* CN获取最多只有一秒延迟的粗略时间戳适用于对时间精度要求不高的场景比System.currentTimeMillis()的性能高10倍
* <p>
* 比System.currentTimeMillis()的性能高10倍
* ENObtain a coarse timestamp with a delay of up to one second, which is suitable for scenarios that do not require high time accuracy
*/
public static long now() {
return timestamp;
@@ -143,7 +143,7 @@ public class StorageManager implements IStorageManager {
@Override
public void inject() {
var applicationContext = StorageContext.getApplicationContext();
var componentBeans = applicationContext.getBeansWithAnnotation(Component.class);
var componentBeans = applicationContext.getBeansWithAnnotation(Component.class);
for (var bean : componentBeans.values()) {
ReflectionUtils.filterFieldsInClass(bean.getClass(), field -> field.isAnnotationPresent(ResInjection.class), field -> {
Type type = field.getGenericType();
@@ -251,15 +251,14 @@ public class StorageManager implements IStorageManager {
var resourcePatternResolver = new PathMatchingResourcePatternResolver();
var metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
String fileName;
if(clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).value().equals("")
&&clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).alias().equals("")){
fileName=clazz.getSimpleName();
}
else{
if(clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).value().equals(""))
fileName=clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).alias();
if (clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).value().equals("")
&& clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).alias().equals("")) {
fileName = clazz.getSimpleName();
} else {
if (clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).value().equals(""))
fileName = clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).alias();
else
fileName=clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).value();
fileName = clazz.getAnnotation(com.zfoo.storage.model.anno.Resource.class).value();
}
try {
// 一个class类只能匹配一个资源文件如果匹配多个则会有歧义
@@ -3,7 +3,7 @@ package com.zfoo.storage.model.anno;
import java.lang.annotation.*;
/**
*指定文件列名不指定则默认列名与字段名一致
* 指定文件列名不指定则默认列名与字段名一致
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@@ -21,6 +21,7 @@ import java.lang.annotation.*;
* 资源注解
* 可以指定对应的资源文件名只指定文件名不需要文件后缀
* 如果不指定资源文件名则默认通过扫描路径获取与类名相同的文件资源
*
* @author godotg
* @version 4.0
*/
@@ -20,6 +20,7 @@ import com.zfoo.protocol.serializer.CodeLanguage;
import com.zfoo.protocol.util.FileUtils;
import com.zfoo.protocol.util.JsonUtils;
import com.zfoo.storage.manager.StorageManager;
import com.zfoo.storage.model.anno.ExcelFieldName;
import com.zfoo.storage.model.anno.Id;
import com.zfoo.storage.model.anno.Resource;
import com.zfoo.storage.model.config.StorageConfig;
@@ -60,6 +61,7 @@ public class ExportBinaryTest {
public int id;
public String name;
@ExcelFieldName("年龄")
public int age;
public float score;
public String[] courses;