Skip to content

Commit

Permalink
test[protocol]: fury test
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Oct 26, 2023
1 parent 57b6ba8 commit 1ec936b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
13 changes: 13 additions & 0 deletions protocol/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.furyio</groupId>
<artifactId>fury-core</artifactId>
<version>0.2.1</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>slf4j-api</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import java.nio.ByteBuffer;

/**
* 自定义私有协议格式,可以针对性的对存在性能瓶颈的数据结构做特定优化
* 自定义协议格式,可以针对某些特定场景对做特定优化
*
* @author godotg
*/
Expand Down
69 changes: 69 additions & 0 deletions protocol/src/test/java/com/zfoo/protocol/BenchmarkTesting.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
import com.zfoo.protocol.generate.GenerateOperation;
import com.zfoo.protocol.packet.*;
import com.zfoo.protocol.util.StringUtils;
import io.fury.Fury;
import io.fury.ThreadLocalFury;
import io.fury.ThreadSafeFury;
import io.fury.config.Language;
import io.fury.memory.MemoryBuffer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.UnpooledHeapByteBuf;
Expand Down Expand Up @@ -63,6 +68,7 @@ public void singleThreadBenchmarks() {
System.out.println(StringUtils.format("[单线程性能测试-->[benchmark:{}]]", benchmark));

zfooTest();
furyTest();
protobufTest();
kryoTest();

Expand All @@ -84,6 +90,7 @@ public void multipleThreadBenchmarks() throws InterruptedException {
System.out.println(StringUtils.format("[多线程性能测试-->[benchmark:{}]]", benchmark));

zfooMultipleThreadTest();
furyMultipleThreadTest();
protobufMultipleThreadTest();
kryoMultipleThreadTest();

Expand Down Expand Up @@ -135,6 +142,42 @@ public void zfooTest() {
System.out.println(StringUtils.format("[zfoo] [复杂对象] [thread:{}] [size:{}] [time:{}]", Thread.currentThread().getName(), buffer.writerIndex(), System.currentTimeMillis() - startTime));
}

@Test
public void furyTest() {
var buffer = MemoryBuffer.newHeapBuffer(1_0000);
// 序列化和反序列化简单对象
long startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
buffer.writerIndex(0);
buffer.readerIndex(0);
fury.serialize(buffer, simpleObject);
var obj = fury.deserialize(buffer);
}

System.out.println(StringUtils.format("[fury] [简单对象] [thread:{}] [size:{}] [time:{}]", Thread.currentThread().getName(), buffer.writerIndex(), System.currentTimeMillis() - startTime));

// 序列化和反序列化常规对象
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
buffer.writerIndex(0);
buffer.readerIndex(0);
fury.serialize(buffer, normalObject);
var obj = fury.deserialize(buffer);
}

System.out.println(StringUtils.format("[fury] [常规对象] [thread:{}] [size:{}] [time:{}]", Thread.currentThread().getName(), buffer.writerIndex(), System.currentTimeMillis() - startTime));

// 序列化和反序列化复杂对象
startTime = System.currentTimeMillis();
for (int i = 0; i < benchmark; i++) {
buffer.writerIndex(0);
buffer.readerIndex(0);
fury.serialize(buffer, complexObject);
var obj = fury.deserialize(buffer);
}
System.out.println(StringUtils.format("[fury] [复杂对象] [thread:{}] [size:{}] [time:{}]", Thread.currentThread().getName(), buffer.writerIndex(), System.currentTimeMillis() - startTime));
}

@Test
public void kryoTest() {
try {
Expand Down Expand Up @@ -235,6 +278,17 @@ public void zfooMultipleThreadTest() throws InterruptedException {
countdown.await();
}

@Test
public void furyMultipleThreadTest() throws InterruptedException {
var countdown = new CountDownLatch(threadNum);
for (var i = 0; i < threadNum; i++) {
executors[i].execute(() -> {
furyTest();
countdown.countDown();
});
}
countdown.await();
}

@Test
public void kryoMultipleThreadTest() throws InterruptedException {
Expand Down Expand Up @@ -310,6 +364,21 @@ protected Kryo initialValue() {
}
}

private static ThreadSafeFury fury;

static {
fury = new ThreadLocalFury(classLoader -> {
Fury f = Fury.builder().withLanguage(Language.JAVA)
.build();
f.register(ComplexObject.class);
f.register(NormalObject.class);
f.register(SimpleObject.class);
f.register(VeryBigObject.class);
f.register(ObjectA.class);
f.register(ObjectB.class);
return f;
});
}

// -------------------------------------------以下为测试用例---------------------------------------------------------------
// 简单类型
Expand Down

0 comments on commit 1ec936b

Please sign in to comment.