changeset 660:92e6eadc7f0d

client-server - connect-disconnect loop
author Devel 1
date Mon, 13 Nov 2017 11:51:20 +0100
parents 827609bd0999
children e274cacdc4b8
files client-server/src/main/java/com/passus/st/clientserver/AbstractClient.java client-server/src/main/java/com/passus/st/clientserver/ChannelClient.java client-server/src/main/java/com/passus/st/clientserver/ClientSettings.java client-server/src/main/java/com/passus/st/clientserver/SocketClient.java
diffstat 4 files changed, 71 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/client-server/src/main/java/com/passus/st/clientserver/AbstractClient.java	Mon Nov 13 11:51:20 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/AbstractClient.java	Mon Nov 13 11:51:20 2017 +0100
@@ -1,5 +1,6 @@
 package com.passus.st.clientserver;
 
+import com.passus.st.clientserver.ClientSettings.Operation;
 import com.passus.st.utils.CliUtils;
 import com.passus.st.utils.MultiByteUtils;
 import java.io.IOException;
@@ -22,6 +23,7 @@
     static final float MEGA = 1024f * 1024f;
 
     static void addCommonOptions(Options options) {
+        options.addOption(CliUtils.createOption("op", "operation", "[conn-num] or conn-rate", "name"));
         options.addOption(CliUtils.createOption("p", "port", "remote port", "port"));
         options.addOption(CliUtils.createOption("t", "threads", "number of worker threads", "threads"));
         options.addOption(CliUtils.createOption("b", "bind", "bind before connect", null));
@@ -79,9 +81,11 @@
     }
 
     static void sleepSilently(long millis) {
-        try {
-            Thread.sleep(millis);
-        } catch (InterruptedException ignore) {
+        if (millis > 0) {
+            try {
+                Thread.sleep(millis);
+            } catch (InterruptedException ignore) {
+            }
         }
     }
 
@@ -99,6 +103,7 @@
         }
     }
 
+    final Operation operation;
     final byte[] msg;
     final Worker[] workers;
     final SocketAddress remote;
@@ -110,6 +115,7 @@
     final int connBurstTime;
 
     public AbstractClient(ClientSettings cs) {
+        operation = cs.operation;
         remote = new InetSocketAddress(cs.host, cs.port);
         host = cs.host;
         port = cs.port;
@@ -138,7 +144,18 @@
 
     abstract void receive(int bgn, int end) throws IOException;
 
-    void run(int bgn, int end, int rank) throws IOException {
+    void run(final int bgn, final int end, final int rank) throws IOException {
+        switch (operation) {
+            case CONN_SEND_RECV_DISC:
+                runConnSendRecvDisc(bgn, end, rank);
+                break;
+            case CONN_DISC_LOOP:
+                runConnDiscLoop(bgn, end, rank);
+                break;
+        }
+    }
+
+    void runConnSendRecvDisc(final int bgn, final int end, final int rank) throws IOException {
         String threadName = "[" + Thread.currentThread().getName() + "] ";
         long millis = System.currentTimeMillis();
         connect(bgn, end, rank);
@@ -174,6 +191,30 @@
         System.out.println(threadName + "closed in " + (System.currentTimeMillis() - millis));
     }
 
+    void runConnDiscLoop(final int bgn, final int end, final int rank) throws IOException {
+        String threadName = "[" + Thread.currentThread().getName() + "] ";
+        int currBgn = bgn;
+        long total = 0;
+
+        while (currBgn < end) {
+            int currEnd = Math.min(currBgn + connBurstSize, end);
+            long millis = System.currentTimeMillis();
+
+            connect(currBgn, currEnd, rank);
+            disconnect(currBgn, currEnd);
+            long duration = System.currentTimeMillis() - millis;
+            sleepSilently(connBurstTime);
+
+            float speed = connBurstSize / (duration / 1000.f);
+            total += duration;
+            System.out.println(threadName + " " + connBurstSize + " connections in " + duration + "ms (" + speed + " per second)");
+            currBgn = currEnd;
+        }
+
+        float avgSpeed = (end - bgn) / (total / 1000.f);
+        System.out.println(threadName + " " + (end - bgn) + " connections in " + total + "ms (" + avgSpeed + " per second)");
+    }
+
     void runWorkers() {
         for (Worker worker : workers) {
             worker.start();
--- a/client-server/src/main/java/com/passus/st/clientserver/ChannelClient.java	Mon Nov 13 11:51:20 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/ChannelClient.java	Mon Nov 13 11:51:20 2017 +0100
@@ -64,7 +64,6 @@
     void connect(int bgn, int end, int rank) {
         int errors = 0;
         int stride = workers.length > 0 ? workers.length : 1;
-        System.out.println("Connect: offset=" + rank + " stride=" + stride);
         int iii = rank;
 
         for (int i = bgn; i < end;) {
--- a/client-server/src/main/java/com/passus/st/clientserver/ClientSettings.java	Mon Nov 13 11:51:20 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/ClientSettings.java	Mon Nov 13 11:51:20 2017 +0100
@@ -8,6 +8,11 @@
  */
 public class ClientSettings {
 
+    enum Operation {
+        CONN_SEND_RECV_DISC, CONN_DISC_LOOP
+    }
+
+    final Operation operation;
     final String host;
     final int port;
     final int connections;
@@ -21,6 +26,7 @@
     final int connBurstTime;
 
     public ClientSettings(CommandLine cl, String[] clArgs) {
+        operation = getOperation(cl);
         host = clArgs[1];
         port = Integer.parseInt(cl.getOptionValue('p', "400"));
         connections = Integer.parseInt(clArgs[0]);
@@ -30,10 +36,29 @@
         localAddrNum = Integer.parseInt(cl.getOptionValue("lan", "10"));
         bind = cl.hasOption('b');
         flp = cl.hasOption("flp");
-        connBurstSize = Integer.parseInt(cl.getOptionValue("cbs", "1000"));
+        connBurstSize = Integer.parseInt(cl.getOptionValue("cbs", "100"));
         connBurstTime = Integer.parseInt(cl.getOptionValue("cbt", "0"));
     }
 
+    private static Operation getOperation(CommandLine cl) {
+        if (!cl.hasOption("op")) {
+            return Operation.CONN_SEND_RECV_DISC;
+        }
+
+        String value = cl.getOptionValue("op");
+        switch (value) {
+            case "csrd":
+            case "hold":
+            case "conn-num":
+                return Operation.CONN_SEND_RECV_DISC;
+            case "cdl":
+            case "conn-rate":
+                return Operation.CONN_DISC_LOOP;
+            default:
+                throw new IllegalArgumentException("Invalid operation: " + value);
+        }
+    }
+
     @Override
     public String toString() {
         return "conn=" + connections + " workers=" + workers + " host= " + host + " port=" + port
--- a/client-server/src/main/java/com/passus/st/clientserver/SocketClient.java	Mon Nov 13 11:51:20 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/SocketClient.java	Mon Nov 13 11:51:20 2017 +0100
@@ -62,7 +62,6 @@
         int errors = 0;
         if (bind) {
             int stride = workers.length > 0 ? workers.length : 1;
-            System.out.println("Connect: offset=" + rank + " stride=" + stride);
             int iii = rank;
             for (int i = bgn; i < end;) {
                 InetAddress localAddress = getAddressFor(iii);