changeset 1148:2a19b0b893d9

CsvHelper
author Devel 2
date Fri, 12 Jun 2020 11:40:25 +0200
parents 2d394f066c80
children fd3c71a0ec25
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/utils/CsvHelper.java stress-tester/src/test/java/com/passus/st/metric/CsvMetricsCollectionEncoderTest.java
diffstat 4 files changed, 163 insertions(+), 81 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/metric/CsvMetricsCollectionEncoder.java	Fri Jun 12 10:35:10 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/metric/CsvMetricsCollectionEncoder.java	Fri Jun 12 11:40:25 2020 +0200
@@ -4,31 +4,16 @@
 import com.passus.commons.EncoderException;
 import com.passus.commons.metric.Metric;
 import com.passus.commons.metric.MetricsCollection;
-import com.passus.commons.utils.FormatUtils;
+import com.passus.st.utils.CsvHelper;
 
-import java.text.DecimalFormat;
 import java.util.Set;
 
 public class CsvMetricsCollectionEncoder implements MetricsCollectionEncoder<String> {
 
-    public static final char DEFAULT_ESCAPE_CHARACTER = '\\';
-
-    public static final char DEFAULT_DELIMITER = ',';
-
-    public static final char DEFAULT_QUOTE_CHARACTER = '"';
-
-    private char delimiter = DEFAULT_DELIMITER;
+    private final CsvHelper csvHelper = new CsvHelper();
 
-    private char quotechar = DEFAULT_QUOTE_CHARACTER;
-
-    private char escapechar = DEFAULT_ESCAPE_CHARACTER;
-
-    private DecimalFormat decimalFormat = FormatUtils.decimalFormat(4);
-
-    private StringBuilder sb1 = new StringBuilder();
-
-    private StringBuilder sb2 = new StringBuilder();
-
+    private final StringBuilder sb = new StringBuilder();
+    
     private final String metricName;
 
     public CsvMetricsCollectionEncoder(String metricName) {
@@ -41,67 +26,27 @@
     }
 
     public char getDelimiter() {
-        return delimiter;
+        return csvHelper.getDelimiter();
     }
 
     public void setDelimiter(char delimiter) {
-        this.delimiter = delimiter;
+        csvHelper.setDelimiter(delimiter);
     }
 
     public char getQuotechar() {
-        return quotechar;
+        return csvHelper.getQuotechar();
     }
 
     public void setQuotechar(char quotechar) {
-        this.quotechar = quotechar;
+        csvHelper.setQuotechar(quotechar);
     }
 
     public char getEscapechar() {
-        return escapechar;
+        return csvHelper.getEscapechar();
     }
 
     public void setEscapechar(char escapechar) {
-        this.escapechar = escapechar;
-    }
-
-    private boolean containsSpecialCharacter(String line) {
-        return line.indexOf(quotechar) != -1 || line.indexOf(escapechar) != -1 || line.indexOf(delimiter) != -1;
-    }
-
-    private void escapeChars(String value, StringBuilder out) {
-        for (int j = 0; j < value.length(); j++) {
-            char ch = value.charAt(j);
-            if (ch == quotechar || ch == escapechar) {
-                out.append(escapechar).append(ch);
-            } else {
-                out.append(ch);
-            }
-        }
-    }
-
-    private void appendValue(Object value, StringBuilder out) {
-        if (value != null) {
-            if (decimalFormat != null && (value instanceof Double || value instanceof Float)) {
-                Number num = (Number) value;
-                String strValue = decimalFormat.format(num.doubleValue());
-                sb1.append(strValue);
-            } else {
-                String strValue = value.toString();
-                appendStringValue(strValue, sb1);
-            }
-        }
-    }
-
-    private void appendStringValue(String value, StringBuilder out) {
-        if (containsSpecialCharacter(value)) {
-            sb2.setLength(0);
-            out.append(quotechar);
-            escapeChars(value, sb2);
-            out.append(sb2.toString());
-            out.append(quotechar);
-        } else {
-            out.append(value);
-        }
+        csvHelper.setEscapechar(escapechar);
     }
 
     public String encodeHeaders(MetricsCollection mc) {
@@ -110,19 +55,19 @@
             return null;
         }
 
-        sb1.setLength(0);
+        sb.setLength(0);
 
-        appendStringValue("startTime", sb1);
-        sb1.append(delimiter);
-
-        appendStringValue("endTime", sb1);
+        csvHelper.appendString("startTime", sb)
+                .appendDelimiter(sb)
+                .appendString("endTime", sb);
 
         Set<String> names = metric.getAttributesName();
         for (String name : names) {
-            sb1.append(delimiter);
-            appendStringValue(name, sb1);
+            csvHelper.appendDelimiter(sb)
+                    .appendString(name, sb);
         }
-        return sb1.toString();
+
+        return sb.toString();
     }
 
     @Override
@@ -132,20 +77,21 @@
             return null;
         }
 
