This commit is contained in:
Administrator
2025-04-24 10:16:38 +08:00
parent 6de65562be
commit 7267aec1fd
108 changed files with 7621 additions and 1890 deletions
+29 -27
View File
@@ -24,7 +24,6 @@ import org.kne.cloud.network.klalb.KLALBPacketLink;
import org.kne.cloud.network.klalb.KLALBUtils;
import org.kne.cloud.network.klalb.PINGPacket;
import org.kne.cloud.network.klalb.PONGPacket;
import org.kne.cloud.network.klalb.TESTPacket;
import org.kne.cloud.network.klalb.VADDRACKPacket;
import org.kne.cloud.network.klalb.VADDRPacket;
import org.kne.cloud.network.klalb.VADDRREQPacket;
@@ -74,12 +73,13 @@ public class Kperf implements Runnable{
private SpeedLimiter test = new SpeedLimiter(MIN_SPEED,10000000L);
private static long MIN_SPEED=64*1024L;
private SpeedLimiter congress = new SpeedLimiter(0,10000000L);
private SpeedLimiter congress = new SpeedLimiter(10000000L);
private long congressSpeed = 0;
private double loadPercent = 1.01;
private volatile int testSize=0;
//kperf {KLALB_Stream}[2486:1:34de:4abd:98c1:abd1:fc7a:401]:4564
KLALBPacket testPacket=null;
//kperf {KLALB_Stream}[2486:1:f189:7589:f1f7:6f5e:b2eb:1]:4564
//kperf {KLALB_Stream}[2486:1::8888]:4564
@Override
public void run() {
Thread tc = ThreadTool.makeVDaemonThreadIfSupport("测速控制线程", () -> {
@@ -100,14 +100,14 @@ public class Kperf implements Runnable{
System.out.printf("下载延迟 最小:%.3fms 平均:%.3fms 抖动:%.3fms\n",monitor.getInDelayMin()/1000000.0,monitor.getInDelayAvg()/1000000.0,monitor.getInJitter()/1000000.0);
System.out.println("等待时间"+wtimes+"s");
Thread.sleep(wtimes*1000);*/
Thread.sleep(wtimes*1000);
*/
startTime=System.nanoTime();
if(targetAddress!=null) {
System.out.println("测试2/4:下载带宽测试");
test.setLimitspeed(0);
testSize=60000;
testPacket=new TESTNPacket(60000);
for (int i = 0; i < 30; i++) {
System.out.printf("%.2fs\t", (System.nanoTime()- startTime)/1000000000.0);
System.out.printf("下载带宽:%s/s\n", KLALBUtils.bytesUnit(monitor.getInSpeedAvg()));
@@ -118,7 +118,7 @@ public class Kperf implements Runnable{
}else {
System.out.println("测试2/4:上传带宽测试");
test.setLimitspeed(1000L*1024L*1024L*1024L);
testSize=60000;
testPacket=new TESTNPacket(60000);
for (int i = 0; i < 30; i++) {
System.out.printf("%.2fs\t", (System.nanoTime()- startTime)/1000000000.0);
System.out.printf("上传带宽:%s/s\n", KLALBUtils.bytesUnit(monitor.getOutSpeedAvg()));
@@ -135,7 +135,7 @@ public class Kperf implements Runnable{
if(targetAddress!=null) {
System.out.println("测试3/4:上传带宽测试");
test.setLimitspeed(1000L*1024L*1024L*1024L);
testSize=60000;
testPacket=new TESTNPacket(60000);
for (int i = 0; i < 30; i++) {
System.out.printf("%.2fs\t", (System.nanoTime()- startTime)/1000000000.0);
System.out.printf("上传带宽:%s/s\n", KLALBUtils.bytesUnit(monitor.getOutSpeedAvg()));
@@ -146,7 +146,7 @@ public class Kperf implements Runnable{
}else {
System.out.println("测试3/4:下载带宽测试");
test.setLimitspeed(0);
testSize=60000;
testPacket=new TESTNPacket(60000);
for (int i = 0; i < 30; i++) {
System.out.printf("%.2fs\t", (System.nanoTime()- startTime)/1000000000.0);
System.out.printf("下载带宽:%s/s\n", KLALBUtils.bytesUnit(monitor.getInSpeedAvg()));
@@ -162,7 +162,7 @@ public class Kperf implements Runnable{
if(targetAddress!=null) {
System.out.println("测试2/4:下载包转发率测试");
test.setLimitspeed(0);
testSize=0;
testPacket=new TEST1Packet();
for (int i = 0; i < 30; i++) {
System.out.printf("%.2fs\t", (System.nanoTime()- startTime)/1000000000.0);
System.out.printf("下载包转发率:%sPPS\n", KLALBUtils.defaultUnit(monitor.getInPPS()));
@@ -171,7 +171,7 @@ public class Kperf implements Runnable{
}else {
System.out.println("测试2/4:上传包转发率测试");
test.setLimitspeed(1000L*1024L*1024L*1024L);
testSize=0;
testPacket=new TEST1Packet();
for (int i = 0; i < 30; i++) {
System.out.printf("%.2fs\t", (System.nanoTime()- startTime)/1000000000.0);
System.out.printf("上传包转发率:%sPPS\n", KLALBUtils.defaultUnit(monitor.getOutPPS()));
@@ -186,7 +186,7 @@ public class Kperf implements Runnable{
if(targetAddress!=null) {
System.out.println("测试3/4:上传包转发率测试");
test.setLimitspeed(1000L*1024L*1024L*1024L);
testSize=0;
testPacket=new TEST1Packet();
for (int i = 0; i < 30; i++) {
System.out.printf("%.2fs\t", (System.nanoTime()- startTime)/1000000000.0);
System.out.printf("上传包转发率:%sPPS\n", KLALBUtils.defaultUnit(monitor.getOutPPS()));
@@ -196,7 +196,7 @@ public class Kperf implements Runnable{
}else {
System.out.println("测试3/4:下载包转发率测试");
test.setLimitspeed(0);
testSize=0;
testPacket=new TEST1Packet();
for (int i = 0; i < 30; i++) {
System.out.printf("%.2fs\t", (System.nanoTime()- startTime)/1000000000.0);
System.out.printf("下载包转发率:%sPPS\n", KLALBUtils.defaultUnit(monitor.getInPPS()));
@@ -206,17 +206,19 @@ public class Kperf implements Runnable{
}
System.out.println("等待时间"+wtimes+"s");
Thread.sleep(wtimes*1000);
System.out.println("测速结束!");
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("测速结束!");
System.out.println("测速被中断!");
}finally {
close();
}
});
try {
monitor.setState(MonitorData.CONNECTING);
if(link==null)
link=KLALBUtils.createKLALBPacketLink(bindAddress, targetAddress,20000);
link=KLALBUtils.createKLALBPacketLink(bindAddress, targetAddress,false,20000);
link.setSoTimeout(20000);
@@ -243,10 +245,9 @@ public class Kperf implements Runnable{
}else {
if (test.checkTransmit(testSize+3)) {
TESTPacket tp=new TESTPacket(testSize);
writePacketToKPL(tp);
KLALBPacket testp=testPacket;
if (testp!=null&&test.checkTransmit(testp.getLength())) {
writePacketToKPL(testp);
@@ -351,7 +352,7 @@ public class Kperf implements Runnable{
congress.setLimitspeed(Math.max(MIN_SPEED,(long) (congressSpeed * loadPercent)) );
// System.out.println(congressSpeed);
break;
case KLALBPacket.TEST:
case KLALBPacket.TESTN:
break;
}
Thread.yield();
@@ -371,6 +372,7 @@ public class Kperf implements Runnable{
e.printStackTrace();
}
}
tc.interrupt();
}
}
private volatile long time = System.nanoTime();
@@ -441,7 +443,7 @@ public class Kperf implements Runnable{
private KLALBPacket readPacketFromKPL() throws IOException {
KLALBPacket packet = link.readPacket();
KLALBPacket packet = link.readKLALBPacket();
if (showpacket) {
System.out.println("RX:" + packet);
@@ -459,7 +461,7 @@ public class Kperf implements Runnable{
long length=packet.getLength();
monitor.getOutTrafficAL().addAndGet(length);
monitor.getOutPacketCounterAL().incrementAndGet();
link.writePacket(packet);
link.writeKLALBPacket(packet);
link.flush();
packet.putTimePassport("sended");
packet.printPassport();
@@ -0,0 +1,57 @@
package org.kne.cloud.network.perf;
import java.nio.ByteBuffer;
import org.kne.cloud.network.NetworkPacket;
public class MemcpyBenchmark {
public static void testArrayCopySpeed(int i) {
byte[]a=new byte[i];
byte[]b=new byte[i];
long start=System.nanoTime();
System.arraycopy(a, 0, b, 0, i);
long time=System.nanoTime()-start;
long speed=i*1000000000L/time;
System.out.println(speed/1024L/1024L+" MB/s");
}
public static void testByteBufferCopySpeed(int i) {
ByteBuffer a=ByteBuffer.allocateDirect(i);
ByteBuffer b=ByteBuffer.allocateDirect(i);
long start=System.nanoTime();
a.put(b);
long time=System.nanoTime()-start;
long speed=i*1000000000L/time;
System.out.println(speed/1024L/1024L+" MB/s");
}
public static Object un;
public static void testDirectByteBufferAllocateSpeed(int i) {
long start=System.nanoTime();
for(int ix=0;ix<10000;ix++) {
ByteBuffer a=ByteBuffer.allocateDirect(i);
un=a;
}
long time=System.nanoTime()-start;
long speed=i*1000000000L/time*10000L;
System.out.println(speed/1024L/1024L+" MB/s");
}
public static void testByteBufferAllocateSpeed(int i) {
long start=System.nanoTime();
for(int ix=0;ix<10000;ix++) {
ByteBuffer a=ByteBuffer.allocate(i);
un=a;
}
long time=System.nanoTime()-start;
long speed=i*1000000000L/time*10000L;
System.out.println(speed/1024L/1024L+" MB/s");
}
public static void testByteBufferAllocatorSpeed(int i) {
long start=System.nanoTime();
for(int ix=0;ix<10000;ix++) {
ByteBuffer a=NetworkPacket.bufferAllocator.allocate(i);
un=a;
}
long time=System.nanoTime()-start;
long speed=i*1000000000L/time*10000L;
System.out.println(speed/1024L/1024L+" MB/s");
}
}
@@ -0,0 +1,91 @@
package org.kne.cloud.network.perf;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ServerSocketChannel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.kne.cloud.network.MultipurposeSocketAddress;
import org.kne.cloud.network.SocketChannelListener;
import org.kne.cloud.network.ThreadTool;
import org.kne.cloud.network.klalb.KLALBController;
import org.kne.cloud.network.klalb.KLALBRemoteLine;
import org.kne.cloud.network.klalb.StreamChannelKLALBPacketLink;
public class NodeBenchmark implements Runnable{
private List<NodeEntry> nodeList=new ArrayList<>();
private class NodeEntry{
private SocketChannelListener scl;
private KLALBController kc;
public SocketChannelListener getScl() {
return scl;
}
public KLALBController getKc() {
return kc;
}
public NodeEntry(SocketChannelListener scl, KLALBController kc) {
super();
this.scl = scl;
this.kc = kc;
}
}
private KLALBController klalbController;
private int nodes;
public NodeBenchmark(KLALBController klalbController,int nodes) {
this.klalbController=klalbController;
this.nodes=nodes;
}
public void run() {
Random r=new Random();
for (int i = 0; i < nodes; i++) {
KLALBController kct=new KLALBController(false);
try {
SocketChannelListener scl=new SocketChannelListener(ServerSocketChannel.open().bind(new InetSocketAddress("127.0.0.8",0)));
scl.setCon((c)->{
try {
kct.addRemoteLine(new KLALBRemoteLine(new StreamChannelKLALBPacketLink(c)));
} catch (IOException e) {
e.printStackTrace();
}
});
System.out.println("KLALBController "+i+" created,locator:"+kct.getSelf());
int size=nodeList.size();
if(size>0) {
for (int j = 0; j < 1; j++) {
int ind=r.nextInt(size);
NodeEntry ne=nodeList.get(ind);
MultipurposeSocketAddress mpsa=new MultipurposeSocketAddress((InetSocketAddress) ne.getScl().getServerSocketChannel().getLocalAddress());
kct.addRemoteLines(mpsa);
System.out.println("connect:" +ne.getKc().getSelf().getAddress());
}
}
nodeList.add(new NodeEntry( scl, kct));
Thread.sleep(100);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
NodeEntry ne=nodeList.get(0);
MultipurposeSocketAddress mpsa=new MultipurposeSocketAddress((InetSocketAddress) ne.getScl().getServerSocketChannel().getLocalAddress());
klalbController.addRemoteLines(mpsa);
} catch (IOException e) {
e.printStackTrace();
}
}
public void startPerfing() {
ThreadTool.makeVThreadIfSupport("节点数量测试线程" ,this).start();
}
}
@@ -0,0 +1,34 @@
package org.kne.cloud.network.perf;
import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import org.kne.cloud.network.klalb.KLALBPacket;
public class TEST1Packet extends KLALBPacket {
private static final int HEADER_LENGTH=1;
@Override
public long getLength() {
return HEADER_LENGTH;
}
public TEST1Packet() {
super(TEST1,HEADER_LENGTH);
}
public TEST1Packet(ByteBuffer bb) {
super(bb,HEADER_LENGTH);
}
@Override
public String toString() {
return "TEST1";
}
}
@@ -0,0 +1,74 @@
package org.kne.cloud.network.perf;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import org.kne.cloud.network.NetworkPacket;
import org.kne.cloud.network.klalb.KLALBPacket;
public class TESTNPacket extends KLALBPacket {
private static final int HEADER_LENGTH=3;
private ByteBuffer dataBuffer;//=NetworkPacket.databufferpool_65535.borrow();
@Override
public long getLength() {
return HEADER_LENGTH+dataBuffer.limit();
}
public TESTNPacket(int limit) {
super(TESTN,HEADER_LENGTH);
//dataBuffer.limit(limit);
dataBuffer=ByteBuffer.allocate(limit);
}
public ByteBuffer getDataBuffer() {
return dataBuffer;
}
@Override
public void writeToChannel(WritableByteChannel dto) throws IOException {
klalbHeader.putChar(1, (char) dataBuffer.limit());
super.writeToChannel(dto);
//System.out.println(dataBuffer);
dto.write(dataBuffer.slice(0, dataBuffer.limit()));
}
@Override
public void readFromChannel(ReadableByteChannel din) throws IOException {
super.readFromChannel(din);
int limit=klalbHeader.getChar(1);
//dataBuffer.clear();
//dataBuffer.limit(limit);
dataBuffer=ByteBuffer.allocate(limit);
while(dataBuffer.hasRemaining()){
if(din.read(dataBuffer)==-1) {
throw new EOFException();
}
}
dataBuffer.flip();
}
public TESTNPacket(ByteBuffer bb) {
super(bb,HEADER_LENGTH);
}
@Override
public String toString() {
return "TESTN["+dataBuffer.limit()+"]";
}
}