changeset 702:aa0a94b0469d

converter
author Devel 1
date Tue, 28 Nov 2017 13:30:08 +0100
parents be2f53a20274
children 68d12f09c3a1
files stress-tester/src/main/java/com/passus/st/ConverterHttpClient.java stress-tester/src/main/java/com/passus/st/ConverterMain.java stress-tester/src/main/java/com/passus/st/reader/nc/NcDataBlockWriter.java stress-tester/src/main/java/com/passus/st/source/NcEventDestination.java stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java
diffstat 5 files changed, 319 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/ConverterHttpClient.java	Tue Nov 28 13:30:08 2017 +0100
@@ -0,0 +1,88 @@
+package com.passus.st;
+
+import com.passus.commons.Assert;
+import com.passus.commons.service.Service;
+import com.passus.st.client.Event;
+import com.passus.st.client.EventHandler;
+import com.passus.st.client.http.filter.HttpFilter;
+import com.passus.st.client.http.filter.HttpFilterAware;
+import com.passus.st.client.http.filter.HttpFilterChain;
+import com.passus.st.source.NcEventDestination;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+class ConverterHttpClient implements Service, EventHandler, HttpFilterAware {
+
+    private static final Logger LOGGER = LogManager.getLogger(ConverterHttpClient.class);
+
+    private final String ncFile;
+    private final HttpFilterChain filterChain = new HttpFilterChain();
+
+    private NcEventDestination dst;
+    private boolean started;
+
+    public ConverterHttpClient(String ncFile) {
+        this.ncFile = ncFile;
+    }
+
+    @Override
+    public boolean isStarted() {
+        return started;
+    }
+
+    @Override
+    public void start() {
+        if (started) {
+            return;
+        }
+
+        dst = new NcEventDestination(ncFile);
+//        dst.setAllowOverride(true);
+        dst.start();
+
+        started = true;
+    }
+
+    @Override
+    public void stop() {
+        if (!started) {
+            return;
+        }
+
+        dst.stop();
+        dst = null;
+
+        started = false;
+    }
+
+    @Override
+    public void handle(Event event) {
+        if (!started) {
+            throw new IllegalArgumentException("Service not started.");
+        }
+
+        if (LOGGER.isTraceEnabled()) {
+            LOGGER.trace("Event: {}", event);
+        }
+
+        try {
+            dst.handle(event);
+        } catch (Exception e) {
+            LOGGER.debug(e.getMessage(), e);
+        }
+    }
+
+    @Override
+    public void addFilter(HttpFilter filter) {
+        Assert.notNull(filter, "filter");
+        filterChain.addFilter(filter);
+    }
+
+    public void join() {
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/ConverterMain.java	Tue Nov 28 13:30:08 2017 +0100
@@ -0,0 +1,197 @@
+package com.passus.st;
+
+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.http.filter.HttpFiltersConfigurator;
+import com.passus.st.source.PcapSessionEventSource;
+import static com.passus.st.utils.CliUtils.option;
+import java.io.File;
+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.cli.ParseException;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class ConverterMain {
+
+    static void printHelp(Options options) {
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp("[options] <input pcap> <output nc>", "description", options, "");
+    }
+
+    private static Options createOptions() {
+        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()
+        );
+
+//        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.addOption(option("pr", "parallelReplays").desc("Number of parallel replays. Works only for one pcap file.")
+////                .hasArg().argName("replays")
+////                .build());
+//
+////        options.addOption(option("ca", "cache").desc("Cache (and preprocess) input file.")
+////                .hasArg(false)
+////                .build());
+//
+////        options.addOption(option("lp", "loops").desc("Loops number (default 1).")
+////                .hasArg().argName("loop")
+////                .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()
+//        );
+//        options.addOption(option("ri", "reporterIp").desc("Reporter ip address.")
+//                .hasArg().argName("ip")
+//                .build()
+//        );
+//        options.addOption(option("nrt", "numReporterThreads").desc("Number of sending threads. (range 1 - 8, default 2)")
+//                .hasArg().argName("threads")
+//                .build()
+//        );
+//
+//        options.addOption(option("wf", "writeFile").desc("Write result to file.")
+//                .hasArg().argName("file").optionalArg(true)
+//                .build()
+//        );
+//
+//        options.addOption(option("wd", "writeDirectory").desc("Write HTTP messages to separate files.")
+//                .hasArg().argName("directory")
+//                .build()
+//        );
+//
+//        options.addOption(option("wdro", "writeDirectoryRequestsOnly").desc("Write only HTTP requests.")
+//                .hasArg(false)
+//                .build()
+//        );
+//
+//        options.addOption(option("wm", "writeMetrics").desc("Write metrics to file.")
+//                .hasArg().argName("file")
+//                .build()
+//        );
+        return options;
+    }
+
+    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 < 2) {
+                System.err.println("At least one pcap file required.");
+                printHelp(options);
+                return;
+            }
+            String input = clArgs[0];
+            String output = clArgs[1];
+            System.out.println("Converting " + input + " to " + output);
+
+            String logLevel = "error";
+            if (cl.hasOption("l")) {
+                logLevel = cl.getOptionValue("l");
+            }
+            if (!logLevel.equals("default")) {
+                Log4jConfigurationFactory.enableFactory(logLevel);
+            }
+
+            // mapper
+            // emitter
+            // client
+            // reporter
+            // filter file - dostaje w argumencie klienta !!!
+            ConverterHttpClient client = new ConverterHttpClient(output);
+
+            if (cl.hasOption("ff")) {
+                File filtersFile = new File(cl.getOptionValue("ff"));
+                Configuration config = YamlConfigurationReader.readFromFile(filtersFile);
+                HttpFiltersConfigurator configurator = new HttpFiltersConfigurator(client);
+                Errors errors = new Errors();
+                configurator.configure(config, errors, null);
+                if (errors.getErrorCount() != 0) {
+                    System.out.println("Error in file '" + filtersFile.getAbsolutePath() + "'.");
+                    for (ObjectError error : errors.getAllErrors()) {
+                        System.out.println("\t" + objectErrorToString(error));
+                    }
+                    System.exit(1);
+                }
+            }
+            client.start();
+
+            PcapSessionEventSource eventSrc = new PcapSessionEventSource();
+            eventSrc.setPcapFile(input);
+            eventSrc.setAllowPartialSession(cl.hasOption("ps"));
+            eventSrc.setCollectMetrics(true);
+
+            if (cl.hasOption("hp")) {
+                PortRangeSet portsRanges = eventSrc.getPortsRange();
+                portsRanges.clear();
+
+                String[] ports = cl.getOptionValues("hp");
+                for (String port : ports) {
+                    portsRanges.add(port);
+                }
+            }
+
+            eventSrc.setHandler(client);
+            eventSrc.start();
+
+            eventSrc.join();
+            client.join();
+            eventSrc.stop();
+            client.stop();
+
+        } catch (ParseException e) {
+            System.out.println(e.getMessage());
+            printHelp(options);
+        } catch (Exception e) {
+            e.printStackTrace(System.err);
+        } finally {
+            AppUtils.unregisterAll();
+        }
+    }
+
+    public static void main(String[] args) {
+        new ConverterMain().start(args);
+    }
+
+}
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataBlockWriter.java	Tue Nov 28 11:52:06 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataBlockWriter.java	Tue Nov 28 13:30:08 2017 +0100
@@ -41,6 +41,8 @@
 
     private boolean opened;
 
