changeset 1146:c25812252c9d

MetricsCollectionsEncoder - metric synchronization bugfix
author Devel 2
date Fri, 12 Jun 2020 10:34:19 +0200
parents 20c663622c30
children 2d394f066c80
files stress-tester/src/main/java/com/passus/st/metric/CsvMetricsCollectionEncoder.java stress-tester/src/main/java/com/passus/st/metric/JsonMetricsCollectionEncoder.java stress-tester/src/main/java/com/passus/st/metric/TextMetricCollectionEncoder.java
diffstat 3 files changed, 33 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/metric/CsvMetricsCollectionEncoder.java	Wed Jun 10 15:07:57 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/metric/CsvMetricsCollectionEncoder.java	Fri Jun 12 10:34:19 2020 +0200
@@ -135,18 +135,18 @@
         sb1.setLength(0);
         appendValue(mc.getStartTimestamp(), sb1);
         sb1.append(delimiter);
-
         appendValue(mc.getEndTimestamp(), sb1);
+        synchronized (metric) {
+            Set<String> names = metric.getAttributesName();
+            int i = 0;
+            for (String name : names) {
+                sb1.append(delimiter);
+                Object value = metric.getAttributeValue(name);
+                appendValue(value, sb1);
+            }
 
-        Set<String> names = metric.getAttributesName();
-        int i = 0;
-        for (String name : names) {
-            sb1.append(delimiter);
-            Object value = metric.getAttributeValue(name);
-            appendValue(value, sb1);
+            return sb1.toString();
         }
-
-        return sb1.toString();
     }
 
 }
--- a/stress-tester/src/main/java/com/passus/st/metric/JsonMetricsCollectionEncoder.java	Wed Jun 10 15:07:57 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/metric/JsonMetricsCollectionEncoder.java	Fri Jun 12 10:34:19 2020 +0200
@@ -4,6 +4,7 @@
 import com.passus.commons.EncoderException;
 import com.passus.commons.metric.Metric;
 import com.passus.commons.metric.MetricsCollection;
+
 import java.io.IOException;
 import java.io.StringWriter;
 import java.io.Writer;
@@ -13,11 +14,10 @@
 import java.util.Set;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 public class JsonMetricsCollectionEncoder implements MetricsCollectionEncoder<String> {
-
+    
     private static void writeScalar(JsonWriter jsonWriter, Object value) throws IOException {
         if (value == null) {
             jsonWriter.nullValue();
@@ -58,22 +58,24 @@
 
     public static String encodeMetrics(MetricsCollection mc) throws EncoderException {
         try (Writer writer = new StringWriter();
-                JsonWriter jsonWriter = new JsonWriter(writer)) {
+             JsonWriter jsonWriter = new JsonWriter(writer)) {
             jsonWriter.beginObject();
             jsonWriter.name("@startTime").value(mc.getStartTimestamp());
             jsonWriter.name("@endTime").value(mc.getEndTimestamp());
             List<Metric> metrics = mc.getMetrics();
             for (Metric metric : metrics) {
-                jsonWriter.name(metric.getName());
-                jsonWriter.beginObject();
-                Set<String> names = metric.getAttributesName();
-                for (String name : names) {
-                    jsonWriter.name(name);
-                    Object value = metric.getAttributeValue(name);
-                    writeValue(jsonWriter, value);
+                synchronized (metric) {
+                    jsonWriter.name(metric.getName());
+                    jsonWriter.beginObject();
+                    Set<String> names = metric.getAttributesName();
+                    for (String name : names) {
+                        jsonWriter.name(name);
+                        Object value = metric.getAttributeValue(name);
+                        writeValue(jsonWriter, value);
+                    }
+
+                    jsonWriter.endObject();
                 }
-
-                jsonWriter.endObject();
             }
             jsonWriter.endObject();
 
--- a/stress-tester/src/main/java/com/passus/st/metric/TextMetricCollectionEncoder.java	Wed Jun 10 15:07:57 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/metric/TextMetricCollectionEncoder.java	Fri Jun 12 10:34:19 2020 +0200
@@ -4,12 +4,12 @@
 import com.passus.commons.metric.Metric;
 import com.passus.commons.metric.MetricsCollection;
 import com.passus.st.utils.PeriodFormatter;
+
 import java.io.Serializable;
 import java.io.StringWriter;
 import java.util.Map;
 
 /**
- *
  * @author mikolaj.podbielski
  */
 public class TextMetricCollectionEncoder implements MetricsCollectionEncoder<String> {
@@ -18,7 +18,7 @@
     public String encode(MetricsCollection collection) {
         return encodeMetrics(collection);
     }
-    
+
     public static String encodeMetrics(MetricsCollection collection) {
         StringWriter sw = new StringWriter();
 
@@ -31,13 +31,15 @@
 
         String line = "%24s: %s\n";
         for (Metric metric : collection.getMetrics()) {
-            sw.append(metric.getName() + ":\n");
-            Map<String, Serializable> valuesMap = metric.getAttributesValue();
-            for (Map.Entry<String, Serializable> entry : valuesMap.entrySet()) {
-                String key = entry.getKey();
-                Serializable value = entry.getValue();
+            synchronized (metric) {
+                sw.append(metric.getName() + ":\n");
+                Map<String, Serializable> valuesMap = metric.getAttributesValue();
+                for (Map.Entry<String, Serializable> entry : valuesMap.entrySet()) {
+                    String key = entry.getKey();
+                    Serializable value = entry.getValue();
 
-                sw.append(String.format(line, key, value));
+                    sw.append(String.format(line, key, value));
+                }
             }
         }