优化sncp

This commit is contained in:
redkale
2023-01-30 16:32:36 +08:00
parent 7dc57c67ed
commit 6d1cf4b6bf
16 changed files with 138 additions and 80 deletions

View File

@@ -6,6 +6,7 @@
package org.redkale.test.net;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import org.redkale.net.*;
@@ -41,7 +42,7 @@ public class TransportTest {
Thread.sleep(1000);
TransportFactory factory = TransportFactory.create(asyncGroup, 0, 0);
DefaultAnyValue conf = DefaultAnyValue.create(TransportFactory.NAME_PINGINTERVAL, 5);
factory.init(conf, Sncp.PING_BUFFER, Sncp.PONG_BUFFER.remaining());
factory.init(conf, ByteBuffer.wrap(Sncp.getPingBytes()).asReadOnlyBuffer(), Sncp.getPingBytes().length);
Transport transport = factory.createTransportTCP("", null, addrs);
System.out.println(String.format(format, System.currentTimeMillis()));
try {

View File

@@ -5,14 +5,14 @@
*/
package org.redkale.test.sncp;
import java.io.*;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousChannelGroup;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.logging.LogManager;
import org.redkale.boot.LoggingBaseHandler;
import org.redkale.convert.bson.*;
import org.redkale.net.*;
import org.redkale.net.sncp.*;
@@ -33,22 +33,18 @@ public class SncpTest {
private static final int port2 = 4240;
private static final String protocol = "SNCP.UDP";
private static final String protocol = "SNCP.TCP";
private static final ResourceFactory factory = ResourceFactory.create();
public static void main(String[] args) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
final PrintStream ps = new PrintStream(out);
ps.println("handlers = java.util.logging.ConsoleHandler");
ps.println(".handlers = java.util.logging.ConsoleHandler");
ps.println(".level = FINEST");
ps.println("java.util.logging.ConsoleHandler.level = FINEST");
LogManager.getLogManager().readConfiguration(new ByteArrayInputStream(out.toByteArray()));
LoggingBaseHandler.initDebugLogConfig();
factory.register("", BsonConvert.class, BsonFactory.root().getConvert());
if (System.getProperty("client") == null) {
runServer();
if (port2 > 0) runServer2();
if (port2 > 0) {
runServer2();
}
}
if (System.getProperty("server") == null) {
runClient();
@@ -72,7 +68,9 @@ public class SncpTest {
public static ObjectPool<ByteBuffer> newBufferPool() {
return ObjectPool.createSafePool(new LongAdder(), new LongAdder(), 16,
(Object... params) -> ByteBuffer.allocateDirect(8192), null, (e) -> {
if (e == null || e.isReadOnly() || e.capacity() != 8192) return false;
if (e == null || e.isReadOnly() || e.capacity() != 8192) {
return false;
}
e.clear();
return true;
});
@@ -82,7 +80,9 @@ public class SncpTest {
InetSocketAddress addr = new InetSocketAddress(myhost, port);
Set<InetSocketAddress> set = new LinkedHashSet<>();
set.add(addr);
if (port2 > 0) set.add(new InetSocketAddress(myhost, port2));
if (port2 > 0) {
set.add(new InetSocketAddress(myhost, port2));
}
final AsyncIOGroup asyncGroup = new AsyncIOGroup(8192, 16);
asyncGroup.start();
final TransportFactory transFactory = TransportFactory.create(asyncGroup, protocol.endsWith(".UDP") ? "UDP" : "TCP", 0, 0);
@@ -102,25 +102,26 @@ public class SncpTest {
callbean.setContent("数据X");
service.queryLongResult("f", 3, 33L);
service.insert(callbean);
System.out.println("bean.id应该会被修改(id不会是1) " + callbean);
callbean = service.insert(callbean);
System.out.println("bean " + callbean);
System.out.println("---------------------------------------------------");
final int count = 10;
final CountDownLatch cld = new CountDownLatch(count);
final AtomicInteger ai = new AtomicInteger();
long s = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
final int k = i + 1;
new Thread() {
@Override
public void run() {
try {
Thread.sleep(k);
//Thread.sleep(k);
SncpTestBean bean = new SncpTestBean();
bean.setId(k);
bean.setContent("数据: " + (k < 10 ? "0" : "") + k);
StringBuilder sb = new StringBuilder();
sb.append(k).append("------");
for (int i = 0; i < 1200; i++) {
for (int i = 0; i < 12; i++) {
sb.append("_").append(i).append("_").append(k).append("_0123456789");
}
bean.setContent(sb.toString());
@@ -138,11 +139,13 @@ public class SncpTest {
}.start();
}
cld.await();
System.out.println("---并发" + count + "次耗时: " + (System.currentTimeMillis() - s) / 1000.0 + "s");
final CountDownLatch cld2 = new CountDownLatch(1);
long s2 = System.currentTimeMillis();
final CompletableFuture<String> future = service.queryResultAsync(callbean);
future.whenComplete((v, e) -> {
System.out.println("异步执行完毕: " + v + ", 异常为: " + e + ", 耗时: " + (System.currentTimeMillis() - s2) / 1000.0 + "s");
cld2.countDown();
System.out.println("异步执行完毕: " + v + ", 异常为: " + e);
});
cld2.await();
System.out.println("---全部运行完毕---");
@@ -168,7 +171,9 @@ public class SncpTest {
conf.addValue("protocol", protocol);
SncpServer server = new SncpServer(null, System.currentTimeMillis(), conf, factory);
Set<InetSocketAddress> set = new LinkedHashSet<>();
if (port2 > 0) set.add(new InetSocketAddress(myhost, port2));
if (port2 > 0) {
set.add(new InetSocketAddress(myhost, port2));
}
final TransportFactory transFactory = TransportFactory.create(asyncGroup, protocol.endsWith(".UDP") ? "UDP" : "TCP", 0, 0);
transFactory.addGroupInfo("server", set);
SncpTestIService service = Sncp.createSimpleLocalService(SncpTestServiceImpl.class, null, factory, transFactory, addr, "server");

View File

@@ -6,10 +6,10 @@
package org.redkale.test.sncp;
import org.redkale.convert.bson.BsonFactory;
import org.redkale.convert.json.JsonConvert;
import org.redkale.persistence.Id;
import org.redkale.util.Utility;
import org.redkale.source.FilterBean;
import org.redkale.convert.json.*;
import org.redkale.util.Utility;
/**
*
@@ -23,13 +23,14 @@ public class SncpTestBean implements FilterBean {
private String content;
public static void main(String[] args) throws Exception {
SncpTestBean bean = JsonConvert.root().convertFrom(SncpTestBean.class, "{\"content\":\"数据: 01\",\"id\":1}");
String json = "{\"content\":\"数据: 01\",\"id\":1}";
SncpTestBean bean = JsonConvert.root().convertFrom(SncpTestBean.class, json);
System.out.println(bean);
byte[] bs = BsonFactory.root().getConvert().convertTo(bean);
Utility.println("---------", bs);
Utility.println("---------", bs);
System.out.println(BsonFactory.root().getConvert().convertFrom(SncpTestBean.class, bs).toString());
}
@Override
public String toString() {
return JsonConvert.root().convertTo(this);

View File

@@ -22,7 +22,7 @@ public interface SncpTestIService extends Service {
public CompletableFuture<String> queryResultAsync(SncpTestBean bean);
public void insert(SncpTestBean... beans);
public SncpTestBean insert(SncpTestBean bean);
public String updateBean(SncpTestBean bean);
}

View File

@@ -30,7 +30,7 @@ public class SncpTestServiceImpl implements SncpTestIService {
public void run() {
try {
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName() + " 运行了异步方法-----------queryResultAsync方法");
System.out.println(Thread.currentThread().getName() + " sleep 1秒后运行了异步方法-----------queryResultAsync方法");
future.complete("异步result: " + bean);
} catch (Exception e) {
e.printStackTrace();
@@ -83,10 +83,9 @@ public class SncpTestServiceImpl implements SncpTestIService {
}
@Override
public void insert(SncpTestBean... beans) {
for (SncpTestBean bean : beans) {
bean.setId(System.currentTimeMillis());
}
public SncpTestBean insert(SncpTestBean bean) {
bean.setId(System.currentTimeMillis());
return bean;
}
@Override