+    private boolean allowOverride;
+
     private int bufferSize = DEFAULT_BYTE_BUFFER;
 
     private int currentBlockType = -1;
@@ -76,6 +78,14 @@
         this.path = path;
     }
 
+    public boolean isAllowOverride() {
+        return allowOverride;
+    }
+
+    public void setAllowOverride(boolean allowOverride) {
+        this.allowOverride = allowOverride;
+    }
+
     public int getMaxSegmentBlocks() {
         return maxSegmentBlocks;
     }
@@ -134,7 +144,8 @@
     @Override
     public void open() throws IOException {
         try {
-            ch = FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
+            StandardOpenOption createOption = allowOverride ? StandardOpenOption.CREATE : StandardOpenOption.CREATE_NEW;
+            ch = FileChannel.open(path, StandardOpenOption.WRITE, createOption);
             writeHeader();
             openSegmentBlock();
             opened = true;
--- a/stress-tester/src/main/java/com/passus/st/source/NcEventDestination.java	Tue Nov 28 11:52:06 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/source/NcEventDestination.java	Tue Nov 28 13:30:08 2017 +0100
@@ -35,6 +35,8 @@
 
     private boolean encodeData = true;
 
+    private boolean allowOverride;
+
     public NcEventDestination() {
     }
 
@@ -67,6 +69,14 @@
         this.encodeData = encodeData;
     }
 
+    public boolean isAllowOverride() {
+        return allowOverride;
+    }
+
+    public void setAllowOverride(boolean allowOverride) {
+        this.allowOverride = allowOverride;
+    }
+
     @Override
     public void start() {
         if (started) {
@@ -75,6 +85,7 @@
 
         try {
             writer = new NcDataBlockWriter(ncFile);
+            writer.setAllowOverride(allowOverride);
             writer.open();
 
             started = true;
--- a/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Tue Nov 28 11:52:06 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Tue Nov 28 13:30:08 2017 +0100
@@ -252,6 +252,17 @@
         started = false;
     }
 
+    public void waitForEmptyQueue() throws InterruptedException {
+        while (queue.isEmpty() == false) {
+            Thread.sleep(100);
+        }
+    }
+
+    public void join() throws InterruptedException {
+        pcapThread.join();
+        waitForEmptyQueue();
+    }
+
     private class PcapThread extends Thread {
 
         private final DataBlockHandler dataBlockHandler = new DataBlockHandler(loopDelay);