changeset 585:ecdae4e189ee

ReporterFileDestination req-resp implemented
author Devel 1
date Mon, 02 Oct 2017 16:36:30 +0200
parents 5f07f000f00f
children 7e7e22b98955
files stress-tester/src/main/java/com/passus/st/Main.java stress-tester/src/main/java/com/passus/st/client/http/ReporterFileDestination.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpSequenceFilter.java
diffstat 3 files changed, 169 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/Main.java	Mon Oct 02 16:13:43 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/Main.java	Mon Oct 02 16:36:30 2017 +0200
@@ -16,6 +16,7 @@
 import com.passus.st.client.http.ReporterRemoteDestination;
 import com.passus.st.client.http.HttpSourceNameAwareClientWorkerDispatcher;
 import com.passus.st.client.http.ReporterDestination;
+import com.passus.st.client.http.ReporterFileDestination;
 import com.passus.st.client.http.SummaryHttpClientListener;
 import com.passus.st.client.http.WriterHttpClientListener;
 import com.passus.st.client.http.filter.HttpFiltersConfigurator;
@@ -159,6 +160,10 @@
                 .build()
         );
 
+        options.addOption(option("rd", "reporterDirectory").desc("Reporter directory.")
+                .hasArg().argName("ip")
+                .build()
+        );
         options.addOption(option("ri", "reporterIp").desc("Reporter ip address.")
                 .hasArg().argName("ip")
                 .build()
@@ -311,7 +316,13 @@
                 }
                 reporterClient.start();
 
