From 6cdd676c2d89862bdedf9a3df749a0ec6e143796 Mon Sep 17 00:00:00 2001 From: godotg Date: Sun, 9 Jul 2023 09:55:41 +0800 Subject: [PATCH] test[rpc]: rpc benchmark test --- .../tcpAsync/client/BenchmarkAsyncTest.java | 70 +++++++++++++++++++ .../tcpSync/client/BenchmarkSyncTest.java | 66 +++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 net/src/test/java/com/zfoo/net/core/tcpAsync/client/BenchmarkAsyncTest.java create mode 100644 net/src/test/java/com/zfoo/net/core/tcpSync/client/BenchmarkSyncTest.java diff --git a/net/src/test/java/com/zfoo/net/core/tcpAsync/client/BenchmarkAsyncTest.java b/net/src/test/java/com/zfoo/net/core/tcpAsync/client/BenchmarkAsyncTest.java new file mode 100644 index 00000000..9ab82404 --- /dev/null +++ b/net/src/test/java/com/zfoo/net/core/tcpAsync/client/BenchmarkAsyncTest.java @@ -0,0 +1,70 @@ +/* + * 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.net.core.tcpAsync.client; + +import com.zfoo.net.NetContext; +import com.zfoo.net.core.tcp.TcpClient; +import com.zfoo.net.packet.tcp.AsyncMessAnswer; +import com.zfoo.net.packet.tcp.AsyncMessAsk; +import com.zfoo.protocol.util.JsonUtils; +import com.zfoo.util.ThreadUtils; +import com.zfoo.util.net.HostAndPort; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author godotg + */ +@Ignore +public class BenchmarkAsyncTest { + + private static final Logger logger = LoggerFactory.getLogger(BenchmarkAsyncTest.class); + + @Test + public void benchmarkTest() throws Exception { + var context = new ClassPathXmlApplicationContext("config.xml"); + + var client = new TcpClient(HostAndPort.valueOf("127.0.0.1:9000")); + var session = client.start(); + + // 异步请求消息是一起发送过去的,服务器排队处理消息容易导致超时,可以调高Router的DEFAULT_TIMEOUT的超时时间,现在默认是3秒超时 + var threadNums = Runtime.getRuntime().availableProcessors(); + var requestNums = 1000; + for (int i = 0; i < threadNums; i++) { + var thread = new Thread(new Runnable() { + @Override + public void run() { + for (int i = 0; i < requestNums; i++) { + try { + var ask = new AsyncMessAsk(); + ask.setMessage("Hello, this is async client!"); + NetContext.getRouter().asyncAsk(session, ask, AsyncMessAnswer.class, null) + .whenComplete(answer -> { + logger.info("异步请求收到结果[{}]", JsonUtils.object2String(answer)); + } + ); + } catch (Exception e) { + logger.info("同步请求异常", e); + } + } + } + }); + thread.start(); + } + ThreadUtils.sleep(Long.MAX_VALUE); + } + +} diff --git a/net/src/test/java/com/zfoo/net/core/tcpSync/client/BenchmarkSyncTest.java b/net/src/test/java/com/zfoo/net/core/tcpSync/client/BenchmarkSyncTest.java new file mode 100644 index 00000000..dc662e80 --- /dev/null +++ b/net/src/test/java/com/zfoo/net/core/tcpSync/client/BenchmarkSyncTest.java @@ -0,0 +1,66 @@ +/* + * 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.net.core.tcpSync.client; + +import com.zfoo.net.NetContext; +import com.zfoo.net.core.tcp.TcpClient; +import com.zfoo.net.packet.tcp.SyncMessAnswer; +import com.zfoo.net.packet.tcp.SyncMessAsk; +import com.zfoo.protocol.util.JsonUtils; +import com.zfoo.util.ThreadUtils; +import com.zfoo.util.net.HostAndPort; +import org.junit.Ignore; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @author godotg + */ +@Ignore +public class BenchmarkSyncTest { + + private static final Logger logger = LoggerFactory.getLogger(BenchmarkSyncTest.class); + + @Test + public void benchmarkTest() throws Exception { + var context = new ClassPathXmlApplicationContext("config.xml"); + + var client = new TcpClient(HostAndPort.valueOf("127.0.0.1:9000")); + var session = client.start(); + + var threadNums = Runtime.getRuntime().availableProcessors(); + var requestNums = 10_0000; + for (int i = 0; i < threadNums; i++) { + var thread = new Thread(new Runnable() { + @Override + public void run() { + for (int i = 0; i < requestNums; i++) { + try { + var ask = new SyncMessAsk(); + ask.setMessage("Hello, this is sync client!"); + var answer = NetContext.getRouter().syncAsk(session, ask, SyncMessAnswer.class, null).packet(); + logger.info("同步请求收到结果[{}]", JsonUtils.object2String(answer)); + } catch (Exception e) { + logger.info("同步请求异常", e); + } + } + } + }); + thread.start(); + } + ThreadUtils.sleep(Long.MAX_VALUE); + } + +}