changeset 1127:ae2d0e7c5f5b

Scanner configuration
author Devel 2
date Fri, 05 Jun 2020 14:36:09 +0200
parents ea20a5ad6d38
children abd8ab4d6b80
files stress-tester/src/main/java/com/passus/st/scanner/HttpScannerPayloadAnalyzer.java stress-tester/src/main/java/com/passus/st/scanner/HttpUrlStatsMetric.java stress-tester/src/main/java/com/passus/st/scanner/HttpUrlsStatsScannerPayloadAnalyzer.java stress-tester/src/main/java/com/passus/st/scanner/Scanner.java stress-tester/src/main/java/com/passus/st/scanner/ScannerAnalyzerConfigurator.java stress-tester/src/main/java/com/passus/st/scanner/ScannerAnalyzerFactory.java stress-tester/src/main/java/com/passus/st/scanner/ScannerAnalyzersNodeDefCreator.java stress-tester/src/main/java/com/passus/st/scanner/ScannerConfiguratorPluginFactory.java stress-tester/src/main/java/com/passus/st/scanner/ScannerJob.java stress-tester/src/main/java/com/passus/st/scanner/ScannerPayloadAnalyzer.java stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java
diffstat 11 files changed, 221 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/scanner/HttpScannerPayloadAnalyzer.java	Fri Jun 05 12:24:20 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/scanner/HttpScannerPayloadAnalyzer.java	Fri Jun 05 14:36:09 2020 +0200
@@ -13,9 +13,16 @@
 
 public class HttpScannerPayloadAnalyzer extends ScannerPayloadAnalyzer implements MetricSource {
 
+    public static final String TYPE = "http";
+
     private HttpMetric metric;
 
     @Override
+    public String getType() {
+        return TYPE;
+    }
+
+    @Override
     public void activate() {
         metric = new HttpMetric();
         metric.activate();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/scanner/HttpUrlStatsMetric.java	Fri Jun 05 14:36:09 2020 +0200
@@ -0,0 +1,38 @@
+package com.passus.st.scanner;
+
+import com.passus.commons.metric.Metric;
+import com.passus.st.client.GenericMetric;
+import org.apache.commons.lang3.mutable.MutableInt;
+
+import java.util.HashMap;
+
+public class HttpUrlStatsMetric extends GenericMetric {
+
+    public static final String DEFAULT_NAME = "HTTP URL stats";
+
+    private HashMap<String, MutableInt> urls = new HashMap<>();
+
+    public HttpUrlStatsMetric(String name) {
+        super(name);
+        attrs.put("urls", urls);
+    }
+
+    public HttpUrlStatsMetric() {
+        this(DEFAULT_NAME);
+    }
+
+    public void addUrl(String url) {
+        Metric.incrementCountMap(url, urls);
+    }
+
+    @Override
+    public void update(Metric metric) {
+        HttpUrlStatsMetric m = (HttpUrlStatsMetric) metric;
+        Metric.updateCountMap(urls, m.urls);
+    }
+
+    @Override
+    public void reset() {
+        urls.clear();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/scanner/HttpUrlsStatsScannerPayloadAnalyzer.java	Fri Jun 05 14:36:09 2020 +0200
@@ -0,0 +1,66 @@
+package com.passus.st.scanner;
+
+import com.passus.commons.annotations.Plugin;
+import com.passus.data.ByteString;
+import com.passus.net.http.HttpRequest;
+import com.passus.st.client.SessionPayloadEvent;
+import com.passus.st.metric.MetricSource;
+import com.passus.st.metric.MetricsContainer;
+import com.passus.st.plugin.PluginConstants;
+
+import static com.passus.st.Protocols.HTTP;
+
+@Plugin(name = HttpUrlsStatsScannerPayloadAnalyzer.TYPE, category = PluginConstants.CATEGORY_SCANNER_ANALYZER)
+public class HttpUrlsStatsScannerPayloadAnalyzer extends ScannerPayloadAnalyzer implements MetricSource {
+
+    public static final String TYPE = "httpUrlStats";
+
+    private HttpUrlStatsMetric metric;
+
+    @Override
+    public String getType() {
+        return TYPE;
+    }
+
+    @Override
+    public void activate() {
+        metric = new HttpUrlStatsMetric();
+        metric.activate();
+    }
+
+    @Override
+    public void deactivate() {
+        metric.deactivate();
+        metric = null;
+    }
+
+    @Override
+    public boolean isCollectMetrics() {
+        return false;
+    }
+
+    @Override
+    public void setCollectMetrics(boolean collectMetrics) {
+
+    }
+
+    @Override
+    public void writeMetrics(MetricsContainer container) {
+        container.update(metric);
+    }
+
+    @Override
+    protected void analyzePayload(SessionPayloadEvent event) {
+        if (event.getProtocolId() != HTTP) {
+            return;
+        }
+
+        HttpRequest req = (HttpRequest) event.getRequest();
+        if (req != null) {
+            ByteString uri = req.getUri();
+            metric.addUrl(uri.toString());
+        }
+    }
+
+
+}
\ No newline at end of file
--- a/stress-tester/src/main/java/com/passus/st/scanner/Scanner.java	Fri Jun 05 12:24:20 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/scanner/Scanner.java	Fri Jun 05 14:36:09 2020 +0200
@@ -81,10 +81,7 @@
             final long startTime = System.currentTimeMillis();
             MetricsCollector metricsCollector = job.getMetricsCollector();
 
-            if (metricsCollector != null) {
-                metricsCollector.addHandler(summMetricsHandler);
-            }
-
+            metricsCollector.addHandler(summMetricsHandler);
             final Logger logger = LogManager.getLogger("Scanner");
             logger.debug("Scanner started");
             job.start();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/scanner/ScannerAnalyzerConfigurator.java	Fri Jun 05 14:36:09 2020 +0200
@@ -0,0 +1,47 @@
+package com.passus.st.scanner;
+
+import com.passus.config.CCompositeNode;
+import com.passus.config.Configuration;
+import com.passus.config.ConfigurationContext;
+import com.passus.config.DomainConfigurator;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.validation.Errors;
+import com.passus.st.utils.ConfigurationContextConsts;
+
+import java.util.List;
+
+public class ScannerAnalyzerConfigurator implements DomainConfigurator {
+
+    public static final String DOMAIN = "analyzer";
+
+    private ScannerAnalyzersNodeDefCreator creator = new ScannerAnalyzersNodeDefCreator();
+
+    private final NodeDefinition eventSourceNodeDef = creator.create();
+
+    @Override
+    public String getDomain() {
+        return DOMAIN;
+    }
+
+    @Override
+    public void validate(Configuration config, Errors errors, ConfigurationContext context) {
+        CCompositeNode rootNode = config.getRootNode();
+        eventSourceNodeDef.validate(rootNode, errors, context);
+    }
+
+    @Override
+    public void configure(Configuration config, Errors errors, ConfigurationContext context) {
+        CCompositeNode rootNode = config.getRootNode();
+        eventSourceNodeDef.transform(rootNode, errors, context);
+        if (!errors.hasError()) {
+            List<ScannerAnalyzer> analyzer = (List<ScannerAnalyzer>) config.get("analyzers");
+            if (analyzer == null || analyzer.isEmpty()) {
+                errors.reject("At least one scanner analyzer should be defined.");
+                return;
+            }
+
+            context.add(ConfigurationContextConsts.SCANNER_ANALYZERS, analyzer);
+        }
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/scanner/ScannerAnalyzerFactory.java	Fri Jun 05 14:36:09 2020 +0200
@@ -0,0 +1,22 @@
+package com.passus.st.scanner;
+
+import com.passus.commons.plugin.PluginFactory;
+import com.passus.st.plugin.PluginConstants;
+
+public class ScannerAnalyzerFactory extends PluginFactory<ScannerAnalyzer> {
+
+    private static ScannerAnalyzerFactory instance;
+
+    private ScannerAnalyzerFactory() {
+        super(PluginConstants.CATEGORY_SCANNER_ANALYZER, ScannerAnalyzer.class);
+    }
+
+    public static synchronized ScannerAnalyzerFactory getInstance() {
+        if (instance == null) {
+            instance = new ScannerAnalyzerFactory();
+        }
+
+        return instance;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/scanner/ScannerAnalyzersNodeDefCreator.java	Fri Jun 05 14:36:09 2020 +0200
@@ -0,0 +1,30 @@
+package com.passus.st.scanner;
+
+import com.passus.commons.plugin.PluginFactory;
+import com.passus.config.schema.DynaKeyValueVaryListNodeDefinition;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
+
+import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
+
+public class ScannerAnalyzersNodeDefCreator implements NodeDefinitionCreator {
+
+    private PluginFactory<ScannerAnalyzer> factory = ScannerAnalyzerFactory.getInstance();
+
+    public NodeDefinition createScannerAnalyzersListDef() {
+        return createScannerAnalyzersListDef(false);
+    }
+
+    public NodeDefinition createScannerAnalyzersListDef(boolean transformToObject) {
+        return new DynaKeyValueVaryListNodeDefinition("type", factory)
+                .setTransformToPluginObject(transformToObject);
+    }
+
+    @Override
+    public NodeDefinition create() {
+        return mapDef(
+                tupleDef("analyzers", createScannerAnalyzersListDef(true))
+        );
+    }
+}
\ No newline at end of file
--- a/stress-tester/src/main/java/com/passus/st/scanner/ScannerConfiguratorPluginFactory.java	Fri Jun 05 12:24:20 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/scanner/ScannerConfiguratorPluginFactory.java	Fri Jun 05 14:36:09 2020 +0200
@@ -8,6 +8,7 @@
 
     public ScannerConfiguratorPluginFactory() {
         add(EventSourceConfigurator.DOMAIN, EventSourceConfigurator.class);
+        add(ScannerAnalyzerConfigurator.DOMAIN, ScannerAnalyzerConfigurator.class);
     }
 
 }
--- a/stress-tester/src/main/java/com/passus/st/scanner/ScannerJob.java	Fri Jun 05 12:24:20 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/scanner/ScannerJob.java	Fri Jun 05 14:36:09 2020 +0200
@@ -16,6 +16,7 @@
 import java.util.List;
 
 import static com.passus.st.utils.ConfigurationContextConsts.EVENT_SOURCE_SOURCES;
+import static com.passus.st.utils.ConfigurationContextConsts.SCANNER_ANALYZERS;
 
 public class ScannerJob implements Job {
 
@@ -109,6 +110,10 @@
 
         List<EventSource> cfgEventSources = context.get(EVENT_SOURCE_SOURCES);
         populateIfNotNull(cfgEventSources, job.eventSources);
+
+        List<ScannerAnalyzer> cfgAnalyzers = context.get(SCANNER_ANALYZERS);
+        populateIfNotNull(cfgAnalyzers, job.analyzers);
+
         return job;
     }
 
--- a/stress-tester/src/main/java/com/passus/st/scanner/ScannerPayloadAnalyzer.java	Fri Jun 05 12:24:20 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/scanner/ScannerPayloadAnalyzer.java	Fri Jun 05 14:36:09 2020 +0200
@@ -5,6 +5,8 @@
 
 public abstract class ScannerPayloadAnalyzer implements ScannerAnalyzer {
 
+    public abstract String getType();
+
     @Override
     public void analyze(Event event) {
         if (event.getType() == SessionPayloadEvent.TYPE) {
--- a/stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java	Fri Jun 05 12:24:20 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java	Fri Jun 05 14:36:09 2020 +0200
@@ -38,6 +38,8 @@
 
     public static final String REPORTER_DEFAULT_DESTINATION = "reporter.defaultDestination";
 
+    public static final String SCANNER_ANALYZERS = "scanner.analyzers";
+
     public static final String PROJECT = "project";
 
     public static final String FLOW_FILTERS = "flowFilters";