Mercurial > stress-tester
changeset 986:2fc3f6154913
Metrics redefinition
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/client/FlowMetric.java Mon Aug 19 12:25:05 2019 +0200 @@ -0,0 +1,109 @@ +package com.passus.st.client; + +import com.passus.commons.metric.LongHistogramMetric; +import com.passus.commons.metric.LongHistogramMetricImpl; +import com.passus.commons.metric.Metric; +import org.apache.commons.lang3.mutable.MutableInt; + +public class FlowMetric extends GenericMetric { + + public static final String FLOW_METRIC = "flow"; + + private final LongHistogramMetric requestSendingTimeHistogram = new LongHistogramMetricImpl(DEFAULT_HISTOGRAM_VALUES); + + private final MutableInt requestsNum = new MutableInt(0); + + private final MutableInt responsesNum = new MutableInt(0); + + private final MutableInt errorsNum = new MutableInt(0); + + private final LongHistogramMetric requestSizeHistogram = new LongHistogramMetricImpl(DEFAULT_HISTOGRAM_VALUES); + + private final LongHistogramMetric responseSizeHistogram = new LongHistogramMetricImpl(DEFAULT_HISTOGRAM_VALUES); + + private final LongHistogramMetric responseReceivingTimeHistogram = new LongHistogramMetricImpl(DEFAULT_HISTOGRAM_VALUES); + + public FlowMetric() { + super(FLOW_METRIC); + attrs.put("requests", requestsNum); + attrs.put("responses", responsesNum); + attrs.put("errors", errorsNum); + attrs.put("requestSendTimeDist", requestSendingTimeHistogram); + attrs.put("requestSizeDist", requestSizeHistogram); + attrs.put("responseSizeDist", responseSizeHistogram); + attrs.put("responseRecvTimeDist", responseReceivingTimeHistogram); + } + + public void incRequestsNum() { + requestsNum.increment(); + } + + public void incResponsesNum() { + responsesNum.increment(); + } + + public void incErrorNum() { + errorsNum.increment(); + } + + public int getRequestsNum() { + return requestsNum.intValue(); + } + + public int getResponsesNum() { + return responsesNum.intValue(); + } + + public void addRequestSendingTime(long time) { + requestSendingTimeHistogram.update(time); + } + + public void addRequestSize(int size) { + requestSizeHistogram.update(size); + } + + public void addResponseReceivingTime(long time) { + responseReceivingTimeHistogram.update(time); + } + + @Override + protected void doActivate() { + responseReceivingTimeHistogram.activate(); + requestSendingTimeHistogram.activate(); + requestSizeHistogram.activate(); + responseSizeHistogram.activate(); + } + + @Override + protected void doDeactivate() { + responseReceivingTimeHistogram.deactivate(); + requestSendingTimeHistogram.deactivate(); + requestSizeHistogram.deactivate(); + responseSizeHistogram.deactivate(); + } + + @Override + public void update(Metric metric) { + FlowMetric flowMetric = (FlowMetric) metric; + errorsNum.add(flowMetric.errorsNum); + requestsNum.add(flowMetric.requestsNum); + responsesNum.add(flowMetric.responsesNum); + requestSendingTimeHistogram.update(flowMetric.requestSendingTimeHistogram); + requestSizeHistogram.update(flowMetric.requestSizeHistogram); + responseSizeHistogram.update(flowMetric.responseSizeHistogram); + responseReceivingTimeHistogram.update(flowMetric.responseReceivingTimeHistogram); + } + + @Override + public void reset() { + errorsNum.setValue(0); + requestsNum.setValue(0); + responsesNum.setValue(0); + requestSendingTimeHistogram.reset(); + requestSizeHistogram.reset(); + responseSizeHistogram.reset(); + responseReceivingTimeHistogram.reset(); + } + + +}
--- a/stress-tester/src/main/java/com/passus/st/client/FlowWorker.java Mon Aug 19 10:24:58 2019 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/FlowWorker.java Mon Aug 19 12:25:05 2019 +0200 @@ -3,7 +3,6 @@ import com.passus.commons.Assert; import com.passus.commons.time.TimeAware; import com.passus.commons.time.TimeGenerator; -import com.passus.st.client.http.HttpClientWorkerMetric; import com.passus.st.emitter.Emitter; import com.passus.st.emitter.EmitterHandler; import com.passus.st.emitter.SessionInfo; @@ -29,7 +28,7 @@ protected boolean collectMetric; - protected HttpClientWorkerMetric metric; + protected FlowMetric metric; protected boolean connectPartialSession = false; @@ -96,7 +95,7 @@ @Override public void setCollectMetrics(boolean collectMetrics) { if (collectMetrics && metric == null) { - metric = new HttpClientWorkerMetric(); + metric = new FlowMetric(); metric.activate(); collectMetric = true; } else if (!collectMetrics && metric != null) {
--- a/stress-tester/src/main/java/com/passus/st/client/FlowWorkerBase.java Mon Aug 19 10:24:58 2019 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/FlowWorkerBase.java Mon Aug 19 12:25:05 2019 +0200 @@ -383,12 +383,19 @@ FlowHandler client = flowContext.client(); FlowHandlerDataDecoder decoder = client.getResponseDecoder(flowContext); decoder.decode(data, flowContext); + long now = timeGenerator.currentTimeMillis(); if (flowContext.receivedStartTimestamp() == -1) { flowContext.receivedStartTimestamp(now); } if (decoder.state() == DataDecoder.STATE_ERROR) { + if (collectMetric) { + synchronized (metric) { + metric.incErrorNum(); + } + } + if (logger.isDebugEnabled()) { debug(flowContext, "Decoder error. " + decoder.getLastError()); } @@ -396,6 +403,13 @@ decoder.clear(flowContext); changeFlowState(flowContext, FlowContext.STATE_RESP_RECEIVED); } else if (decoder.state() == DataDecoder.STATE_FINISHED) { + if (collectMetric) { + synchronized (metric) { + metric.incResponsesNum(); + metric.addResponseReceivingTime(now - flowContext.receivedStartTimestamp()); + } + } + Object resp = decoder.getResult(); Object req = null; if (flowContext.sentEvent() != null) { @@ -414,6 +428,12 @@ changeFlowState(flowContext, FlowContext.STATE_RESP_RECEIVED); } } catch (Exception e) { + if (collectMetric) { + synchronized (metric) { + metric.incErrorNum(); + } + } + if (logger.isDebugEnabled()) { debug(flowContext, e.getMessage(), e); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/client/GenericMetric.java Mon Aug 19 12:25:05 2019 +0200 @@ -0,0 +1,89 @@ +package com.passus.st.client; + +import com.passus.commons.Assert; +import com.passus.commons.metric.Metric; + +import java.io.Serializable; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +public abstract class GenericMetric implements Metric { + + protected static final int DEFAULT_HISTOGRAM_VALUES = 2048; + + private final String name; + + protected final Map<String, Serializable> attrs = new LinkedHashMap<>(); + + private boolean active; + + public GenericMetric(String name) { + Assert.notNull(name, "name"); + this.name = name; + } + + + @Override + public String getName() { + return name; + } + + @Override + public boolean hasAttribute(String name) { + return attrs.containsKey(name); + } + + @Override + public Set<String> getAttributesName() { + return attrs.keySet(); + } + + @Override + public Serializable getAttributeValue(String name) { + return attrs.get(name); + } + + @Override + public Map<String, Serializable> getAttributesValue() { + return Collections.unmodifiableMap(attrs); + } + + @Override + public boolean isActive() { + return active; + } + + @Override + public final void activate() { + if (active) { + return; + } + + doActivate(); + active = true; + } + + protected void doActivate() { + + } + + @Override + public final void deactivate() { + if (!active) { + return; + } + + reset(); + doDeactivate(); + active = false; + } + + protected void doDeactivate() { + + } + + + +}
--- a/stress-tester/src/main/java/com/passus/st/client/http/HttpClientWorkerMetric.java Mon Aug 19 10:24:58 2019 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/HttpClientWorkerMetric.java Mon Aug 19 12:25:05 2019 +0200 @@ -1,112 +1,51 @@ package com.passus.st.client.http; -import com.passus.commons.Assert; import com.passus.commons.metric.LongHistogramMetric; import com.passus.commons.metric.LongHistogramMetricImpl; import com.passus.commons.metric.Metric; -import java.io.Serializable; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Set; -import java.util.TreeMap; +import com.passus.st.client.GenericMetric; import org.apache.commons.lang3.mutable.MutableInt; +import java.util.Map; +import java.util.TreeMap; + /** - * * @author Mirosław Hawrot */ -public class HttpClientWorkerMetric implements Metric { - - private static final int DEFAULT_HISTOGRAM_VALUES = 2048; - - public static final String DEFAULT_NAME = "httpClientWorker"; +public class HttpClientWorkerMetric extends GenericMetric { - private final String name; - - private final Map<String, Serializable> attrs = new LinkedHashMap<>(); + public static final String DEFAULT_NAME = "HTTP"; - private final MutableInt requestsNum = new MutableInt(0); - private final MutableInt responsesNum = new MutableInt(0); - - //private final HashMap<ByteString, MutableInt> reqUrls = new HashMap<>(); private final TreeMap<Integer, MutableInt> respStatuses = new TreeMap<>(); private final LongHistogramMetric responseTimeHistogram = new LongHistogramMetricImpl(DEFAULT_HISTOGRAM_VALUES); - private final LongHistogramMetric requestSendingTimeHistogram = new LongHistogramMetricImpl(DEFAULT_HISTOGRAM_VALUES); - private final LongHistogramMetric responseReceivingTimeHistogram = new LongHistogramMetricImpl(DEFAULT_HISTOGRAM_VALUES); - private final LongHistogramMetric requestSizeHistogram = new LongHistogramMetricImpl(DEFAULT_HISTOGRAM_VALUES); + private final LongHistogramMetric responseSizeHistogram = new LongHistogramMetricImpl(DEFAULT_HISTOGRAM_VALUES); - private boolean active; - public HttpClientWorkerMetric() { this(DEFAULT_NAME); } public HttpClientWorkerMetric(String name) { - Assert.notNull(name, "name"); - this.name = name; - attrs.put("requests", requestsNum); - attrs.put("responses", responsesNum); - //attrs.put("requestsUrl", reqUrls); + super(name); attrs.put("responseStatusCodes", respStatuses); attrs.put("responseTimeDist", responseTimeHistogram); - attrs.put("requestSendTimeDist", requestSendingTimeHistogram); - attrs.put("responseRecvTimeDist", responseReceivingTimeHistogram); - attrs.put("requestSizeDist", requestSizeHistogram); - attrs.put("responseSizeDist", responseSizeHistogram); - } + attrs.put("responseSizeHistogram", responseSizeHistogram); - @Override - public String getName() { - return name; - } - - @Override - public boolean isActive() { - return active; } @Override - public void activate() { + protected void doActivate() { + super.doActivate(); responseTimeHistogram.activate(); - requestSendingTimeHistogram.activate(); - responseReceivingTimeHistogram.activate(); - requestSizeHistogram.activate(); responseSizeHistogram.activate(); - active = true; } @Override - public void deactivate() { - if (!active) { - return; - } - - reset(); + protected void doDeactivate() { + super.doDeactivate(); responseTimeHistogram.deactivate(); - requestSendingTimeHistogram.deactivate(); - responseReceivingTimeHistogram.deactivate(); - requestSizeHistogram.deactivate(); responseSizeHistogram.deactivate(); - active = false; - } - - public void incRequestsNum() { - requestsNum.increment(); - } - - public void incResponsesNum() { - responsesNum.increment(); - } - - public int getRequestsNum() { - return requestsNum.intValue(); - } - - public int getResponsesNum() { - return responsesNum.intValue(); } public Map<Integer, MutableInt> getStatusCodes() { @@ -121,74 +60,23 @@ responseTimeHistogram.update(time); } - public void addRequestSendingTime(long time) { - requestSendingTimeHistogram.update(time); - } - - public void addResponseReceivingTime(long time) { - responseReceivingTimeHistogram.update(time); - } - - public void addRequestSize(long size) { - requestSizeHistogram.update(size); - } - public void addResponseSize(long size) { responseSizeHistogram.update(size); } @Override - public boolean hasAttribute(String name) { - return attrs.containsKey(name); - } - - @Override - public Set<String> getAttributesName() { - return attrs.keySet(); - } - - @Override - public Serializable getAttributeValue(String name) { - return attrs.get(name); - } - - @Override - public Map<String, Serializable> getAttributesValue() { - return Collections.unmodifiableMap(attrs); - } - - @Override public void reset() { - requestsNum.setValue(0); - responsesNum.setValue(0); respStatuses.clear(); responseTimeHistogram.reset(); - requestSendingTimeHistogram.reset(); - responseReceivingTimeHistogram.reset(); - requestSizeHistogram.reset(); responseSizeHistogram.reset(); } @Override public void update(Metric metric) { - if (!(metric instanceof HttpClientWorkerMetric)) { - throw new IllegalArgumentException(HttpClientWorkerMetric.class + " required."); - } - HttpClientWorkerMetric clientMetric = (HttpClientWorkerMetric) metric; - requestsNum.add(clientMetric.requestsNum); - responsesNum.add(clientMetric.responsesNum); Metric.updateCountMap(respStatuses, clientMetric.respStatuses); responseTimeHistogram.update(clientMetric.responseTimeHistogram); - requestSendingTimeHistogram.update(clientMetric.requestSendingTimeHistogram); - responseReceivingTimeHistogram.update(clientMetric.responseReceivingTimeHistogram); - requestSizeHistogram.update(clientMetric.requestSizeHistogram); responseSizeHistogram.update(clientMetric.responseSizeHistogram); } - @Override - public String toString() { - return "HttpClientMetric{" + "requestsNum=" + requestsNum + ", responsesNum=" + responsesNum + ", respStatuses=" + respStatuses + '}'; - } - }
--- a/stress-tester/src/main/java/com/passus/st/client/http/HttpFlowHandlerDataDecoder.java Mon Aug 19 10:24:58 2019 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/http/HttpFlowHandlerDataDecoder.java Mon Aug 19 12:25:05 2019 +0200 @@ -5,8 +5,8 @@ import com.passus.net.http.HttpFullMessageDecoder; import com.passus.net.http.HttpRequest; import com.passus.net.http.HttpResponse; +import com.passus.st.client.FlowContext; import com.passus.st.client.FlowHandlerDataDecoder; -import com.passus.st.client.FlowContext; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -81,10 +81,8 @@ if (handler.isCollectMetrics()) { synchronized (handler.metric) { - handler.metric.incResponsesNum(); handler.metric.addResponseStatusCode(resp.getStatus().getCode()); handler.metric.addResponseSize(decoder.getHeaderSize() + decoder.getContentSize()); - handler.metric.addResponseReceivingTime(now - flowContext.receivedStartTimestamp()); if (lastRequest != null) { handler.metric.addResponseTime(now - (long) lastRequest.getTag(TAG_TIME_START)); }
--- a/stress-tester/src/main/java/com/passus/st/client/pgsql/PgSqlFlowHandlerDataDecoder.java Mon Aug 19 10:24:58 2019 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/pgsql/PgSqlFlowHandlerDataDecoder.java Mon Aug 19 12:25:05 2019 +0200 @@ -52,17 +52,7 @@ ); } - //TODO Dorobic metryki - /*if (client.isCollectMetrics()) { - synchronized (client.metric) { - client.metric.incResponsesNum(); - client.metric.addResponseSize(decoder.getHeaderSize() + decoder.getContentSize()); - client.metric.addResponseReceivingTime(now - flowContext.receivedStartTimestamp()); - if (lastRequest != null) { - client.metric.addResponseTime(now - (long) lastRequest.getTag(TAG_TIME_START)); - } - } - }*/ + } return res;
--- a/stress-tester/src/main/java/com/passus/st/client/pgsql/PgSqlFlowHandlerNetflowFlowHandler.java Mon Aug 19 10:24:58 2019 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/pgsql/PgSqlFlowHandlerNetflowFlowHandler.java Mon Aug 19 12:25:05 2019 +0200 @@ -3,11 +3,7 @@ import com.passus.commons.Assert; import com.passus.commons.time.TimeAware; import com.passus.commons.time.TimeGenerator; -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.client.netflow.NetflowFlowHandlerDataEncoder; +import com.passus.st.client.*; import com.passus.st.metric.MetricsContainer; import static com.passus.st.Protocols.NETFLOW;