changeset 779:1d0369d5006b

StatsMain
author Devel 2
date Tue, 19 Dec 2017 12:28:25 +0100
parents 95f53a5be1be
children 6ae3b6a8bd5d
files stress-tester/src/main/java/com/passus/st/StatsMain.java stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataReader.java
diffstat 2 files changed, 290 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/StatsMain.java	Tue Dec 19 12:28:25 2017 +0100
@@ -0,0 +1,290 @@
+package com.passus.st;
+
+import com.passus.commons.metric.LongHistogramMetric;
+import com.passus.commons.metric.LongHistogramMetricImpl;
+import com.passus.data.ByteString;
+import com.passus.data.ByteStringImpl;
+import com.passus.data.PooledAllocator;
+import static com.passus.net.http.HttpHeaders.CONTENT_ENCODING;
+import static com.passus.net.http.HttpHeaders.TRANSFER_ENCODING;
+import com.passus.net.http.HttpMessage;
+import com.passus.net.http.HttpMessageHelper;
+import com.passus.net.http.HttpRequest;
+import com.passus.net.http.HttpResponse;
+import com.passus.st.client.Event;
+import com.passus.st.client.SessionStatusEvent;
+import com.passus.st.client.http.HttpSessionPayloadEvent;
+import com.passus.st.source.EventSource;
+import com.passus.st.source.NcEventSource;
+import com.passus.st.source.PcapSessionEventSource;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+import org.apache.commons.lang3.mutable.MutableInt;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class StatsMain {
+
+    private Logger logger;
+
+    private Stats stats = new Stats();
+
+    private final CliHelper cliHelper = new CliHelper();
+
+    private final HttpMessageHelper httpHelper = HttpMessageHelper.get();
+
+    private void printHelp(Options options) {
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp("[options] <file>", "description", options, "");
+    }
+
+    private Options createOptions() {
+        final CliOptions options = cliHelper.options();
+
+        options.addLogLevelOption();
+        options.addAllowPartialSessionOption();
+        options.addHttpPortsOption();
+
+        return options;
+    }
+
+    private void printStats() {
+        StringBuilder sb = new StringBuilder();
+
+        sb.append("Requests:\n");
+        sb.append("Count: ").append(stats.reqNum).append("\n");
+        sb.append("Content size: ").append(stats.reqContentSize).append("\n");
+        sb.append("Content size sum: ").append(stats.reqContentSizeSum).append(" B\n");
+        sb.append("Transfer encodings: ").append(stats.reqTransferEncodings).append("\n");
+        sb.append("Content encodings: ").append(stats.reqContentEncodings).append("\n");
+        sb.append("Encodings: ").append(stats.reqEncodings).append("\n");
+        sb.append("\n");
+
+        sb.append("Responses:\n");
+        sb.append("Count: ").append(stats.respNum).append("\n");
+        sb.append("Content size: ").append(stats.respContentSize).append("\n");
+        sb.append("Content size sum: ").append(stats.respContentSizeSum).append(" B\n");
+        sb.append("Transfer encodings: ").append(stats.respTransferEncodings).append("\n");
+        sb.append("Content encodings: ").append(stats.respContentEncodings).append("\n");
+        sb.append("Encodings: ").append(stats.respEncodings).append("\n");
+        sb.append("\n");
+
+        System.out.println(sb.toString());
+    }
+
+    private void stats(Event event) {
+        if (event.getType() == SessionStatusEvent.TYPE) {
+
+        } else if (event.getType() == HttpSessionPayloadEvent.TYPE) {
+            HttpSessionPayloadEvent payloadEvent = (HttpSessionPayloadEvent) event;
+            HttpRequest req = payloadEvent.getRequest();
+            HttpResponse resp = payloadEvent.getResponse();
+
+            if (req != null) {
+                stats.reqNum++;
+                stats.addContentEncoding(req);
+                stats.addTransferEncoding(req);
+                stats.addEncoding(req);
+                if (req.getContent() != null) {
+                    stats.reqContentSize.update(req.getContent().available());
+                    stats.reqContentSizeSum += req.getContent().available();
+                }
+            }
+
+            if (resp != null) {
+                stats.respNum++;
+                stats.addContentEncoding(resp);
+                stats.addTransferEncoding(resp);
+                stats.addEncoding(resp);
+                if (resp.getContent() != null) {
+                    stats.respContentSize.update(resp.getContent().available());
+                    stats.respContentSizeSum += resp.getContent().available();
+                }
+            }
+        }
+    }
+
+    private void readNcFile(NcEventSource src) throws Exception {
+        src.setParallel(false);
+        src.setAllocator(new PooledAllocator(64 * 1024, 100, 0));
+        src.setHandler((event) -> stats(event));
+
+        try {
+            src.start();
+        } finally {
+            src.stop();
+        }
+    }
+
+    private void readPcapFile(PcapSessionEventSource eventSrc) throws Exception {
+        eventSrc.setHandler((event) -> stats(event));
+        try {
+            eventSrc.start();
+            eventSrc.join();
+        } finally {
+            eventSrc.stop();
+        }
+    }
+
+    public void start(String[] args) {
+        AppUtils.registerAll();
+        Options options = createOptions();
+
+        try {
+            CommandLine cl = new DefaultParser().parse(options, args);
+            String[] clArgs = cl.getArgs();
+            if (clArgs.length != 1) {
+                System.err.println("Source file required.");
+                printHelp(options);
+                return;
+            }
+
+            cliHelper.configureLogger(cl);
+            logger = LogManager.getLogger(ReaderMain.class);
+
+            EventSource eventSource = cliHelper.createEventSource(clArgs[0], cl);
+
+            if (eventSource instanceof NcEventSource) {
+                readNcFile((NcEventSource) eventSource);
+            } else if (eventSource instanceof PcapSessionEventSource) {
+                readPcapFile((PcapSessionEventSource) eventSource);
+            } else {
+                cliHelper.printError("Not supported event source type '" + eventSource.getType() + "'.");
+            }
+
+            printStats();
+        } catch (Exception e) {
+            e.printStackTrace(System.err);
+        } finally {
+            AppUtils.unregisterAll();
+        }
+    }
+
+    public static void main(String[] args) {
+        new StatsMain().start(args);
+    }
+
+    private class Stats {
+
+        private int reqNum = 0;
+
+        private int respNum = 0;
+
+        private long reqContentSizeSum = 0;
+
+        private long respContentSizeSum = 0;
+
+        private LongHistogramMetric reqContentSize = new LongHistogramMetricImpl(1024);
+
+        private LongHistogramMetric respContentSize = new LongHistogramMetricImpl(1024);
+
+        private Map<ByteString, MutableInt> reqContentEncodings = new HashMap<>();
+
+        private Map<ByteString, MutableInt> respContentEncodings = new HashMap<>();
+
+        private Map<ByteString, MutableInt> reqTransferEncodings = new HashMap<>();
+
+        private Map<ByteString, MutableInt> respTransferEncodings = new HashMap<>();
+
+        private Map<ByteString, MutableInt> reqEncodings = new HashMap<>();
+
+        private Map<ByteString, MutableInt> respEncodings = new HashMap<>();
+
+        public void addEncoding(HttpMessage message) {
+            ByteString encoding = null;
+
+            ByteString transferEncoding = message.getHeaders().get(TRANSFER_ENCODING);
+            if (transferEncoding != null) {
+                if (encoding == null) {
+                    encoding = new ByteStringImpl(transferEncoding);
+                }
+            }
+
+            ByteString contentEncoding = message.getHeaders().get(CONTENT_ENCODING);
+            if (contentEncoding != null) {
+                if (encoding == null) {
+                    encoding = new ByteStringImpl(contentEncoding);
+                } else {
+                    encoding = encoding.concat(", ").concat(contentEncoding);
+                }
+            }
+
+            if (message.isRequest()) {
+                addEncoding(encoding, reqEncodings);
+            } else {
+                addEncoding(encoding, respEncodings);
+            }
+        }
+
+        private void addEncoding(ByteString encoding, Map<ByteString, MutableInt> encodings) {
+            if (encoding == null) {
+                return;
+            }
+
+            MutableInt count = encodings.get(encoding);
+            if (count == null) {
+                count = new MutableInt(1);
+                encodings.put(encoding, count);
+            } else {
+                count.increment();
+            }
+        }
+
+        public void addContentEncoding(HttpMessage message) {
+            ByteString contentEncoding = message.getHeaders().get(CONTENT_ENCODING);
+            if (message.isRequest()) {
+                addContentEncoding(contentEncoding, reqContentEncodings);
+            } else {
+                addContentEncoding(contentEncoding, respContentEncodings);
+            }
+        }
+
+        private void addContentEncoding(ByteString contentEncoding, Map<ByteString, MutableInt> contentEncodings) {
+            if (contentEncoding == null) {
+                return;
+            }
+
+            MutableInt count = contentEncodings.get(contentEncoding);
+            if (count == null) {
+                count = new MutableInt(1);
+                contentEncodings.put(contentEncoding, count);
+            } else {
+                count.increment();
+            }
+        }
+
+        public void addTransferEncoding(HttpMessage message) {
+            ByteString transferEncoding = message.getHeaders().get(TRANSFER_ENCODING);
+            if (transferEncoding != null) {
+                if (message.isRequest()) {
+                    addTransferEncoding(transferEncoding, reqTransferEncodings);
+                } else {
+                    addTransferEncoding(transferEncoding, respTransferEncodings);
+                }
+            }
+        }
+
+        private void addTransferEncoding(ByteString transferEncoding, Map<ByteString, MutableInt> transferEncodings) {
+            if (transferEncoding == null) {
+                return;
+            }
+
+            MutableInt count = transferEncodings.get(transferEncoding);
+            if (count == null) {
+                count = new MutableInt(1);
+                transferEncodings.put(transferEncoding, count);
+            } else {
+                count.increment();
+            }
+        }
+    }
+
+}
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataReader.java	Mon Dec 18 23:56:27 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/HttpSessionPayloadEventDataReader.java	Tue Dec 19 12:28:25 2017 +0100
@@ -7,7 +7,6 @@
 import com.passus.data.ByteString;
 import com.passus.data.DataSource;
 import com.passus.data.DefaultAllocator;
-import com.passus.data.HeapByteBuff;
 import com.passus.net.http.HttpCachedHeadersImpl;
 import com.passus.net.http.HttpConsts;
 import com.passus.net.http.HttpHeaders;