changeset 755:2105a00fae90

CliHelper improvements
author Devel 2
date Wed, 13 Dec 2017 14:55:15 +0100
parents 66fe741beb94
children 2042253e3dab
files stress-tester/src/main/java/com/passus/st/CliHelper.java stress-tester/src/main/java/com/passus/st/CliOptions.java stress-tester/src/main/java/com/passus/st/Main.java stress-tester/src/main/java/com/passus/st/Main2.java stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFiltersConfigurator.java
diffstat 5 files changed, 55 insertions(+), 82 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/CliHelper.java	Wed Dec 13 14:54:32 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/CliHelper.java	Wed Dec 13 14:55:15 2017 +0100
@@ -7,8 +7,10 @@
 import static com.passus.config.validation.ErrorsUtils.objectErrorToString;
 import com.passus.config.validation.ObjectError;
 import com.passus.net.PortRangeSet;
+import com.passus.st.client.http.HttpClient;
 import com.passus.st.client.http.filter.HttpFilter;
 import com.passus.st.client.http.filter.HttpFiltersConfigurator;
+import com.passus.st.emitter.Emitter;
 import com.passus.st.emitter.PassThroughSessionMapper;
 import com.passus.st.emitter.RuleBasedSessionMapper;
 import com.passus.st.emitter.SessionMapper;
@@ -17,8 +19,11 @@
 import com.passus.st.source.EventSource;
 import com.passus.st.source.NcEventSource;
 import com.passus.st.source.PcapSessionEventSource;
+import com.passus.st.vars.HttpVarsFilter;
 import java.io.File;
 import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.LinkedList;
 import java.util.List;
 import org.apache.commons.cli.CommandLine;
 import static org.apache.commons.io.FileUtils.openInputStream;
@@ -128,6 +133,24 @@
         }
     }
 
+    public HttpClient createHttpCliet(Emitter emitter, CommandLine cl) {
+        HttpClient client = new HttpClient(emitter);
+        client.setCollectMetrics(true);
+        client.setConnectPartialSession(cl.hasOption("ps"));
+        client.setWokerType(cl.getOptionValue("wt", "synch"));
+
+        if (cl.hasOption("rs")) {
+            float speed = Float.parseFloat(cl.getOptionValue("rs"));
+            if (speed > .1f && speed < 100f) {
+                client.setSleepFactor(1f / speed);
+            } else if (speed != 0f) {
+                throw new IllegalArgumentException();
+            }
+        }
+
+        return client;
+    }
+
     public List<HttpFilter> readHttpFilters(CommandLine cl) throws Exception {
         if (cl.hasOption("ff")) {
             File filtersFile = new File(cl.getOptionValue("ff"));
@@ -148,4 +171,15 @@
 
         return null;
     }
+
+    public List<HttpFilter> readHttpClientFilters(CommandLine cl) throws Exception {
+        List<HttpFilter> filters = readHttpFilters(cl);
+        if (filters == null) {
+            filters = new ArrayList<>();
+        }
+
+        filters.add(new HttpVarsFilter());
+        return filters;
+    }
+
 }
--- a/stress-tester/src/main/java/com/passus/st/CliOptions.java	Wed Dec 13 14:54:32 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/CliOptions.java	Wed Dec 13 14:55:15 2017 +0100
@@ -75,6 +75,16 @@
 
     public CliOptions addHttpClientOptions() {
         addHttpFiltersFileOption();
+        
+        addOption(option("rs", "replaySpeed").desc("Speedup factor (default 0 - top speed)")
+                .hasArg().argName("speed")
+                .build());
+        
+        addOption(option("wt", "workerType").desc("Worker type: synch|asynch|parallel (default synch).")
+                .hasArg().argName("type")
+                .build()
+        );
+        
         return addHttpPortsOption();
     }
     
--- a/stress-tester/src/main/java/com/passus/st/Main.java	Wed Dec 13 14:54:32 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/Main.java	Wed Dec 13 14:55:15 2017 +0100
@@ -4,11 +4,6 @@
 import com.passus.commons.metric.MapMetric;
 import com.passus.commons.metric.Metric;
 import com.passus.commons.service.Registry;
