New option to specify how long test should run.
Some checks failed
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is failing

This commit is contained in:
Mark Nellemann 2023-07-14 08:15:15 +02:00
parent 9f67f98fec
commit 0ca08be582
11 changed files with 171 additions and 79 deletions

View file

@ -12,16 +12,17 @@ You need Java (JRE) version 8 or later to run jnetperf.
- Run **/opt/jnetperf/bin/jperf**, if installed from package, or as **java -jar /path/to/jnetperf.jar** - Run **/opt/jnetperf/bin/jperf**, if installed from package, or as **java -jar /path/to/jnetperf.jar**
```shell ```shell
Usage: jnetperf [-huV] [-l=SIZE] [-n=NUM] [-p=PORT] (-c=SERVER | -s) Usage: jnetperf [-huV] [-l=NUM] [-n=NUM] [-p=NUM] [-t=SEC] (-c=SRV | -s)
For more information visit https://git.data.coop/nellemann/jnetperf For more information visit https://git.data.coop/nellemann/jnetperf
-c, --connect=SERVER Connect to remote server. -c, --connect=SRV Connect to remote server (client).
-h, --help Show this help message and exit. -h, --help Show this help message and exit.
-l, --pkt-len=SIZE Packet size in bytes [default: 1432]. -l, --pkt-len=NUM Packet size in bytes (client) [default: 1432].
-n, --pkt-num=NUM Number of packets to send [default: 150000]. -n, --pkt-num=NUM Number of packets to send (client) [default: 150000].
-p, --port=PORT Network port [default: 4445]. -p, --port=NUM Network port [default: 4445].
-s, --server Run server and wait for client. -s, --server Run server and wait for client (server).
-u, --udp Use UDP network protocol [default: false]. -t, --runtime=SEC Time to run, supersedes pkt-num (client) [default: 0].
-V, --version Print version information and exit. -u, --udp Use UDP network protocol [default: false].
-V, --version Print version information and exit.
``` ```

View file

@ -1,3 +1,3 @@
projectId = jnetperf projectId = jnetperf
projectGroup = biz.nellemann.jnetperf projectGroup = biz.nellemann.jnetperf
projectVersion = 0.0.6 projectVersion = 0.0.8

View file

