changeset 1248:d8b7a6da8618

flow - in progress
author Devel 1
date Wed, 01 Jul 2020 12:24:12 +0200
parents 1deac0382914
children f9aa26c8f638
files stress-tester/src/main/java/com/passus/st/scanner/FlowAnalyzerUtils.java stress-tester/src/main/java/com/passus/st/scanner/HttpUrlSequencePayloadAnalyzer.java stress-tester/src/main/java/com/passus/st/scanner/SequenceConverter.java
diffstat 3 files changed, 102 insertions(+), 55 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/scanner/FlowAnalyzerUtils.java	Wed Jul 01 12:24:12 2020 +0200
@@ -0,0 +1,40 @@
+package com.passus.st.scanner;
+
+import com.google.gson.Gson;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class FlowAnalyzerUtils {
+
+    static ArrayList<ArrayList<String>> readSequences(File file, Gson gson) throws IOException {
+        try (FileInputStream fis = new FileInputStream(file);
+                InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
+            return gson.fromJson(isr, ArrayList.class);
+        }
+    }
+
+    static List<SeqWithFolds> readFolds(File file, Gson gson) throws IOException {
+        try (FileInputStream fis = new FileInputStream(file);
+                InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
+            SeqWithFolds[] arr = gson.fromJson(isr, SeqWithFolds[].class);
+            return Arrays.asList(arr);
+        }
+    }
+
+    static final class SeqWithFolds {
+
+        public List<String> sequence;
+        public float prob;
+        public ArrayList<ArrayList<String>> unfolded;
+    }
+}
--- a/stress-tester/src/main/java/com/passus/st/scanner/HttpUrlSequencePayloadAnalyzer.java	Wed Jul 01 11:55:49 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/scanner/HttpUrlSequencePayloadAnalyzer.java	Wed Jul 01 12:24:12 2020 +0200
@@ -22,14 +22,11 @@
 import com.passus.st.metric.MetricsContainer;
 import com.passus.st.plugin.PluginConstants;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
-import java.io.InputStreamReader;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.nio.charset.StandardCharsets;
 import java.text.SimpleDateFormat;
-import java.util.ArrayList;
 import java.util.Date;
 
 import static com.passus.config.schema.ConfigurationSchemaBuilder.listDef;
@@ -159,7 +156,7 @@
             for (String fname : dir.list()) {
                 Matcher m = pattern.matcher(fname);
                 if (m.matches()) {
-                    metric.addSequences(m.group(1), readSequences(new File(dir, fname), gson));
+                    metric.addSequences(m.group(1), FlowAnalyzerUtils.readSequences(new File(dir, fname), gson));
                 }
             }
 
@@ -168,13 +165,6 @@
         }
     }
 
-    static ArrayList<ArrayList<String>> readSequences(File file, Gson gson) throws IOException {
-        try (FileInputStream fis = new FileInputStream(file);
-                InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
-            return gson.fromJson(isr, ArrayList.class);
-        }
-    }
-
     static UserIdExtractor resolveUserIdExtractor(String spec) {
         int idx = spec.indexOf(':');
         String type;
--- a/stress-tester/src/main/java/com/passus/st/scanner/SequenceConverter.java	Wed Jul 01 11:55:49 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/scanner/SequenceConverter.java	Wed Jul 01 12:24:12 2020 +0200
@@ -1,22 +1,25 @@
 package com.passus.st.scanner;
 
 import com.google.gson.Gson;
+import com.passus.st.Log4jConfigurationFactory;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
-import org.yaml.snakeyaml.DumperOptions;
+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;
 import org.yaml.snakeyaml.Yaml;
 
 import static com.passus.commons.collection.FluentBuilder.*;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
+import static com.passus.st.utils.CliUtils.option;
 
 /**
  *
@@ -33,12 +36,6 @@
     int multiplier = 7;
     int maxRepeats = 100;
 
-    /*
-    - type: matcher
-      matches:
-          '@req.uri': {$contains: 'test'}
-          '@resp.status.code': 200
-     */
     static Map<String, Object> prepareMatchFilter(List<String> ignoredUrls) {
         List<Map<String, Object>> matches = new ArrayList<>(ignoredUrls.size());
         for (String ignoredUrl : ignoredUrls) {
@@ -46,20 +43,12 @@
         }
         return lmap(
                 e("type", "matcher"),
-                //                e("acceptOnMatch", true),
+                //e("acceptOnMatch", true),
                 e("matches", matches)
         );
 
     }
 
-    /*
-  - type: sequence
-    exact: true
-    sequence:
-      - match: {"@req.uri": "/res1", "@resp.getCookie('id')": {$neq: "123"}}
-      - match: {"@req.getHeader('xyz')": "abc"}
-        repeats: 100
-     */
     Map<String, Object> prepareSequenceFilter(List<String> steps) {
         List<Map<String, Object>> sequence = new ArrayList<>(steps.size());
         boolean first = true;
@@ -94,7 +83,7 @@
         );
     }
 
