changeset 1243:0dddf15429a8

SequenceFilter.maxSequences
author Devel 2
date Wed, 01 Jul 2020 08:29:08 +0200
parents 172c6a940a93
children fe3bbcdf8a1f
files stress-tester/src/main/java/com/passus/st/filter/SequenceFilter.java stress-tester/src/test/java/com/passus/st/filter/SequenceFilterTest.java
diffstat 2 files changed, 70 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/filter/SequenceFilter.java	Wed Jul 01 08:16:57 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/filter/SequenceFilter.java	Wed Jul 01 08:29:08 2020 +0200
@@ -35,13 +35,13 @@
 
     public static final String TYPE = "sequence";
 
-    private static final int DEFAULT_CHAINS_NUM = 32;
+    private static final int DEFAULT_MAX_SEQUENCES = 10_000;
 
     private static final String STAGE_PREFIX = "_i";
 
     private TimeGenerator timeGenerator = new SystemTimeGenerator();
 
-    private final List<StageChain> chains = new ArrayList<>(DEFAULT_CHAINS_NUM);
+    private final List<StageChain> chains = new ArrayList<>(DEFAULT_MAX_SEQUENCES / 4);
 
     private final FlowFilterFactory filterFactory;
 
@@ -55,6 +55,8 @@
 
     private boolean exact = false;
 
+    private int maxSequences = DEFAULT_MAX_SEQUENCES;
+
     public SequenceFilter() {
         this(FlowFilterFactory.DEFAULT_FACTORY);
     }
@@ -64,7 +66,6 @@
         this.filterFactory = filterFactory;
     }
 
-
     public void init(final Stage[] stages, final Action[] actions) {
         Assert.notNull(stages, "stages");
         Assert.notNull(actions, "actions");
@@ -116,6 +117,15 @@
         this.exact = exact;
     }
 
+    public int getMaxSequences() {
+        return maxSequences;
+    }
+
+    public void setMaxSequences(int maxSequences) {
+        Assert.greaterThanZero(maxSequences, "maxSequences");
+        this.maxSequences = maxSequences;
+    }
+
     public FilterDirection getDirection() {
         return direction;
     }
@@ -265,14 +275,16 @@
             }
         }
 
-        final Map<String, Object> valueMap = new HashMap<>(2);
-        valueMap.put("req", value.getReq());
-        valueMap.put("resp", value.getResp());
-        if (rootStage.match(valueMap)) {
-            StageChain chain = new StageChain(valueMap);
-            chain.setStage(now, rootStage, null);
-            if (nextStage(chain, now, value, context)) {
-                chains.add(chain);
+        if (chains.size() <= maxSequences) {
+            final Map<String, Object> valueMap = new HashMap<>(2);
+            valueMap.put("req", value.getReq());
+            valueMap.put("resp", value.getResp());
+            if (rootStage.match(valueMap)) {
+                StageChain chain = new StageChain(valueMap);
+                chain.setStage(now, rootStage, null);
+                if (nextStage(chain, now, value, context)) {
+                    chains.add(chain);
+                }
             }
         }
     }
--- a/stress-tester/src/test/java/com/passus/st/filter/SequenceFilterTest.java	Wed Jul 01 08:16:57 2020 +0200
+++ b/stress-tester/src/test/java/com/passus/st/filter/SequenceFilterTest.java	Wed Jul 01 08:29:08 2020 +0200
@@ -114,10 +114,13 @@
     }
 
     private List<Metric> processStages(Stage[] stages, Collection<Pair<HttpRequest, HttpResponse>> pairs) throws Exception {
-        return processStages(stages, pairs, false);
+        return processStages(stages, pairs, Collections.EMPTY_MAP);
     }
 
-    private List<Metric> processStages(Stage[] stages, Collection<Pair<HttpRequest, HttpResponse>> pairs, boolean exact) throws Exception {
+    private List<Metric> processStages(Stage[] stages, Collection<Pair<HttpRequest, HttpResponse>> pairs, Map<String, Object> params) throws Exception {
+        boolean exact = (boolean) params.getOrDefault("exact", false);
+        int maxSequences = (int) params.getOrDefault("maxSequences", 10_000);
+
         Map<String, ValueExtractor> values = new HashMap<>();
         for (int i = 0; i < stages.length; i++) {
             values.put("url" + i, value("@_i" + i + ".req.url"));
@@ -129,6 +132,7 @@
 
         SequenceFilter filter = new SequenceFilter();
         filter.setExact(exact);
+        filter.setMaxSequences(maxSequences);
         filter.init(stages, actions);
 
         for (Pair<HttpRequest, HttpResponse> p : pairs) {
@@ -240,7 +244,9 @@
                 pair(req("http://example.com/res3"), null)
         );
 
-        List<Metric> metrics = processStages(stages, pairs, true);
+        Map<String, Object> params = new HashMap<>(2);
+        params.put("exact", true);
+        List<Metric> metrics = processStages(stages, pairs, params);
         assertNotNull(metrics);
         assertEquals(3, metrics.size());
 
@@ -265,7 +271,9 @@
                 pair(req("http://example.com/res2"), null)
         );
 
-        List<Metric> metrics = processStages(stages, pairs, true);
+        Map<String, Object> params = new HashMap<>(2);
+        params.put("exact", true);
+        List<Metric> metrics = processStages(stages, pairs, params);
         assertNull(metrics);
     }
 
@@ -285,7 +293,9 @@
                 pair(req("http://example.com/res3"), null)
         );
 
-        List<Metric> metrics = processStages(stages, pairs, true);
+        Map<String, Object> params = new HashMap<>(2);
+        params.put("exact", true);
+        List<Metric> metrics = processStages(stages, pairs, params);
         assertNotNull(metrics);
         assertEquals(2, metrics.size());
 
@@ -296,6 +306,38 @@
         }
     }
 
+
+    @Test
+    public void testMaxSequences() throws Exception {
+        Stage[] stages = {
+                stage("{\"@req.uri\": \"/res1\"}", 10),
+                stage("{\"@req.uri\": \"/res2\"}", 0),
+                stage("{\"@req.uri\": \"/res3\"}", 0)
+        };
+
+        List<Pair<HttpRequest, HttpResponse>> pairs = pairs(
+                pair(req("http://example.com/res1"), null),
+                pair(req("http://example.com/res2"), null),
+                pair(req("http://example.com/res1"), null),
+                pair(req("http://example.com/res2"), null),
+                pair(req("http://example.com/res3"), null),
+                pair(req("http://example.com/res3"), null)
+        );
+
+        Map<String, Object> params = new HashMap<>(2);
+        params.put("exact", true);
+        params.put("maxSequences", 1);
+        List<Metric> metrics = processStages(stages, pairs, params);
+        assertNotNull(metrics);
+        assertEquals(1, metrics.size());
+
+        for (Metric metric : metrics) {
+            assertEquals("http://example.com/res1", metric.getAttributeValue("url0").toString());
+            assertEquals("http://example.com/res2", metric.getAttributeValue("url1").toString());
+            assertEquals("http://example.com/res3", metric.getAttributeValue("url2").toString());
+        }
+    }
+
     @Test
     public void testConfigure() throws Exception {
         File file = ResourceUtils.getFile("com/passus/st/client/http/filter/sequence.yml");