@ -20,6 +20,7 @@ import picocli.CommandLine;
import picocli.CommandLine.Command; import picocli.CommandLine.Command;
import java.io.IOException; import java.io.IOException;
import java.util.Locale;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
@ -32,20 +33,23 @@ public class Application implements Callable<Integer> {
RunMode runMode; RunMode runMode;
static class RunMode { static class RunMode {
@CommandLine.Option(names = { "-c", "--connect" }, required = true, description = "Connect to remote server.", paramLabel = "HOST") @CommandLine.Option(names = { "-c", "--connect" }, required = true, description = "Connect to remote server (client).", paramLabel = "SRV")
String remoteServer; String remoteServer;
@CommandLine.Option(names = { "-s", "--server" }, required = true, description = "Run server and wait for client.") @CommandLine.Option(names = { "-s", "--server" }, required = true, description = "Run server and wait for client (server).")
boolean runServer = false; boolean runServer = false;
} }
@CommandLine.Option(names = { "-l", "--pkt-len" }, paramLabel = "NUM", description = "Packet size in bytes [default: ${DEFAULT-VALUE}].", converter = SuffixConverter.class) @CommandLine.Option(names = { "-l", "--pkt-len" }, paramLabel = "NUM", description = "Packet size in bytes (client) [default: ${DEFAULT-VALUE}].", converter = UnitSuffixConverter.class)
int packetSize = Payload.DEFAULT_LENGTH; int packetSize = Payload.DEFAULT_LENGTH;
@CommandLine.Option(names = { "-n", "--pkt-num" }, paramLabel = "NUM", description = "Number of packets to send [default: ${DEFAULT-VALUE}].", converter = SuffixConverter.class) @CommandLine.Option(names = { "-n", "--pkt-num" }, paramLabel = "NUM", description = "Number of packets to send (client) [default: ${DEFAULT-VALUE}].", converter = UnitSuffixConverter.class)
int packetCount = 150_000; int packetCount = 150_000;
@CommandLine.Option(names = { "-p", "--port" }, paramLabel = "PORT", description = "Network port [default: ${DEFAULT-VALUE}].") @CommandLine.Option(names = { "-t", "--runtime" }, paramLabel = "SEC", description = "Time to run, precedes pkt-num (client) [default: ${DEFAULT-VALUE}].", converter = TimeSuffixConverter.class)
int timeInSeconds = 0;
@CommandLine.Option(names = { "-p", "--port" }, paramLabel = "NUM", description = "Network port [default: ${DEFAULT-VALUE}].")
int port = 4445; int port = 4445;
@CommandLine.Option(names = { "-u", "--udp" }, description = "Use UDP network protocol [default: ${DEFAULT-VALUE}].") @CommandLine.Option(names = { "-u", "--udp" }, description = "Use UDP network protocol [default: ${DEFAULT-VALUE}].")
@ -54,12 +58,19 @@ public class Application implements Callable<Integer> {
@Override @Override
public Integer call() throws Exception { public Integer call() {
if(runMode.runServer) { // Set locale to en_US to ensure correct/identical number formatting
runServer(); Locale.setDefault(new Locale("en", "US"));
} else if(runMode.remoteServer != null) {
runClient(runMode.remoteServer); try {
if (runMode.runServer) {
runServer();
} else if (runMode.remoteServer != null) {
runClient(runMode.remoteServer);
}
} catch (IOException | InterruptedException e) {
System.err.println(e.getMessage());
} }
return 0; return 0;
@ -81,13 +92,14 @@ public class Application implements Callable<Integer> {
if(useUdp) { if(useUdp) {
if(packetSize > Payload.MAX_UDP_LENGTH) { if(packetSize > Payload.MAX_UDP_LENGTH) {
System.err.println("Packetsize > MAX UDP: " + packetSize);
packetSize = Payload.MAX_UDP_LENGTH; packetSize = Payload.MAX_UDP_LENGTH;
} }
UdpClient udpClient = new UdpClient(remoteHost, port, packetSize, packetCount, 0); UdpClient udpClient = new UdpClient(remoteHost, port, packetSize, packetCount, timeInSeconds);
udpClient.start(); udpClient.start();
} else { } else {
TcpClient tcpClient = new TcpClient(remoteHost, port, packetSize, packetCount, 0); TcpClient tcpClient = new TcpClient(remoteHost, port, packetSize, packetCount, timeInSeconds);
tcpClient.start(); tcpClient.start();
} }
} }

View file

@ -34,7 +34,7 @@ import java.util.Arrays;
public class Payload { public class Payload {
public final static int MIN_LENGTH = 64; public final static int MIN_LENGTH = 64;
public final static int MAX_UDP_LENGTH = 65507; public final static int MAX_UDP_LENGTH = 64000;
public final static int DEFAULT_LENGTH = 1432; public final static int DEFAULT_LENGTH = 1432;
public final static int HEADER_LENGTH = 32; public final static int HEADER_LENGTH = 32;

View file

@ -31,6 +31,7 @@ public class Statistics {
private long packetsPerSec; private long packetsPerSec;
private long packetsUnacked = 0; private long packetsUnacked = 0;
private int tickItererations = 0; private int tickItererations = 0;
private int tickTotal = 0;
private final long[] bytesPerSecAvgTmp = new long[MAX_TICKS_AVG]; private final long[] bytesPerSecAvgTmp = new long[MAX_TICKS_AVG];
private final long[] packetsPerSecAvgTmp = new long[MAX_TICKS_AVG]; private final long[] packetsPerSecAvgTmp = new long[MAX_TICKS_AVG];
@ -67,6 +68,7 @@ public class Statistics {
if(tickItererations % LOG_AVG_MODULO == 0) { if(tickItererations % LOG_AVG_MODULO == 0) {
printAverage(); printAverage();
} }
tickTotal++;
} }
@ -121,6 +123,11 @@ public class Statistics {
} }
public int getRuntime() {
return tickTotal;
}
private long getAverage(long[] array, long fallback) { private long getAverage(long[] array, long fallback) {
long avg = getAverage(array); long avg = getAverage(array);
if(avg < 1) { if(avg < 1) {

View file

@ -5,12 +5,13 @@ import org.slf4j.LoggerFactory;
import java.io.*; import java.io.*;
import java.net.*; import java.net.*;
import java.util.concurrent.atomic.AtomicBoolean;
public class TcpClient { public class TcpClient {
final Logger log = LoggerFactory.getLogger(TcpClient.class); final Logger log = LoggerFactory.getLogger(TcpClient.class);
private Statistics statistics; private final Statistics statistics;
private DataOutputStream out; private DataOutputStream out;
private DataInputStream in; private DataInputStream in;
@ -21,18 +22,18 @@ public class TcpClient {
private Socket socket; private Socket socket;
private final byte[] inBuffer = new byte[Payload.DEFAULT_LENGTH]; private final byte[] inBuffer = new byte[Payload.DEFAULT_LENGTH];
private final int packetCount; private final int packets;
private final int packetSize; private final int length;
private final int packetTime; private final int runtime;
public TcpClient(String hostname, int port, int size, int maxPackets, int maxTime) throws IOException { public TcpClient(String hostname, int port, int length, int packets, int runtime) throws IOException {
log.info("TcpClient() - target: {}, port: {}", hostname, port); log.info("TcpClient() - target: {}, port: {}", hostname, port);
this.port = port; this.port = port;
this.packetSize = size; this.length = length;
this.packetCount = maxPackets; this.packets = packets;
this.packetTime = maxTime; this.runtime = runtime;
address = InetAddress.getByName(hostname); address = InetAddress.getByName(hostname);
statistics = new Statistics(); statistics = new Statistics();
@ -61,39 +62,53 @@ public class TcpClient {
public void start() throws IOException, InterruptedException { public void start() throws IOException, InterruptedException {
AtomicBoolean keepRunning = new AtomicBoolean(true);
Thread shutdownHook = new Thread(() -> {
keepRunning.set(false);
System.out.println("Stopping jnetperf, please wait ...");
});
Runtime.getRuntime().addShutdownHook(shutdownHook);
long sequence = 0;
socket = new Socket(address, port); socket = new Socket(address, port);
in = new DataInputStream(socket.getInputStream()); in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream()); out = new DataOutputStream(socket.getOutputStream());
long sequence = 0;
// Send handshake // Send handshake
Payload payload = new Payload(PayloadType.HANDSHAKE.getValue(), packetSize, sequence++, packetCount); Payload payload = new Payload(PayloadType.HANDSHAKE.getValue(), length, sequence++, packets);
send(payload); send(payload);
payload = receive(); payload = receive();
if(payload.getType() != PayloadType.ACK.getValue()) { if(payload.getType() != PayloadType.ACK.getValue()) {
log.warn("No ACK!"); log.warn("No ACK!");
return; return;
} }
// Data datagrams ... // Send data
for(int i = 0; i < packetCount; i++) { do {
payload = new Payload(PayloadType.DATA.getValue(), packetSize, sequence++, packetCount); payload = new Payload(PayloadType.DATA.getValue(), length, sequence++, packets);
send(payload); send(payload);
payload = receive(); payload = receive();
if(payload.getType() != PayloadType.ACK.getValue()) { if(payload.getType() != PayloadType.ACK.getValue()) {
log.warn("No ACK!"); log.warn("No ACK!");
} }
statistics.tick(); statistics.tick();
}
// End datagram if (sequence > packets) {
//Thread.sleep(100); System.out.println("Max packets reached");
payload = new Payload(PayloadType.END.getValue(), packetSize, sequence++, packetCount); keepRunning.set(false);;
}
if(runtime > 0 && statistics.getRuntime() > runtime) {
System.out.println("Max runtime reached");
keepRunning.set(false);
}
} while (keepRunning.get());
// Send end
payload = new Payload(PayloadType.END.getValue(), length, sequence++, packets);
send(payload); send(payload);
// TODO: Wait for ACK
payload = receive(); payload = receive();
statistics.ack(); statistics.ack();
if(payload.getType() != PayloadType.ACK.getValue()) { if(payload.getType() != PayloadType.ACK.getValue()) {

View file

@ -12,6 +12,7 @@ public class TcpServer extends Thread {
final Logger log = LoggerFactory.getLogger(TcpServer.class); final Logger log = LoggerFactory.getLogger(TcpServer.class);
private final int port;
private ServerSocket socket; private ServerSocket socket;
private DataInputStream in; private DataInputStream in;
private DataOutputStream out; private DataOutputStream out;
@ -20,22 +21,20 @@ public class TcpServer extends Thread {
public TcpServer(int port) throws IOException { public TcpServer(int port) throws IOException {
log.info("TcpServer()"); log.info("TcpServer()");
this.port = port;
socket = new ServerSocket(port);
socket.setSoTimeout(10000);
} }
public void run() { public void run() {
boolean running = true;
try { try {
while (running) { while (true) {
socket = new ServerSocket(port);
socket.setSoTimeout(0); // Wait indefinitely
inBuffer = new byte[Payload.DEFAULT_LENGTH]; inBuffer = new byte[Payload.DEFAULT_LENGTH];
session(); session();
socket.close();
} }
socket.close();
} catch(IOException e) { } catch(IOException e) {
log.error(e.getMessage()); log.error(e.getMessage());
} }
@ -95,8 +94,7 @@ public class TcpServer extends Thread {
private Payload receive() throws IOException { private Payload receive() throws IOException {
in.readFully(inBuffer); in.readFully(inBuffer);
Payload payload = new Payload(inBuffer); return new Payload(inBuffer);
return payload;
} }
} }

View file

@ -0,0 +1,42 @@
package biz.nellemann.jnetperf;
import picocli.CommandLine;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TimeSuffixConverter implements CommandLine.ITypeConverter<Integer> {
final private Pattern pattern = Pattern.compile("(\\d+)([smh])?", Pattern.CASE_INSENSITIVE);
public Integer convert(String value) {
int seconds = 0;
Matcher matcher = pattern.matcher(value);
if (matcher.find()) {
int number = Integer.parseInt(matcher.group(1));
if(matcher.group(2) != null) { // We got the second, minute or hour suffix
String suffix = matcher.group(2);
switch (suffix.toLowerCase(Locale.ROOT)) {
case "s":
seconds = number;
break;
case "m":
seconds = number * 60;
break;
case "h":
seconds = number * 60 * 60;
break;
default:
System.err.println("Unknown suffix: " + suffix);
seconds = number;
}
} else {
seconds = number;
}
}
return seconds;
}
}

View file

@ -21,6 +21,7 @@ import java.net.DatagramSocket;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.SocketException; import java.net.SocketException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.concurrent.atomic.AtomicBoolean;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -29,25 +30,25 @@ public class UdpClient {
final Logger log = LoggerFactory.getLogger(UdpClient.class); final Logger log = LoggerFactory.getLogger(UdpClient.class);
private Statistics statistics; private final Statistics statistics;
private final int port; private final int port;
private final InetAddress address; private final InetAddress address;
private final DatagramSocket socket; private final DatagramSocket socket;
private final byte[] inBuffer = new byte[Payload.DEFAULT_LENGTH]; private final byte[] inBuffer = new byte[Payload.DEFAULT_LENGTH];
private final int packetCount; private final int length;
private final int packetSize; private final int packets;
private final int packeTime; private final int runtime;
public UdpClient(String hostname, int port, int size, int maxPackets, int maxTime) throws UnknownHostException, SocketException { public UdpClient(String hostname, int port, int length, int packets, int runtime) throws UnknownHostException, SocketException {
log.info("UdpClient() - target: {}, port: {}", hostname, port); log.info("UdpClient() - target: {}, port: {}", hostname, port);
this.port = port; this.port = port;
this.packetSize = size; this.length = length;
this.packetCount = maxPackets; this.packets = packets;
this.packeTime = maxTime; this.runtime = runtime;
socket = new DatagramSocket(); socket = new DatagramSocket();
address = InetAddress.getByName(hostname); address = InetAddress.getByName(hostname);
@ -62,7 +63,7 @@ public class UdpClient {
} }
private Payload receive() throws IOException { private Payload receive() throws IOException {
DatagramPacket packet = new DatagramPacket(inBuffer, inBuffer.length); DatagramPacket packet = new DatagramPacket(inBuffer, Payload.DEFAULT_LENGTH);
socket.receive(packet); socket.receive(packet);
return new Payload(inBuffer); return new Payload(inBuffer);
} }
@ -75,35 +76,51 @@ public class UdpClient {
public void start() throws IOException, InterruptedException { public void start() throws IOException, InterruptedException {
AtomicBoolean keepRunning = new AtomicBoolean(true);
Thread shutdownHook = new Thread(() -> {
keepRunning.set(false);
System.out.println("Stopping jnetperf, please wait ...");
});
Runtime.getRuntime().addShutdownHook(shutdownHook);
long sequence = 0; long sequence = 0;
// Send handshake // Send handshake
Payload payload = new Payload(PayloadType.HANDSHAKE.getValue(), Payload.DEFAULT_LENGTH, sequence++, packetCount); Payload payload = new Payload(PayloadType.HANDSHAKE.getValue(), Payload.DEFAULT_LENGTH, sequence++, packets);
send(payload); send(payload);
payload = receive(); payload = receive();
if(payload.getType() != PayloadType.ACK.getValue()) { if(payload.getType() != PayloadType.ACK.getValue()) {
log.warn("No ACK!"); log.warn("No ACK!");
return; return;
} }
// Data datagrams ... // Send data
for(int i = 0; i < packetCount; i++) { do {
payload = new Payload(PayloadType.DATA.getValue(), packetSize, sequence++, packetCount); payload = new Payload(PayloadType.DATA.getValue(), length, sequence++, packets);
send(payload); send(payload);
payload = receive(); payload = receive();
if(payload.getType() != PayloadType.ACK.getValue()) { if(payload.getType() != PayloadType.ACK.getValue()) {
log.warn("No ACK!"); log.warn("No ACK!");
} }
statistics.tick(); statistics.tick();
}
// End datagram if (sequence > packets) {
System.out.println("Max packets reached");
keepRunning.set(false);
}
if(runtime > 0 && statistics.getRuntime() > runtime) {
System.out.println("Max runtime reached");
keepRunning.set(false);
}
} while (keepRunning.get());
// Send end
//Thread.sleep(100); //Thread.sleep(100);
payload = new Payload(PayloadType.END.getValue(), packetSize, sequence++, packetCount); payload = new Payload(PayloadType.END.getValue(), length, sequence++, packets);
send(payload); send(payload);
// TODO: Wait for ACK
payload = receive(); payload = receive();
statistics.ack(); statistics.ack();
if(payload.getType() != PayloadType.ACK.getValue()) { if(payload.getType() != PayloadType.ACK.getValue()) {

View file

@ -28,25 +28,25 @@ public class UdpServer extends Thread {
final Logger log = LoggerFactory.getLogger(UdpServer.class); final Logger log = LoggerFactory.getLogger(UdpServer.class);
private final DatagramSocket socket; private final int port;
private DatagramSocket socket;
private byte[] inBuffer; private byte[] inBuffer;
public UdpServer(int port) throws SocketException { public UdpServer(int port) {
log.info("UdpServer()"); log.info("UdpServer()");
socket = new DatagramSocket(port); this.port = port;
} }
public void run() { public void run() {
boolean running = true;
try { try {
while (running) { while (true) {
inBuffer = new byte[Payload.DEFAULT_LENGTH]; inBuffer = new byte[Payload.DEFAULT_LENGTH];
socket = new DatagramSocket(port);
session(); session();
socket.close();
} }
socket.close();
} catch(IOException e) { } catch(IOException e) {
log.error(e.getMessage()); log.error(e.getMessage());
} }

View file

@ -6,7 +6,7 @@ import java.util.Locale;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class SuffixConverter implements CommandLine.ITypeConverter<Integer> { public class UnitSuffixConverter implements CommandLine.ITypeConverter<Integer> {
final private Pattern pattern = Pattern.compile("(\\d+)([kmg])?b?", Pattern.CASE_INSENSITIVE); final private Pattern pattern = Pattern.compile("(\\d+)([kmg])?b?", Pattern.CASE_INSENSITIVE);