-import com.passus.config.Configuration;
-import com.passus.config.YamlConfigurationReader;
-import com.passus.config.validation.Errors;
-import static com.passus.config.validation.ErrorsUtils.objectErrorToString;
-import com.passus.config.validation.ObjectError;
 import com.passus.st.client.MemoryEventsCache;
 import com.passus.st.client.http.DumperHttpClientListener;
 import com.passus.st.client.http.HttpClient;
@@ -19,7 +14,6 @@
 import com.passus.st.client.http.SummaryHttpClientListener;
 import com.passus.st.client.http.WriterHttpClientListener;
 import com.passus.st.client.http.filter.HttpFilter;
-import com.passus.st.client.http.filter.HttpFiltersConfigurator;
 import com.passus.st.emitter.SessionMapper;
 import com.passus.st.emitter.nio.NioEmitter;
 import com.passus.st.metric.FileMetricsCollectionAppender;
@@ -109,10 +103,6 @@
         options.addAllowPartialSessionOption();
         options.addHttpClientOptions();
 
-        options.addOption(option("rs", "replaySpeed").desc("Speedup factor (default 0 - top speed)")
-                .hasArg().argName("speed")
-                .build());
-
         options.addOption(option("pr", "parallelReplays").desc("Number of parallel replays. Works only for one pcap file.")
                 .hasArg().argName("replays")
                 .build());
@@ -126,11 +116,6 @@
                 .build()
         );
 
