simulator TCP data rate #4203

java to java TCP/IP baseline test
This commit is contained in:
rusefillc 2022-05-28 17:24:09 -04:00
parent 122dc2e5d7
commit 17d6cfc8d2
5 changed files with 135 additions and 17 deletions

View File

@ -63,7 +63,7 @@ public class BinaryProtocolProxy {
*/
while (!targetEcu.isClosed()) {
byte firstByte = clientStream.getDataBuffer().readByte(timeoutMs);
if (firstByte == Fields.TS_COMMAND_F) {
if (firstByte == Fields.TS_GET_PROTOCOL_VERSION_COMMAND_F) {
clientStream.write(TS_PROTOCOL.getBytes());
clientStream.flush();
continue;

View File

@ -50,6 +50,10 @@ public class BinaryProtocolServer {
private static final int SD_STATUS_OFFSET = 246;
private static final int FAST_TRANSFER_PACKET_SIZE = 2048;
static {
log.configureDebugEnabled(false);
}
public AtomicInteger unknownCommands = new AtomicInteger();
public static final ServerSocketFunction SECURE_SOCKET_FACTORY = rusEFISSLContext::getSSLServerSocket;
@ -149,14 +153,7 @@ public class BinaryProtocolServer {
if (length == null)
continue;
if (log.debugEnabled())
log.debug("Got [" + length + "] length promise");
Packet packet = readPromisedBytes(in, length);
byte[] payload = packet.getPacket();
if (payload.length == 0)
throw new IOException("Empty packet");
byte[] payload = getPacketContent(in, length);
byte command = payload[0];
@ -211,11 +208,24 @@ public class BinaryProtocolServer {
}
}
@NotNull
public static byte[] getPacketContent(IncomingDataBuffer in, Integer length) throws IOException {
if (log.debugEnabled())
log.debug("Got [" + length + "] length promise");
Packet packet = readPromisedBytes(in, length);
byte[] payload = packet.getPacket();
if (payload.length == 0)
throw new IOException("Empty packet");
return payload;
}
/**
* @return null if we have handled GET_PROTOCOL_VERSION_COMMAND command
*/
@Nullable
private Integer getPendingPacketLengthOrHandleProtocolCommand(Socket clientSocket, Context context, IncomingDataBuffer in) throws IOException {
public static Integer getPendingPacketLengthOrHandleProtocolCommand(Socket clientSocket, Context context, IncomingDataBuffer in) throws IOException {
AtomicBoolean handled = new AtomicBoolean();
Handler protocolCommandHandler = () -> {
handleProtocolCommand(clientSocket);
@ -280,7 +290,8 @@ public class BinaryProtocolServer {
}
public static void handleProtocolCommand(Socket clientSocket) throws IOException {
log.info("Got plain F command");
if (log.debugEnabled())
log.debug("Got plain GetProtocol F command");
OutputStream outputStream = clientSocket.getOutputStream();
outputStream.write(TS_PROTOCOL.getBytes());
outputStream.flush();

View File

@ -21,6 +21,9 @@ import static com.devexperts.logging.Logging.getLogging;
public class SandboxCommon {
private static final Logging log = getLogging(SandboxCommon.class);
static {
log.configureDebugEnabled(false);
}
static ConfigurationImage readImage(IoStream tsStream, LinkManager linkManager) throws InterruptedException {
AtomicReference<ConfigurationImage> configurationImageAtomicReference = new AtomicReference<>();
@ -76,14 +79,15 @@ public class SandboxCommon {
throw new IllegalStateException("Unexpected S " + signature);
}
static void runFcommand(String prefix, IoStream tsStream) throws IOException {
static void runGetProtocolCommand(String prefix, IoStream tsStream) throws IOException {
IncomingDataBuffer dataBuffer = tsStream.getDataBuffer();
tsStream.write(new byte[]{Fields.TS_COMMAND_F});
tsStream.write(new byte[]{Fields.TS_GET_PROTOCOL_VERSION_COMMAND_F});
tsStream.flush();
byte[] fResponse = new byte[3];
dataBuffer.waitForBytes("hello", System.currentTimeMillis(), fResponse.length);
dataBuffer.getData(fResponse);
log.info(prefix + " Got F response " + IoStream.printByteArray(fResponse));
if (log.debugEnabled())
log.debug(prefix + " Got GetProtocol F response " + IoStream.printByteArray(fResponse));
if (fResponse[0] != '0' || fResponse[1] != '0' || fResponse[2] != '1')
throw new IllegalStateException("Unexpected TS_COMMAND_F response " + Arrays.toString(fResponse));
}

View File

@ -1,6 +1,5 @@
package com.rusefi.binaryprotocol.test;
import com.opensr5.ConfigurationImage;
import com.rusefi.binaryprotocol.BinaryProtocol;
import com.rusefi.io.LinkManager;
import com.rusefi.io.tcp.TcpIoStream;
@ -19,9 +18,46 @@ public class SimulatorTcpSandbox {
TcpIoStream tsStream = new TcpIoStream("sandbox", s);
LinkManager linkManager = new LinkManager();
SandboxCommon.verifyCrcNoPending(tsStream, linkManager);
// SandboxCommon.verifyCrcNoPending(tsStream, linkManager);
ConfigurationImage ci = SandboxCommon.readImage(tsStream, linkManager);
for (int i = 0; i < 3; i++) {
// warm-up cycles just for fun
String signature = BinaryProtocol.getSignature(tsStream);
}
{
int count = 10000;
long startMs = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
// warm-up cycles just for fun
String signature = BinaryProtocol.getSignature(tsStream);
}
long time = System.currentTimeMillis() - startMs;
double timePerCommand = 1.0 * time / count;
System.out.println("Executed " + count + " getSignature in " + time + "ms\n" + "Per-signature: " + timePerCommand + "ms");
// linkManager.submit(() -> {
}
{
BinaryProtocol bp = new BinaryProtocol(linkManager, tsStream);
int count = 10000;
long startMs = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
// boolean response = bp.requestOutputChannels();
SandboxCommon.runGetProtocolCommand("dd", tsStream);
}
long time = System.currentTimeMillis() - startMs;
double timePerCommand = 1.0 * time / count;
System.out.println("Executed " + count + " GetProtocol in " + time + "ms\n" + "Per-GetProtocol: " + timePerCommand + "ms");
// log.info("requestOutputChannels " + response);
// });
}
// ConfigurationImage ci = SandboxCommon.readImage(tsStream, linkManager);
System.exit(0);
}

View File

@ -0,0 +1,67 @@
package com.rusefi.binaryprotocol.test;
import com.rusefi.CompatibleFunction;
import com.rusefi.Listener;
import com.rusefi.binaryprotocol.IncomingDataBuffer;
import com.rusefi.config.generated.Fields;
import com.rusefi.io.IoStream;
import com.rusefi.io.commands.HelloCommand;
import com.rusefi.io.tcp.BinaryProtocolServer;
import com.rusefi.io.tcp.TcpIoStream;
import java.io.IOException;
import java.net.Socket;
import static com.rusefi.config.generated.Fields.TS_PROTOCOL;
import static com.rusefi.io.tcp.BinaryProtocolServer.TS_OK;
public class TcpServerSandbox {
public static void main(String[] args) throws IOException {
BinaryProtocolServer.tcpServerSocket(29001,
"server",
new CompatibleFunction<Socket, Runnable>() {
@Override
public Runnable apply(Socket socket) {
System.out.println("apply");
return new Runnable() {
@Override
public void run() {
System.out.println("Run server socket: " + socket);
try {
IoStream stream = new TcpIoStream("gauge", socket);
IncomingDataBuffer in = stream.getDataBuffer();
while (!socket.isClosed()) {
Integer length = BinaryProtocolServer.getPendingPacketLengthOrHandleProtocolCommand(socket, new BinaryProtocolServer.Context(), in);
if (length == null)
continue;
byte[] payload = BinaryProtocolServer.getPacketContent(in, length);
byte command = payload[0];
if (command == Fields.TS_HELLO_COMMAND) {
new HelloCommand(Fields.TS_SIGNATURE).handle(stream);
} else if (command == Fields.TS_GET_PROTOCOL_VERSION_COMMAND_F) {
stream.sendPacket((TS_OK + TS_PROTOCOL).getBytes());
} else
throw new UnsupportedOperationException("Unsupported command " + command);
}
} catch (Exception e) {
System.out.println(e);
}
}
};
}
},
new Listener() {
@Override
public void onResult(Object parameter) {
System.out.println("onResult");
}
});
}
}