jnetperf/src/main/java/biz/nellemann/jperf/Application.java

76 lines
2.3 KiB
Java

/*
* This Java source file was generated by the Gradle 'init' task.
*/
package biz.nellemann.jperf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import java.io.IOException;
import java.net.SocketException;
import java.util.concurrent.Callable;
@Command(name = "jperf", mixinStandardHelpOptions = true, version = "0.1",
description = "Network performance measurement tool.")
public class Application implements Callable<Integer> {
final Logger log = LoggerFactory.getLogger(Application.class);
@CommandLine.Option(names = { "-c", "--connect" }, paramLabel = "SERVER", description = "run client and connect to remote server")
String remoteServer;
@CommandLine.Option(names = { "-s", "--server" }, description = "run server and wait for client")
boolean runServer = false;
@CommandLine.Option(names = { "-l", "--pkt-size" }, paramLabel = "SIZE", description = "datagram size in bytes, max 65507 [default: ${DEFAULT-VALUE}]")
//int packetSize = 16384; // Min: 256 Max: 65507
int packetSize = 65507; // Min: 256 Max: 65507
@CommandLine.Option(names = { "-n", "--pkt-num" }, paramLabel = "NUM", description = "number of packets to send [default: ${DEFAULT-VALUE}]")
int packetCount = 5000;
@CommandLine.Option(names = { "-p", "--port" }, paramLabel = "PORT", description = "network port [default: ${DEFAULT-VALUE}]")
int port = 4445;
@Override
public Integer call() throws Exception { // your business logic goes here...
if(runServer) {
runServer();
}
if(remoteServer != null) {
runClient(remoteServer);
}
return 0;
}
public static void main(String... args) {
int exitCode = new CommandLine(new Application()).execute(args);
System.exit(exitCode);
}
private void runClient(String remoteHost) throws InterruptedException, IOException {
UdpClient udpClient = new UdpClient(remoteHost, port, packetCount, packetSize);
udpClient.start();
}
private void runServer() throws SocketException, InterruptedException {
UdpServer udpServer = new UdpServer(port);
udpServer.start();
udpServer.join();
}
}