-        sb1.setLength(0);
-        appendValue(mc.getStartTimestamp(), sb1);
-        sb1.append(delimiter);
-        appendValue(mc.getEndTimestamp(), sb1);
+        sb.setLength(0);
+        csvHelper.append(mc.getStartTimestamp(), sb)
+                .appendDelimiter(sb)
+                .append(mc.getEndTimestamp(), sb);
+
         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);
+                csvHelper.appendDelimiter(sb)
+                        .append(value, sb);
             }
 
-            return sb1.toString();
+            return sb.toString();
         }
     }
 
--- a/stress-tester/src/main/java/com/passus/st/metric/JsonMetricsCollectionEncoder.java	Fri Jun 12 10:35:10 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/metric/JsonMetricsCollectionEncoder.java	Fri Jun 12 11:40:25 2020 +0200
@@ -17,7 +17,7 @@
  * @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();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/utils/CsvHelper.java	Fri Jun 12 11:40:25 2020 +0200
@@ -0,0 +1,135 @@
+package com.passus.st.utils;
+
+import com.passus.commons.utils.FormatUtils;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.text.DecimalFormat;
+
+public class CsvHelper {
+
+    public static final char DEFAULT_ESCAPE_CHARACTER = '\\';
+
+    public static final char DEFAULT_DELIMITER = ',';
+
+    public static final char DEFAULT_QUOTE_CHARACTER = '"';
+
+    private char delimiter = DEFAULT_DELIMITER;
+
+    private char quotechar = DEFAULT_QUOTE_CHARACTER;
+
+    private char escapechar = DEFAULT_ESCAPE_CHARACTER;
+
+    private DecimalFormat decimalFormat = FormatUtils.decimalFormat(4);
+
+    private final StringBuilder sb = new StringBuilder();
+
+    public char getDelimiter() {
+        return delimiter;
+    }
+
+    public void setDelimiter(char delimiter) {
+        this.delimiter = delimiter;
+    }
+
+    public char getQuotechar() {
+        return quotechar;
+    }
+
+    public void setQuotechar(char quotechar) {
+        this.quotechar = quotechar;
+    }
+
+    public char getEscapechar() {
+        return escapechar;
+    }
+
+    public void setEscapechar(char escapechar) {
+        this.escapechar = escapechar;
+    }
+
+    private boolean containsSpecialCharacter(String line) {
+        return line.indexOf(quotechar) != -1 || line.indexOf(escapechar) != -1 || line.indexOf(delimiter) != -1;
+    }
+
+    public CsvHelper appendDelimiter(StringBuilder out) {
+        out.append(delimiter);
+        return this;
+    }
+
+    private void escapeChars(String value, StringBuilder out) {
+        for (int j = 0; j < value.length(); j++) {
+            char ch = value.charAt(j);
+            if (ch == quotechar || ch == escapechar) {
+                out.append(escapechar).append(ch);
+            } else {
+                out.append(ch);
+            }
+        }
+    }
+
+    public CsvHelper appendString(String value, StringBuilder out) {
+        if (containsSpecialCharacter(value)) {
+            sb.setLength(0);
+            out.append(quotechar);
+            escapeChars(value, sb);
+            out.append(sb.toString());
+            out.append(quotechar);
+        } else {
+            out.append(value);
+        }
+
+        return this;
+    }
+
+    public CsvHelper append(Object value, StringBuilder out) {
+        if (value != null) {
+            if (decimalFormat != null && (value instanceof Double || value instanceof Float)) {
+                Number num = (Number) value;
+                String strValue = decimalFormat.format(num.doubleValue());
+                out.append(strValue);
+            } else {
+                String strValue = value.toString();
+                appendString(strValue, out);
+            }
+        }
+
+        return this;
+    }
+
+    private int writeString(String value, Writer writer) throws IOException {
+        if (containsSpecialCharacter(value)) {
+            int written = 0;
+            sb.setLength(0);
+            writer.write(quotechar);
+            escapeChars(value, sb);
+
+            String escaped = sb.toString();
+            writer.write(escaped);
+            writer.write(quotechar);
+
+            written += escaped.length();
+            written += 2;
+            return written;
+        } else {
+            writer.write(value);
+            return value.length();
+        }
+    }
+
+    public int write(Object value, Writer writer) throws IOException {
+        if (value != null) {
+            if (decimalFormat != null && (value instanceof Double || value instanceof Float)) {
+                Number num = (Number) value;
+                String strValue = decimalFormat.format(num.doubleValue());
+                writer.write(strValue);
+                return strValue.length();
+            } else {
+                String strValue = value.toString();
+                return writeString(strValue, writer);
+            }
+        }
+
+        return 0;
+    }
+}
--- a/stress-tester/src/test/java/com/passus/st/metric/CsvMetricsCollectionEncoderTest.java	Fri Jun 12 10:35:10 2020 +0200
+++ b/stress-tester/src/test/java/com/passus/st/metric/CsvMetricsCollectionEncoderTest.java	Fri Jun 12 11:40:25 2020 +0200
@@ -19,6 +19,7 @@
 
         String expected = "0,1,testString,\"{key1=1, key2=value1, key3=null}\",1";
         assertEquals(expected, encoder.encode(coll));
+        assertEquals(expected, encoder.encode(coll));
     }
 
     @Test