Mercurial > stress-tester
changeset 1141:c4c4625a8523
GlobalConfigMain - metric format
author | Devel 1 |
---|---|
date | Wed, 10 Jun 2020 10:37:05 +0200 |
parents | 3f1884efeb15 |
children | bd85413f06cb |
files | stress-tester/src/main/java/com/passus/st/GlobalConfigMain.java stress-tester/src/main/java/com/passus/st/Main.java stress-tester/src/main/java/com/passus/st/metric/JsonMetricsCollectionEncoder.java stress-tester/src/main/java/com/passus/st/metric/TextMetricCollectionEncoder.java |
diffstat | 4 files changed, 74 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/GlobalConfigMain.java Wed Jun 10 10:05:41 2020 +0200 +++ b/stress-tester/src/main/java/com/passus/st/GlobalConfigMain.java Wed Jun 10 10:37:05 2020 +0200 @@ -27,6 +27,7 @@ private Options createOptions() { CliOptions options = cliHelper.options(); options.addLogLevelOption(); + options.addOption("mf", "metric-format", true, "[text]|json"); return options; } @@ -53,6 +54,8 @@ } cliHelper.configureLogger(cl); + final String metricFormat = cl.getOptionValue("mf", "text"); + Errors errors = new Errors(); File configFile = new File(clArgs[0]); Configuration config = YamlConfigurationReader.readFromFile(configFile); @@ -78,7 +81,7 @@ if (metricsCollector != null) { metricsCollector.addHandler(summMetricsHandler); executor = Executors.newSingleThreadScheduledExecutor(); - executor.scheduleAtFixedRate(() -> Main.printMetrics(getMetrics(), startTime), 5, 5, TimeUnit.SECONDS); + executor.scheduleAtFixedRate(() -> Main.printMetrics(getMetrics(), startTime, metricFormat), 5, 5, TimeUnit.SECONDS); } else { executor = null; } @@ -94,7 +97,7 @@ executor.shutdownNow(); metricsCollector.collect(); metricsCollector.flush(true); - Main.printMetrics(getMetrics(), startTime); + Main.printMetrics(getMetrics(), startTime, metricFormat); } testJob.stop();
--- a/stress-tester/src/main/java/com/passus/st/Main.java Wed Jun 10 10:05:41 2020 +0200 +++ b/stress-tester/src/main/java/com/passus/st/Main.java Wed Jun 10 10:37:05 2020 +0200 @@ -1,6 +1,6 @@ package com.passus.st; -import com.passus.commons.ConversionException; +import com.passus.commons.EncoderException; import com.passus.commons.metric.Metric; import com.passus.commons.metric.MetricsCollection; import com.passus.commons.service.Registry; @@ -11,9 +11,11 @@ import com.passus.st.emitter.nio.NioEmitter; import com.passus.st.filter.FlowFilter; import com.passus.st.metric.FileMetricsCollectionAppender; +import com.passus.st.metric.JsonMetricsCollectionEncoder; import com.passus.st.metric.MetricSource; import com.passus.st.metric.ScheduledMetricsCollector; import com.passus.st.metric.SummaryMetricsCollectionHandler; +import com.passus.st.metric.TextMetricCollectionEncoder; import com.passus.st.source.EventSource; import com.passus.st.utils.PeriodFormatter; import org.apache.commons.cli.*; @@ -21,7 +23,6 @@ import java.io.File; import java.io.FileOutputStream; -import java.io.Serializable; import java.util.*; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -51,26 +52,22 @@ } static void printMetrics(MetricsCollection collection, String format) { - long endTime = System.currentTimeMillis(); - synchronized (System.out) { - System.out.println(""); - try { - System.out.println("Elapsed time: " + PeriodFormatter.INSTANCE.reverseTransform(collection.getEndTimestamp() - collection.getStartTimestamp()) + "."); - } catch (ConversionException ignored) { + String s; + try { + switch (format) { + case "json": + s = JsonMetricsCollectionEncoder.encodeMetrics(collection); + break; + default: + s = TextMetricCollectionEncoder.encodeMetrics(collection); + break; } - System.out.println("Metrics:"); - - String line = "%24s: %s\n"; - for (Metric metric : collection.getMetrics()) { - System.out.println(metric.getName() + ":"); - Map<String, Serializable> valuesMap = metric.getAttributesValue(); - for (Map.Entry<String, Serializable> entry : valuesMap.entrySet()) { - String key = entry.getKey(); - Serializable value = entry.getValue(); - - System.out.printf(line, key, value); - } + synchronized (System.out) { + System.out.println(""); + System.out.println(s); } + } catch (EncoderException ex) { + System.err.println("Could not write metrics."); } }
--- a/stress-tester/src/main/java/com/passus/st/metric/JsonMetricsCollectionEncoder.java Wed Jun 10 10:05:41 2020 +0200 +++ b/stress-tester/src/main/java/com/passus/st/metric/JsonMetricsCollectionEncoder.java Wed Jun 10 10:37:05 2020 +0200 @@ -18,11 +18,7 @@ */ public class JsonMetricsCollectionEncoder implements MetricsCollectionEncoder<String> { - public JsonMetricsCollectionEncoder() { - - } - - private void writeScalar(JsonWriter jsonWriter, Object value) throws IOException { + private static void writeScalar(JsonWriter jsonWriter, Object value) throws IOException { if (value == null) { jsonWriter.nullValue(); } else if (value instanceof Number) { @@ -32,7 +28,7 @@ } } - private void writeValue(JsonWriter jsonWriter, Object value) throws IOException { + private static void writeValue(JsonWriter jsonWriter, Object value) throws IOException { if (value instanceof Collection) { Collection coll = (Collection) value; jsonWriter.beginArray(); @@ -57,6 +53,10 @@ @Override public String encode(MetricsCollection mc) throws EncoderException { + return encodeMetrics(mc); + } + + public static String encodeMetrics(MetricsCollection mc) throws EncoderException { try (Writer writer = new StringWriter(); JsonWriter jsonWriter = new JsonWriter(writer)) { jsonWriter.beginObject();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/metric/TextMetricCollectionEncoder.java Wed Jun 10 10:37:05 2020 +0200 @@ -0,0 +1,46 @@ +package com.passus.st.metric; + +import com.passus.commons.ConversionException; +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> { + + @Override + public String encode(MetricsCollection collection) { + return encodeMetrics(collection); + } + + public static String encodeMetrics(MetricsCollection collection) { + StringWriter sw = new StringWriter(); + + try { + String time = PeriodFormatter.INSTANCE.reverseTransform(collection.getEndTimestamp() - collection.getStartTimestamp()); + sw.append("Elapsed time: ").append(time).append(".\n"); + } catch (ConversionException ignored) { + } + sw.append("Metrics:\n"); + + 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(); + + sw.append(String.format(line, key, value)); + } + } + + return sw.toString(); + } +}