-                ReporterRemoteDestination reporterDestination = new ReporterRemoteDestination(reporterClient);
+                ReporterDestination reporterDestination = new ReporterRemoteDestination(reporterClient);
+                Registry.getInstance().add(ReporterDestination.SERVICE_NAME, reporterDestination);
+                client.addListener(reporterDestination);
+                collector.addHandler(reporterDestination);
+            } else if (cl.hasOption("rd")) {
+                // TODO: refactor
+                ReporterDestination reporterDestination = new ReporterFileDestination(cl.getOptionValue("rd"));
                 Registry.getInstance().add(ReporterDestination.SERVICE_NAME, reporterDestination);
                 client.addListener(reporterDestination);
                 collector.addHandler(reporterDestination);
--- a/stress-tester/src/main/java/com/passus/st/client/http/ReporterFileDestination.java	Mon Oct 02 16:13:43 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/ReporterFileDestination.java	Mon Oct 02 16:36:30 2017 +0200
@@ -1,26 +1,143 @@
 package com.passus.st.client.http;
 
 import com.passus.commons.metric.MapMetric;
+import com.passus.commons.metric.Metric;
 import com.passus.commons.metric.MetricsCollection;
+import com.passus.data.ByteString;
+import com.passus.net.SocketAddress;
 import com.passus.net.http.HttpRequest;
 import com.passus.net.http.HttpResponse;
+import static com.passus.st.client.http.HttpConsts.TAG_CONTENT_SIZE;
+import static com.passus.st.client.http.HttpConsts.TAG_HEADER_SIZE;
+import static com.passus.st.client.http.HttpConsts.TAG_TIME_END;
+import static com.passus.st.client.http.HttpConsts.TAG_TIME_START;
+import static com.passus.st.client.http.ReporterDestination.ALLOWED_REQ_HEADERS;
+import static com.passus.st.client.http.ReporterDestination.ALLOWED_RESP_HEADERS;
+import com.passus.st.emitter.SessionInfo;
+import com.passus.st.reporter.ReporterImpl;
+import static com.passus.st.reporter.ReporterImpl.addValue;
 import java.io.File;
 import java.io.IOException;
-
+import java.io.PrintWriter;
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Map;
 
 public class ReporterFileDestination implements ReporterDestination {
 
+    private final PrintWriter reqFile;
+    private final PrintWriter respFile;
+    private final PrintWriter emitterFile;
+
     public ReporterFileDestination(String directory) throws IOException {
-        
+        this(new File(directory));
     }
 
     public ReporterFileDestination(File directory) throws IOException {
-        
+        if (!directory.exists()) {
+            directory.mkdirs();
+        }
+
+        if (!directory.isDirectory()) {
+            throw new IOException("Provided path is not a directory.");
+        }
+
+        reqFile = new PrintWriter(new File(directory, ReporterImpl.REQ_FILE), ReporterImpl.CHARSET);
+        respFile = new PrintWriter(new File(directory, ReporterImpl.RESP_FILE), ReporterImpl.CHARSET);
+        emitterFile = new PrintWriter(new File(directory, ReporterImpl.EMITTER_FILE), ReporterImpl.CHARSET);
     }
 
     @Override
     public void responseReceived(HttpRequest request, HttpResponse response, HttpFlowContext context) {
-        throw new UnsupportedOperationException("Not supported yet.");
+        try {
+            if (request == null && response == null) {
+                return;
+            }
+
+            SessionInfo session = context.sessionInfo();
+
+            String clientIp;
+            int clientPort;
+            String serverIp;
+            int serverPort;
+            if (context.channelContext != null) {
+                SocketAddress localAddress = context.channelContext.getLocalAddress();
+                SocketAddress remoteAddress = context.channelContext.getRemoteAddress();
+                clientIp = localAddress.getIp().toString();
+                clientPort = localAddress.getPort();
+                serverIp = remoteAddress.getIp().toString();
+                serverPort = remoteAddress.getPort();
+            } else {
+                clientIp = session.getSrcIp().toString();
+                clientPort = session.getSrcPort();
+                serverIp = session.getDstIp().toString();
+                serverPort = session.getDstPort();
+            }
+//            String origClientIp = session.getSrcIp().toString();
+//            int origClientPort = session.getSrcPort();
+//            String origServerIp = session.getDstIp().toString();
+//            int origServerPort = session.getDstPort();
+
+            StringBuilder builder = new StringBuilder();
+            String reqId = "";
+            if (request != null) {
+                reqId = request.getId();
+                addValue(builder, request.getId());
+                addValue(builder, request.getMethod().toString());
+                addValue(builder, request.getVersion().toString());
+                addValue(builder, request.getUrl().toString());
+                addValue(builder, request.getTag(TAG_TIME_START));
+                addValue(builder, request.getTag(TAG_TIME_END));
+                addValue(builder, serverIp);
+                addValue(builder, serverPort);
+                addValue(builder, clientIp);
+                addValue(builder, clientPort);
+                addValue(builder, request.getTag(TAG_HEADER_SIZE));
+                addValue(builder, request.getTag(TAG_CONTENT_SIZE));
+
+                Map<ByteString, ByteString> reqHdrs = new HashMap<>();
+                ReporterRemoteDestination.populateHeaders(reqHdrs, ALLOWED_REQ_HEADERS, request);
+                addValue(builder, getValue(reqHdrs, "User-Agent"));
+
+                HashMap<String, String> misc = new HashMap<>();
+                ReporterRemoteDestination.populateMisc(misc, context, request);
+                addValue(builder, misc.get("sessionId"));
+                addValue(builder, misc.get("username"));
+            } else {
+                addValue(builder, null);
+            }
+
+            synchronized (reqFile) {
+                reqFile.println(builder.toString());
+                reqFile.flush();
+            }
+            builder.setLength(0);
+
+            if (response != null) {
+                addValue(builder, reqId);
+                addValue(builder, response.getStatus().getReasonPhrase().toString());
+                addValue(builder, response.getStatus().getCode());
+                addValue(builder, response.getTag(TAG_TIME_START));
+                addValue(builder, response.getTag(TAG_TIME_END));
+
+                HashMap<ByteString, ByteString> respHdrs = new HashMap<>();
+                ReporterRemoteDestination.populateHeaders(respHdrs, ALLOWED_RESP_HEADERS, response);
+                addValue(builder, getValue(respHdrs, "Content-Type"));
+
+                addValue(builder, response.getTag(TAG_HEADER_SIZE));
+                addValue(builder, response.getTag(TAG_CONTENT_SIZE));
+            } else {
+                addValue(builder, reqId);
+            }
+
+            synchronized (respFile) {
+                respFile.println(builder.toString());
+                respFile.flush();
+            }
+            builder.setLength(0);
+        } catch (Throwable t) {
+            t.printStackTrace();
+        }
     }
 
     @Override
@@ -28,7 +145,39 @@
     }
 
     @Override
-    public void handle(MetricsCollection mc) {
+    public void handle(MetricsCollection collection) {
+        StringBuilder builder = new StringBuilder();
+
+        addValue(builder, collection.getStartTimestamp());
+        addValue(builder, collection.getEndTimestamp());
+
+        Metric metric = collection.getMetric("emitter");
+        if (metric != null) {
+            Map<String, Serializable> fields = metric.getAttributesValue();
+            addValue(builder, fields.get("receivedBytes"));
+            addValue(builder, fields.get("sentBytes"));
+            addValue(builder, fields.get("establishedConnections"));
+            addValue(builder, fields.get("closedConnections"));
+            addValue(builder, fields.get("bindErrors"));
+        } else {
+            builder.append(";;;;;");
+        }
+
+        metric = collection.getMetric("pcapSource");
+        if (metric != null) {
+            Map<String, Serializable> fields = metric.getAttributesValue();
+            addValue(builder, fields.get("frames"));
+            addValue(builder, fields.get("tcpPackets"));
+        } else {
+            builder.append(";;");
+        }
+
+        emitterFile.println(builder.toString());
+        emitterFile.flush();
     }
-    
+
+    private static ByteString getValue(Map<ByteString, ByteString> map, String key) {
+        return map.get(ByteString.create(key));
+    }
+
 }
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpSequenceFilter.java	Mon Oct 02 16:13:43 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpSequenceFilter.java	Mon Oct 02 16:36:30 2017 +0200
@@ -379,8 +379,8 @@
 
         values = (Map<String, ValueExtractor>) config.get("values", Collections.EMPTY_MAP);
 
-        ReporterRemoteDestination reporterWrapper = Registry.getInstance().get(
-                ReporterDestination.SERVICE_NAME, ReporterRemoteDestination.class);
+        ReporterDestination reporterWrapper = Registry.getInstance().get(
+                ReporterDestination.SERVICE_NAME, ReporterDestination.class);
         if (reporterWrapper != null) {
             listener = reporterWrapper;
         }