-        options.addOption(option("wt", "workerType").desc("Worker type: synch|asynch|parallel (default synch).")
-                .hasArg().argName("type")
-                .build()
-        );
-
         options.addOption(option("rd", "reporterDirectory").desc("Reporter directory.")
                 .hasArg().argName("ip")
                 .build()
@@ -196,10 +181,7 @@
             emitter.setSessionMapper(mapper);
             emitter.setCollectMetrics(true);
 
-            HttpClient client = new HttpClient(emitter);
-            client.setCollectMetrics(true);
-            client.setConnectPartialSession(cl.hasOption("ps"));
-            client.setWokerType(cl.getOptionValue("wt", "synch"));
+            HttpClient client = cliHelper.createHttpCliet(emitter, cl);
             client.addListener((request, response, context) -> {
                 if (startTime == 0) {
                     startTime = System.currentTimeMillis();
@@ -223,15 +205,6 @@
             }
             emitter.start();
 
-            if (cl.hasOption("rs")) {
-                float speed = Float.parseFloat(cl.getOptionValue("rs"));
-                if (speed > .1f && speed < 100f) {
-                    client.setSleepFactor(1f / speed);
-                } else if (speed != 0f) {
-                    throw new IllegalArgumentException();
-                }
-            }
-
             if (cl.hasOption("wf")) {
                 WriterHttpClientListener writerListener;
                 String value = cl.getOptionValue("wf");
@@ -290,7 +263,7 @@
                 collector.addHandler(reporterDestination);
             }
 
-            List<HttpFilter> httpFilters = cliHelper.readHttpFilters(cl);
+            List<HttpFilter> httpFilters = cliHelper.readHttpClientFilters(cl);
             if (httpFilters != null) {
                 httpFilters.forEach((filter) -> client.addFilter(filter));
             }
--- a/stress-tester/src/main/java/com/passus/st/Main2.java	Wed Dec 13 14:54:32 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/Main2.java	Wed Dec 13 14:55:15 2017 +0100
@@ -4,11 +4,6 @@
 import com.passus.commons.metric.MapMetric;
 import com.passus.commons.metric.Metric;
 import com.passus.commons.service.Registry;
-import com.passus.config.Configuration;
-import com.passus.config.YamlConfigurationReader;
-import com.passus.config.validation.Errors;
-import static com.passus.config.validation.ErrorsUtils.objectErrorToString;
-import com.passus.config.validation.ObjectError;
 import com.passus.net.PortRangeSet;
 import com.passus.st.client.MemoryEventsCache;
 import com.passus.st.client.http.DumperHttpClientListener;
@@ -20,9 +15,6 @@
 import com.passus.st.client.http.SummaryHttpClientListener;
 import com.passus.st.client.http.WriterHttpClientListener;
 import com.passus.st.client.http.filter.HttpFilter;
-import com.passus.st.client.http.filter.HttpFiltersConfigurator;
-import com.passus.st.emitter.PassThroughSessionMapper;
-import com.passus.st.emitter.RuleBasedSessionMapper;
 import com.passus.st.emitter.SessionMapper;
 import com.passus.st.emitter.nio.NioEmitter;
 import com.passus.st.emitter.nio.NioEmitterWorker2;
@@ -109,36 +101,12 @@
 
     void start(String... args) {
         AppUtils.registerAll();
-        final Options options = new Options();
-
-        options.addOption(option("l", "logLevel").desc("Log level.")
-                .hasArg().argName("level")
-                .build()
-        );
-
-        options.addOption(option("ff", "filtersFile").desc("Filters file.")
-                .hasArg().argName("file")
-                .build()
-        );
+        final CliOptions options = cliHelper.options();
 
-        options.addOption(option("mr", "mapperRule").desc("Session mapper rule.")
-                .hasArg().argName("rule")
-                .build()
-        );
-
-        options.addOption(option("ps", "allowPartialSession").desc("Allow partial sessions.")
-                .hasArg(false)
-                .build()
-        );
-
-        options.addOption(option("hp", "httpPorts").desc("Specify HTTP ports in input file (default: 80, 8080)")
-                .hasArg().argName("ports")
-                .build()
-        );
-
-        options.addOption(option("rs", "replaySpeed").desc("Speedup factor (default 0 - top speed)")
-                .hasArg().argName("speed")
-                .build());
+        options.addLogLevelOption();
+        options.addMapperRuleOption();
+        options.addAllowPartialSessionOption();
+        options.addHttpClientOptions();
 
         options.addOption(option("pr", "parallelReplays").desc("Number of parallel replays. Works only for one pcap file.")
                 .hasArg().argName("replays")
@@ -207,10 +175,7 @@
             emitter.setSessionMapper(mapper);
             emitter.setCollectMetrics(true);
 
-            HttpClient client = new HttpClient(emitter);
-            client.setCollectMetrics(true);
-            client.setConnectPartialSession(cl.hasOption("ps"));
-            client.setWokerType(cl.getOptionValue("wt", "synch"));
+            HttpClient client = cliHelper.createHttpCliet(emitter, cl);
             client.addListener((request, response, context) -> {
                 if (startTime == 0) {
                     startTime = System.currentTimeMillis();
@@ -234,15 +199,6 @@
             }
             emitter.start();
 
-            if (cl.hasOption("rs")) {
-                float speed = Float.parseFloat(cl.getOptionValue("rs"));
-                if (speed > .1f && speed < 100f) {
-                    client.setSleepFactor(1f / speed);
-                } else if (speed != 0f) {
-                    throw new IllegalArgumentException();
-                }
-            }
-
             if (cl.hasOption("wf")) {
                 WriterHttpClientListener writerListener;
                 String value = cl.getOptionValue("wf");
@@ -301,7 +257,7 @@
                 collector.addHandler(reporterDestination);
             }
 
-            List<HttpFilter> httpFilters = cliHelper.readHttpFilters(cl);
+            List<HttpFilter> httpFilters = cliHelper.readHttpClientFilters(cl);
             if (httpFilters != null) {
                 httpFilters.forEach((filter) -> client.addFilter(filter));
             }
--- a/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFiltersConfigurator.java	Wed Dec 13 14:54:32 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/filter/HttpFiltersConfigurator.java	Wed Dec 13 14:55:15 2017 +0100
@@ -27,7 +27,7 @@
 
     private static final ConfigurationSchema SCHEMA = new ConfigurationSchemaImpl(new HttpFiltersNodeDefinitionCreator().create());
 
-    private final List<HttpFilter> filters = Collections.EMPTY_LIST;
+    private List<HttpFilter> filters;
 
     public List<HttpFilter> getFilters() {
         return filters;
@@ -81,7 +81,7 @@
         List<HttpFilter> cfgFilters = getFilters(config, errors, context);
         filters.clear();
         if (errors.getErrorCount() == 0) {
-            filters.addAll(cfgFilters);
+            filters = new ArrayList<>(cfgFilters);
         }
     }