changeset 870:84c93f726188

TestJob configuration in progress
author Devel 2
date Thu, 25 Jan 2018 12:58:42 +0100
parents 6cba3257c006
children 56e55f320734
files stress-tester/src/main/java/com/passus/st/Builder.java stress-tester/src/main/java/com/passus/st/CliHelper.java stress-tester/src/main/java/com/passus/st/job/TestJob.java stress-tester/src/main/java/com/passus/st/job/TestJobBuilder.java stress-tester/src/main/java/com/passus/st/lookup/LookupsDomainConfigurator.java stress-tester/src/main/java/com/passus/st/reader/pcap/PcapFileHeader.java stress-tester/src/main/java/com/passus/st/vars/VarsGlobalDomainConfigurator.java stress-tester/src/main/java/com/passus/st/vars/VarsNodeDefinitionCreator.java stress-tester/src/test/resources/com/passus/st/config/test_job_config.yml
diffstat 9 files changed, 284 insertions(+), 14 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/Builder.java	Thu Jan 25 12:58:42 2018 +0100
@@ -0,0 +1,12 @@
+package com.passus.st;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ * @param <T>
+ */
+public interface Builder<T> {
+
+    public T build();
+
+}
--- a/stress-tester/src/main/java/com/passus/st/CliHelper.java	Thu Jan 25 12:53:30 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/CliHelper.java	Thu Jan 25 12:58:42 2018 +0100
@@ -133,7 +133,7 @@
 
         if (NcHeader.isPreambule(preambule, 0)) {
             return new NcEventSource(fileName);
-        } else if (PcapFileHeader.isFormatSupported(preambule)) {
+        } else if (PcapFileHeader.isPreambule(preambule, 0)) {
             return createPcapEventSource(fileName, cl);
         } else {
             printError("Not supported file format '" + fileName + "'.");
--- a/stress-tester/src/main/java/com/passus/st/job/TestJob.java	Thu Jan 25 12:53:30 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/job/TestJob.java	Thu Jan 25 12:58:42 2018 +0100
@@ -1,28 +1,42 @@
 package com.passus.st.job;
 
-import com.passus.config.ConfigurationContext;
+import com.passus.st.client.Client;
+import com.passus.st.client.http.ReporterDestination;
+import com.passus.st.emitter.Emitter;
+import com.passus.st.source.EventSource;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import java.util.ArrayList;
+import java.util.List;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 public class TestJob implements Job {
 
+    private static final Logger LOGGER = LogManager.getLogger(TestJob.class);
+
+    private List<EventSource> eventSources = new ArrayList<>();
+
+    private List<Client> clients = new ArrayList<>();
+
+    private List<Emitter> emitters = new ArrayList<>();
+
+    private List<ReporterDestination> reporterDestinations = new ArrayList<>();
+
     public TestJob() {
     }
 
-    public TestJob(ConfigurationContext context) {
-
-    }
-
     @Override
     public void start() {
-        throw new UnsupportedOperationException("Not supported yet.");
+        clients.forEach(Client::start);
     }
 
     @Override
     public void stop() {
-        throw new UnsupportedOperationException("Not supported yet.");
+        clients.forEach(Client::stop);
+        emitters.forEach(Emitter::stop);
     }
-    
+
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/job/TestJobBuilder.java	Thu Jan 25 12:58:42 2018 +0100
@@ -0,0 +1,195 @@
+package com.passus.st.job;
+
+import com.passus.config.Configuration;
+import com.passus.config.ConfigurationContext;
+import com.passus.config.validation.Errors;
+import com.passus.st.Builder;
+import com.passus.st.client.Client;
+import com.passus.st.client.http.HttpClient;
+import com.passus.st.client.http.ReporterDestination;
+import com.passus.st.config.TestJobConfigurator;
+import com.passus.st.emitter.Emitter;
+import com.passus.st.emitter.nio.NioEmitter;
+import com.passus.st.metric.MetricsCollector;
+import com.passus.st.metric.ScheduledMetricsCollector;
+import com.passus.st.reader.nc.NcHeader;
+import com.passus.st.reader.pcap.PcapFileHeader;
+import com.passus.st.source.EventSource;
+import com.passus.st.source.NcEventSource;
+import com.passus.st.source.PcapSessionEventSource;
+import org.apache.commons.io.IOUtils;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+import static com.passus.st.utils.ConfigurationContextConsts.*;
+import static org.apache.commons.io.FileUtils.openInputStream;
+
+public class TestJobBuilder implements Builder<TestJob> {
+
+    private final List<EventSource> eventSources = new ArrayList<>();
+
+    private final List<Client> clients = new ArrayList<>();
+
+    private final List<Emitter> emitters = new ArrayList<>();
+
+    private final List<ReporterDestination> reporterDestinations = new ArrayList<>();
+
+    private MetricsCollector metricsCollector = new ScheduledMetricsCollector();
+
+    public static NcEventSource createNcEventSource() {
+        return createNcEventSource(null, null);
+    }
+
+    public static Emitter createNioEmitter(Configuration config, ConfigurationContext context) {
+        NioEmitter emitter = new NioEmitter();
+        if (config != null) {
+            emitter.configure(config, context);
+        }
+
+        return emitter;
+    }
+
+    public static NcEventSource createNcEventSource(String fileName) {
+        NcEventSource eventSrc = new NcEventSource();
+        eventSrc.setNcFile(fileName);
+        return eventSrc;
+    }
+
+    public static NcEventSource createNcEventSource(Configuration config, ConfigurationContext context) {
+        NcEventSource eventSrc = new NcEventSource();
+        if (config != null) {
+            eventSrc.configure(config, context);
+        }
+
+        return eventSrc;
+    }
+
+    public static PcapSessionEventSource createPcapEventSource() {
+        return createPcapEventSource(null, null);
+    }
+
+    public static PcapSessionEventSource createPcapEventSource(String fileName) {
+        PcapSessionEventSource eventSrc = new PcapSessionEventSource();
+        eventSrc.setPcapFile(fileName);
+        return eventSrc;
+    }
+
+    public static PcapSessionEventSource createPcapEventSource(Configuration config, ConfigurationContext context) {
+        PcapSessionEventSource eventSrc = new PcapSessionEventSource();
+        if (config != null) {
+            eventSrc.configure(config, context);
+        }
+
+        return eventSrc;
+    }
+
+    public static EventSource createEventSource(String fileName) throws IOException {
+        return createEventSource(fileName, null, null);
+    }
+
+    public static EventSource createEventSource(String fileName, Configuration config, ConfigurationContext context) throws IOException {
+        byte[] preambule;
+        InputStream in = null;
+        try {
+            in = openInputStream(new File(fileName));
+            preambule = IOUtils.toByteArray(in, 20);
+        } finally {
+            IOUtils.closeQuietly(in);
+        }
+
+        EventSource eventSource;
+        if (NcHeader.isPreambule(preambule, 0)) {
+            eventSource = createNcEventSource(fileName);
+        } else if (PcapFileHeader.isPreambule(preambule, 0)) {
+            eventSource = createPcapEventSource(fileName);
+        } else {
+            throw new IllegalArgumentException("Not supported file format '" + fileName + "'.");
+        }
+
+        if (config != null) {
+            eventSource.configure(config, context);
+        }
+
+        return eventSource;
+    }
+
+    public static HttpClient createHttpClient() {
+        return createHttpClient(null, null);
+    }
+
+    public static HttpClient createHttpClient(Configuration config, ConfigurationContext context) {
+        HttpClient client = new HttpClient();
+        if (client != null) {
+            client.configure(config, context);
+        }
+
+        return client;
+    }
+
+    public MetricsCollector metricsCollector() {
+        return metricsCollector;
+    }
+
+    public TestJobBuilder metricsCollector(MetricsCollector metricsCollector) {
+        this.metricsCollector = metricsCollector;
+        return this;
+    }
+
+    private void process(Configuration config, Errors errors) {
+        TestJobConfigurator configurator = new TestJobConfigurator();
+        ConfigurationContext context = ConfigurationContext.create();
+        configurator.configure(config, errors, context);
+        if (!errors.hasError()) {
+            process(context);
+        }
+    }
+
+    private static <T> void populateIfNotNull(Collection<T> srcCollection, Collection<T> dstCollection) {
+        if (srcCollection != null) {
+            dstCollection.addAll(srcCollection);
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    private void process(ConfigurationContext context) {
+        List<EventSource> cfgEventSources = (List<EventSource>) context.get(EVENT_SOURCE_SOURCES);
+        populateIfNotNull(cfgEventSources, eventSources);
+
+        List<Client> cfgClients = (List<Client>) context.get(CLIENT_CLIENTS);
+        populateIfNotNull(cfgClients, clients);
+
+        List<Emitter> cfgEmitters = (List<Emitter>) context.get(EMITTER_EMITTERS);
+        populateIfNotNull(cfgEmitters, emitters);
+    }
+
+    public static TestJobBuilder get() {
+        return new TestJobBuilder();
+    }
+
+    public static TestJobBuilder get(Configuration config, Errors errors) {
+        TestJobBuilder jobBuilder = get();
+        jobBuilder.process(config, errors);
+        return jobBuilder;
+    }
+
+    public static TestJobBuilder get(ConfigurationContext context) {
+        TestJobBuilder jobBuilder = get();
+        jobBuilder.process(context);
+        return jobBuilder;
+    }
+
+    private TestJob createTestJob() {
+        TestJob job = new TestJob();
+        return job;
+    }
+
+    @Override
+    public TestJob build() {
+        return createTestJob();
+    }
+}
--- a/stress-tester/src/main/java/com/passus/st/lookup/LookupsDomainConfigurator.java	Thu Jan 25 12:53:30 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/lookup/LookupsDomainConfigurator.java	Thu Jan 25 12:58:42 2018 +0100
@@ -13,7 +13,7 @@
 @Plugin(name = LookupsDomainConfigurator.DOMAIN, category = PluginConstants.CATEGORY_DOMAIN_CONFIGURATOR)
 public class LookupsDomainConfigurator implements DomainConfigurator {
 
-    public static final String DOMAIN = "lookup";
+    public static final String DOMAIN = "lookups";
 
     private final LookupsNodeDefinitionCreator creator = new LookupsNodeDefinitionCreator();
 
--- a/stress-tester/src/main/java/com/passus/st/reader/pcap/PcapFileHeader.java	Thu Jan 25 12:53:30 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/pcap/PcapFileHeader.java	Thu Jan 25 12:58:42 2018 +0100
@@ -45,9 +45,9 @@
         this.byteOrder = fileByteOrder;
     }
 
-    public static boolean isFormatSupported(byte[] buffer) {
-        int magicBE = DataHelper.BIG_ENDIAN.getInt4(buffer, 0);
-        int magicLE = DataHelper.LITTLE_ENDIAN.getInt4(buffer, 0);
+    public static boolean isPreambule(byte[] buffer, int offset) {
+        int magicBE = DataHelper.BIG_ENDIAN.getInt4(buffer, offset);
+        int magicLE = DataHelper.LITTLE_ENDIAN.getInt4(buffer, offset);
         return magicBE == MAGIC_FORMAT_PLAIN || magicLE == MAGIC_FORMAT_PLAIN
                 || magicBE == MAGIC_FORMAT_NANO || magicLE == MAGIC_FORMAT_NANO;
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/vars/VarsGlobalDomainConfigurator.java	Thu Jan 25 12:58:42 2018 +0100
@@ -0,0 +1,29 @@
+package com.passus.st.vars;
+
+import com.passus.commons.annotations.Plugin;
+import com.passus.config.Configuration;
+import com.passus.config.ConfigurationContext;
+import com.passus.config.DomainConfigurator;
+import com.passus.config.validation.Errors;
+import com.passus.st.plugin.PluginConstants;
+
+@Plugin(name = VarsGlobalDomainConfigurator.DOMAIN, category = PluginConstants.CATEGORY_DOMAIN_CONFIGURATOR)
+public class VarsGlobalDomainConfigurator implements DomainConfigurator {
+
+    public static final String DOMAIN = "globalVars";
+
+    @Override
+    public String getDomain() {
+        return DOMAIN;
+    }
+
+    @Override
+    public void validate(Configuration config, Errors errors, ConfigurationContext context) {
+
+    }
+
+    @Override
+    public void configure(Configuration config, Errors errors, ConfigurationContext context) {
+
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/vars/VarsNodeDefinitionCreator.java	Thu Jan 25 12:58:42 2018 +0100
@@ -0,0 +1,13 @@
+package com.passus.st.vars;
+
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
+
+public class VarsNodeDefinitionCreator implements NodeDefinitionCreator {
+
+
+    @Override
+    public NodeDefinition create() {
+        return null;
+    }
+}
--- a/stress-tester/src/test/resources/com/passus/st/config/test_job_config.yml	Thu Jan 25 12:53:30 2018 +0100
+++ b/stress-tester/src/test/resources/com/passus/st/config/test_job_config.yml	Thu Jan 25 12:58:42 2018 +0100
@@ -39,3 +39,10 @@
 
 httpFilters:
     - type: requestCacheHeadersCleaner
+
+lookups:
+    - type: csvFile
+      fileName: c:\csv
+
+globalVars:
+    varName: varValue