changeset 1043:be4171dac5c7

DnsMetric
author Devel 2
date Tue, 14 Apr 2020 11:30:52 +0200
parents 8cef199c9890
children 64517eef7649
files stress-tester/src/main/java/com/passus/st/client/FlowProcessor.java stress-tester/src/main/java/com/passus/st/client/dns/DnsFlowHandler.java stress-tester/src/main/java/com/passus/st/client/dns/DnsMetric.java stress-tester/src/main/java/com/passus/st/client/http/HttpFlowHandler.java stress-tester/src/main/java/com/passus/st/client/http/HttpFlowHandlerDataDecoder.java stress-tester/src/main/java/com/passus/st/metric/MetricsContainer.java
diffstat 6 files changed, 164 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/FlowProcessor.java	Tue Apr 14 11:30:22 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/FlowProcessor.java	Tue Apr 14 11:30:52 2020 +0200
@@ -271,6 +271,7 @@
 
     private void responseReceived0(FlowContext flowContext, Object response) {
         supervisor.onResponseReceived(flowContext, response);
+        flowContext.client.onResponseReceived(response, flowContext);
         flowContext.sentEvent(null);
         flowContext.receivedStartTimestamp(-1);
     }
@@ -382,6 +383,7 @@
 
     private void requestSent0(FlowContext flowContext, SessionPayloadEvent event) {
         flowContext.sentEvent = event;
+        flowContext.client.onRequestSent(event.getRequest(), flowContext);
         supervisor.onRequestSent(flowContext, event);
     }
 
--- a/stress-tester/src/main/java/com/passus/st/client/dns/DnsFlowHandler.java	Tue Apr 14 11:30:22 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/dns/DnsFlowHandler.java	Tue Apr 14 11:30:52 2020 +0200
@@ -3,6 +3,8 @@
 import com.passus.commons.Assert;
 import com.passus.commons.time.TimeAware;
 import com.passus.commons.time.TimeGenerator;
+import com.passus.net.dns.Dns;
+import com.passus.net.dns.DnsQuery;
 import com.passus.st.client.FlowContext;
 import com.passus.st.client.FlowHandler;
 import com.passus.st.client.FlowHandlerDataDecoder;
@@ -21,6 +23,8 @@
 
     boolean collectMetrics = false;
 
+    DnsMetric metric;
+
     public DnsFlowHandler() {
         this.decoder = new DnsFlowHandlerDataDecoder(this);
         this.encoder = new DnsFlowHandlerDataEncoder();
@@ -39,6 +43,17 @@
     @Override
     public void setCollectMetrics(boolean collectMetrics) {
         this.collectMetrics = collectMetrics;
+        if (collectMetrics) {
+            metric = new DnsMetric();
+            synchronized (metric) {
+                metric.activate();
+            }
+        } else {
+            synchronized (metric) {
+                metric.deactivate();
+            }
+            metric = null;
+        }
     }
 
     @Override
@@ -53,7 +68,12 @@
 
     @Override
     public void writeMetrics(MetricsContainer container) {
-
+        if (collectMetrics) {
+            synchronized (metric) {
+                container.update(metric);
+                metric.reset();
+            }
+        }
     }
 
     @Override
@@ -68,6 +88,34 @@
     }
 
     @Override
