Mercurial > stress-tester
changeset 1121:b45826b35c6e
CsvMetricsCollectionEncoder
author | Devel 2 |
---|---|
date | Wed, 03 Jun 2020 10:30:48 +0200 |
parents | 7f40755547ad |
children | b33418b1f310 |
files | stress-tester/src/main/java/com/passus/st/metric/CsvMetricsCollectionEncoder.java stress-tester/src/test/java/com/passus/st/metric/CsvMetricsCollectionEncoderTest.java stress-tester/src/test/java/com/passus/st/metric/JsonMetricsCollectionEncoderTest.java |
diffstat | 3 files changed, 153 insertions(+), 62 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/metric/CsvMetricsCollectionEncoder.java Wed Jun 03 10:30:48 2020 +0200 @@ -0,0 +1,127 @@ +package com.passus.st.metric; + +import com.passus.commons.Assert; +import com.passus.commons.EncoderException; +import com.passus.commons.metric.Metric; +import com.passus.commons.metric.MetricsCollection; +import com.passus.commons.utils.FormatUtils; + +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 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 String metricName; + + public CsvMetricsCollectionEncoder(String metricName) { + Assert.notNull(metricName, "metricName"); + this.metricName = metricName; + } + + 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; + } + + 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); + } + } + + @Override + public String encode(MetricsCollection mc) throws EncoderException { + Metric metric = mc.getMetric(metricName); + if (metric == null) { + return null; + } + + sb1.setLength(0); + appendValue(mc.getStartTimestamp(), sb1); + sb1.append(delimiter); + + appendValue(mc.getEndTimestamp(), 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(); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/test/java/com/passus/st/metric/CsvMetricsCollectionEncoderTest.java Wed Jun 03 10:30:48 2020 +0200 @@ -0,0 +1,24 @@ +package com.passus.st.metric; + +import com.passus.commons.metric.Metric; +import com.passus.commons.metric.MetricsCollection; +import org.testng.annotations.Test; + +import java.util.Arrays; + +import static org.testng.AssertJUnit.assertEquals; + +public class CsvMetricsCollectionEncoderTest { + + @Test + public void testEncode() throws Exception { + Metric metric = new TestMetric(); + + MetricsCollection coll = new MetricsCollection(0, 1, Arrays.asList(metric)); + CsvMetricsCollectionEncoder encoder = new CsvMetricsCollectionEncoder("test"); + + String expected = "0,1,testString,\"{key1=1, key2=value1, key3=null}\",1"; + assertEquals(expected, encoder.encode(coll)); + } + +} \ No newline at end of file
--- a/stress-tester/src/test/java/com/passus/st/metric/JsonMetricsCollectionEncoderTest.java Wed Jun 03 09:09:42 2020 +0200 +++ b/stress-tester/src/test/java/com/passus/st/metric/JsonMetricsCollectionEncoderTest.java Wed Jun 03 10:30:48 2020 +0200 @@ -1,12 +1,10 @@ package com.passus.st.metric; -import com.passus.commons.metric.GenericMetric; import com.passus.commons.metric.Metric; import com.passus.commons.metric.MetricsCollection; -import java.io.Serializable; + import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; + import static org.testng.AssertJUnit.*; import org.testng.annotations.Test; @@ -27,62 +25,4 @@ assertEquals(expected, jsonEncoder.encode(coll)); } - private static class TestMetric extends GenericMetric { - - public TestMetric() { - addAttribute("int", Integer.class); - addAttribute("string", String.class); - - addAttribute("map", Map.class); - } - - @Override - public String getName() { - return "test"; - } - - @Override - public boolean isActive() { - return true; - } - - @Override - public void activate() { - - } - - @Override - public void deactivate() { - - } - - @Override - public Serializable getAttributeValue(String name) { - switch (name) { - case "int": - return 1; - case "string": - return "testString"; - case "map": - HashMap<String, Object> hashMap = new HashMap<>(); - hashMap.put("key1", 1); - hashMap.put("key2", "value1"); - hashMap.put("key3", null); - return hashMap; - default: - return null; - } - } - - @Override - public void update(Metric metric) { - - } - - @Override - public void reset() { - - } - - } }