-    void writeSequenceFilterConf(List<List<String>> sequences, List<String> ignoredUrls, File file) throws IOException {
+    void writeSequenceFilterConf(List<? extends List<String>> sequences, List<String> ignoredUrls, File file) throws IOException {
         List<Map<String, Object>> filters = new ArrayList<>();
         if (ignoredUrls != null) {
             filters.add(prepareMatchFilter(ignoredUrls));
@@ -104,39 +93,67 @@
         }
         Map<String, Object> cfg = smap("filters", filters);
 
-        DumperOptions opts = new DumperOptions();
         Yaml yaml = new Yaml();
-
         try (FileOutputStream fos = new FileOutputStream(file);
                 OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);) {
             yaml.dump(cfg, osw);
         }
     }
 
-    static List<SeqWithFolds> readFolds(File file, Gson gson) throws IOException {
-        try (FileInputStream fis = new FileInputStream(file);
-                InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
-            SeqWithFolds[] arr = gson.fromJson(isr, SeqWithFolds[].class);
-            return Arrays.asList(arr);
+    public static void main(String... args) {
+        final Options options = new Options();
+        options.addOption(option("l", "logLevel", "Log level.", "level"));
+        options.addOption(option("f", "useFold", "Use this if processing folded sequences."));
+
+        try {
+            CommandLine cl = new DefaultParser().parse(options, args);
+            String[] clArgs = cl.getArgs();
+            if (clArgs.length != 2) {
+                System.err.println("Input and output required.");
+                printHelp(options);
+                return;
+            }
+
+            String logLevel = "error";
+            if (cl.hasOption("l")) {
+                logLevel = cl.getOptionValue("l");
+            }
+            Log4jConfigurationFactory.enableFactory(logLevel);
+
+            File input = new File(clArgs[0]);
+            if (!input.isFile()) {
+                System.err.println("Not a file: " + input.getAbsolutePath());
+                return;
+            }
+            File output = new File(clArgs[1]);
+            if (!output.isFile()) {
+                System.err.println("Not a file: " + output.getAbsolutePath());
+                return;
+            }
+
+            boolean fold = cl.hasOption('f');
+            ArrayList<ArrayList<String>> sequences;
+            if (fold) {
+                sequences = FlowAnalyzerUtils.readFolds(input, new Gson()).get(1).unfolded;
+            } else {
+                sequences = FlowAnalyzerUtils.readSequences(input, new Gson());
+            }
+
+            List<String> ignoredUrls = Arrays.asList("/user/check", "/stacje/podpowiedzi", "/api/mobile");
+            SequenceConverter converter = new SequenceConverter();
+            converter.writeSequenceFilterConf(sequences, ignoredUrls, output);
+
+        } catch (ParseException e) {
+            System.out.println(e.getMessage());
+            printHelp(options);
+        } catch (Exception e) {
+            e.printStackTrace(System.err);
         }
     }
 
-    public static final class SeqWithFolds {
-
-        public List<String> sequence;
-        public float prob;
-        public List<List<String>> unfolded;
-    }
-
-    public static void main(String[] args) throws Exception {
-        File input = new File("D:\\hg\\stress-tester\\stress-tester\\src\\main\\resources\\seq_L7f_unfolded.json");
-        List<SeqWithFolds> swf = readFolds(input, new Gson());
-        System.out.println("");
-
-        List<String> ignoredUrls = Arrays.asList("/user/check", "/stacje/podpowiedzi", "/api/mobile");
-        SequenceConverter converter = new SequenceConverter();
-        File output = new File("D:\\hg\\stress-tester\\stress-tester\\x.yaml");
-        converter.writeSequenceFilterConf(swf.get(1).unfolded, ignoredUrls, output);
+    static void printHelp(Options options) {
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp("SequenceConverter [options] <input> <output>", "description", options, "");
     }
 
 }