+    public void onRequestSent(Object request, FlowContext flowContext) {
+        if (collectMetrics) {
+            synchronized (metric) {
+                Dns dns = (Dns) request;
+                if (dns.getQueriesCount() > 0) {
+                    DnsQuery query = dns.getQueries().get(0);
+                    metric.addSentRecordType(query.getType());
+                }
+            }
+        }
+    }
+
+    @Override
+    public void onResponseReceived(Object response, FlowContext flowContext) {
+        if (collectMetrics) {
+            synchronized (metric) {
+                Dns dns = (Dns) response;
+                metric.addReplyCode(dns.getReplyCode());
+                if (dns.getQueriesCount() > 0) {
+                    dns.getAnswers().forEach((r) -> {
+                        metric.addReceivedRecordType(r.getType());
+                    });
+                }
+            }
+        }
+    }
+
+    @Override
     public void init(FlowContext flowContext) {
 
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/client/dns/DnsMetric.java	Tue Apr 14 11:30:52 2020 +0200
@@ -0,0 +1,75 @@
+package com.passus.st.client.dns;
+
+import com.passus.commons.metric.Metric;
+import com.passus.net.dns.DnsRecordType;
+import com.passus.net.dns.DnsReplyCode;
+import com.passus.st.client.GenericMetric;
+import org.apache.commons.lang3.mutable.MutableInt;
+
+import java.util.HashMap;
+
+public class DnsMetric extends GenericMetric {
+
+    public static final String DEFAULT_NAME = "DNS";
+
+    private HashMap<DnsRecordType, MutableInt> sentRecordsCount;
+
+    private HashMap<DnsRecordType, MutableInt> receivedRecordsCount;
+
+    private HashMap<DnsReplyCode, MutableInt> replayCodes;
+
+    public DnsMetric() {
+        this(DEFAULT_NAME);
+    }
+
+    public DnsMetric(String name) {
+        super(name);
+        attrs.put("sentRecordsCount", sentRecordsCount);
+        attrs.put("receivedRecordsCount", receivedRecordsCount);
+        attrs.put("replayCodes", replayCodes);
+    }
+
+    @Override
+    protected void doActivate() {
+        super.doActivate();
+        sentRecordsCount = new HashMap<>();
+        receivedRecordsCount = new HashMap<>();
+        replayCodes = new HashMap<>();
+    }
+
+    @Override
+    protected void doDeactivate() {
+        super.doDeactivate();
+        reset();
+        sentRecordsCount = null;
+        receivedRecordsCount = null;
+        replayCodes = null;
+    }
+
+    public void addSentRecordType(DnsRecordType recordType) {
+        Metric.incrementCountMap(recordType, sentRecordsCount);
+    }
+
+    public void addReceivedRecordType(DnsRecordType recordType) {
+        Metric.incrementCountMap(recordType, receivedRecordsCount);
+    }
+
+    public void addReplyCode(DnsReplyCode replayCode) {
+        Metric.incrementCountMap(replayCode, replayCodes);
+    }
+
+    @Override
+    public void update(Metric metric) {
+        DnsMetric clientMetric = (DnsMetric) metric;
+        Metric.updateCountMap(sentRecordsCount, clientMetric.sentRecordsCount);
+        Metric.updateCountMap(receivedRecordsCount, clientMetric.receivedRecordsCount);
+        Metric.updateCountMap(replayCodes, clientMetric.replayCodes);
+    }
+
+    @Override
+    public void reset() {
+        sentRecordsCount.clear();
+        receivedRecordsCount.clear();
+        replayCodes.clear();
+    }
+}
--- a/stress-tester/src/main/java/com/passus/st/client/http/HttpFlowHandler.java	Tue Apr 14 11:30:22 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/HttpFlowHandler.java	Tue Apr 14 11:30:52 2020 +0200
@@ -4,11 +4,8 @@
 import com.passus.commons.time.TimeAware;
 import com.passus.commons.time.TimeGenerator;
 import com.passus.net.http.HttpRequest;
-import com.passus.net.http.HttpResponse;
 import com.passus.st.client.FlowContext;
 import com.passus.st.client.FlowHandler;
-import com.passus.st.client.FlowHandlerDataDecoder;
-import com.passus.st.client.FlowHandlerDataEncoder;
 import com.passus.st.metric.MetricsContainer;
 
 import static com.passus.st.Protocols.HTTP;
@@ -17,9 +14,9 @@
 
 public class HttpFlowHandler implements FlowHandler, TimeAware {
 
-    private final FlowHandlerDataDecoder<HttpResponse> decoder;
+    private final HttpFlowHandlerDataDecoder decoder;
 
-    private final FlowHandlerDataEncoder<HttpRequest> encoder;
+    private final HttpFlowHandlerDataEncoder encoder;
 
     TimeGenerator timeGenerator = TimeGenerator.getDefaultGenerator();
 
@@ -35,24 +32,18 @@
         scopes = new HttpScopes();
     }
 
-    public HttpFlowHandler(FlowHandlerDataDecoder<HttpResponse> decoder,
-                           FlowHandlerDataEncoder<HttpRequest> encoder) {
-        this.decoder = decoder;
-        this.encoder = encoder;
-    }
-
     @Override
     public int getProtocolId() {
         return HTTP;
     }
 
     @Override
-    public FlowHandlerDataDecoder getResponseDecoder(FlowContext flowContext) {
+    public HttpFlowHandlerDataDecoder getResponseDecoder(FlowContext flowContext) {
         return decoder;
     }
 
     @Override
-    public FlowHandlerDataEncoder getRequestEncoder(FlowContext flowContext) {
+    public HttpFlowHandlerDataEncoder getRequestEncoder(FlowContext flowContext) {
         return encoder;
     }
 
@@ -81,11 +72,27 @@
     @Override
     public void setCollectMetrics(boolean collectMetrics) {
         this.collectMetrics = collectMetrics;
+        if (collectMetrics) {
+            metric = new HttpClientWorkerMetric();
+            synchronized (metric) {
+                metric.activate();
+            }
+        } else {
+            synchronized (metric) {
+                metric.deactivate();
+            }
+            metric = null;
+        }
     }
 
     @Override
     public void writeMetrics(MetricsContainer container) {
-
+        if (collectMetrics) {
+            synchronized (metric) {
+                container.update(metric);
+                metric.reset();
+            }
+        }
     }
 
     @Override
@@ -99,4 +106,5 @@
         long now = timeGenerator.currentTimeMillis();
         ((HttpRequest) (flowContext.sentEvent().getRequest())).setTag(TAG_TIME_END, now);
     }
+
 }
--- a/stress-tester/src/main/java/com/passus/st/client/http/HttpFlowHandlerDataDecoder.java	Tue Apr 14 11:30:22 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/HttpFlowHandlerDataDecoder.java	Tue Apr 14 11:30:52 2020 +0200
@@ -18,11 +18,9 @@
 
     private static final Logger LOGGER = LogManager.getLogger(HttpFlowHandlerDataDecoder.class);
 
-    private final HttpFullMessageDecoder decoder;
+    final HttpFullMessageDecoder decoder;
 
-    private final HttpFlowHandler handler;
-
-    private HttpRequest lastRequest;
+    final HttpFlowHandler handler;
 
     public HttpFlowHandlerDataDecoder(HttpFlowHandler handler) {
         this.handler = handler;
@@ -45,11 +43,19 @@
         return decoder.getLastError();
     }
 
+    private HttpRequest lastRequest(FlowContext flowContext) {
+        if (flowContext.sentEvent() != null) {
+            return (HttpRequest) flowContext.sentEvent().getRequest();
+        }
+
+        return null;
+    }
+
     @Override
     public void clear(FlowContext flowContext) {
+        HttpRequest lastRequest = lastRequest(flowContext);
         if (lastRequest != null) {
             extractHttpContext(flowContext).scopes().removeConversation(lastRequest);
-            lastRequest = null;
         }
 
         decoder.clear();
@@ -57,10 +63,7 @@
 
     @Override
     public int decode(ByteBuff buffer, FlowContext flowContext) {
-        if (flowContext.sentEvent() != null) {
-            lastRequest = (HttpRequest) flowContext.sentEvent().getRequest();
-        }
-
+        HttpRequest lastRequest = lastRequest(flowContext);
         if (lastRequest != null) {
             decoder.setRequestMethod(lastRequest.getMethod());
         }
--- a/stress-tester/src/main/java/com/passus/st/metric/MetricsContainer.java	Tue Apr 14 11:30:22 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/metric/MetricsContainer.java	Tue Apr 14 11:30:52 2020 +0200
@@ -3,11 +3,14 @@
 import com.passus.commons.metric.Metric;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 public interface MetricsContainer {
 
+    default boolean update(Metric metric) {
+        return update(System.currentTimeMillis(), metric);
+    }
+
     boolean update(long time, Metric metric);
 
 }