changeset 1143:1947030b8b3d

Scanner - metric-format option
author Devel 2
date Wed, 10 Jun 2020 10:48:44 +0200
parents bd85413f06cb
children dbb7bc5daf60
files stress-tester/src/main/java/com/passus/st/CliOptions.java 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/PcapReporter.java stress-tester/src/main/java/com/passus/st/metric/MetricUtils.java stress-tester/src/main/java/com/passus/st/scanner/Scanner.java
diffstat 6 files changed, 50 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/CliOptions.java	Wed Jun 10 10:37:17 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/CliOptions.java	Wed Jun 10 10:48:44 2020 +0200
@@ -45,6 +45,13 @@
         );
     }
 
+    public CliOptions addMetricsFormatOption() {
+        return addOption(option("mf", "metric-format").desc("[text]|json")
+                .hasArg().argName("format")
+                .build()
+        );
+    }
+
     public CliOptions addMapperRuleOption() {
         return addOption(option("mr", "mapperRule").desc("Session mapper rule.")
                 .hasArg().argName("rule")
--- a/stress-tester/src/main/java/com/passus/st/GlobalConfigMain.java	Wed Jun 10 10:37:17 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/GlobalConfigMain.java	Wed Jun 10 10:48:44 2020 +0200
@@ -10,14 +10,16 @@
 import com.passus.st.metric.MetricsCollector;
 import com.passus.st.metric.SummaryMetricsCollectionHandler;
 import org.apache.commons.cli.*;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 
 import java.io.File;
 import java.util.List;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
+
+import static com.passus.st.metric.MetricUtils.printMetrics;
 
 public class GlobalConfigMain {
 
@@ -27,7 +29,7 @@
     private Options createOptions() {
         CliOptions options = cliHelper.options();
         options.addLogLevelOption();
-        options.addOption("mf", "metric-format", true, "[text]|json");
+        options.addMetricsFormatOption();
         return options;
     }
 
@@ -55,7 +57,7 @@
 
             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);
@@ -81,7 +83,7 @@
             if (metricsCollector != null) {
                 metricsCollector.addHandler(summMetricsHandler);
                 executor = Executors.newSingleThreadScheduledExecutor();
-                executor.scheduleAtFixedRate(() -> Main.printMetrics(getMetrics(), startTime, metricFormat), 5, 5, TimeUnit.SECONDS);
+                executor.scheduleAtFixedRate(() -> printMetrics(getMetrics(), startTime, metricFormat), 5, 5, TimeUnit.SECONDS);
             } else {
                 executor = null;
             }
@@ -97,7 +99,7 @@
                 executor.shutdownNow();
                 metricsCollector.collect();
                 metricsCollector.flush(true);
-                Main.printMetrics(getMetrics(), startTime, metricFormat);
+                printMetrics(getMetrics(), startTime, metricFormat);
             }
 
             testJob.stop();
--- a/stress-tester/src/main/java/com/passus/st/Main.java	Wed Jun 10 10:37:17 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/Main.java	Wed Jun 10 10:48:44 2020 +0200
@@ -1,8 +1,6 @@
 package com.passus.st;
 
-import com.passus.commons.EncoderException;
 import com.passus.commons.metric.Metric;
-import com.passus.commons.metric.MetricsCollection;
 import com.passus.commons.service.Registry;
 import com.passus.config.ConfigurationContext;
 import com.passus.st.client.*;
@@ -10,12 +8,7 @@
 import com.passus.st.emitter.SessionMapper;
 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.metric.*;
 import com.passus.st.source.EventSource;
 import com.passus.st.utils.PeriodFormatter;
 import org.apache.commons.cli.*;
@@ -51,39 +44,6 @@
         System.exit(1);
     }
 
-    static void printMetrics(MetricsCollection collection, String format) {
-        String s;
-        try {
-            switch (format) {
-                case "json":
-                    s = JsonMetricsCollectionEncoder.encodeMetrics(collection);
-                    break;
-                default:
-                    s = TextMetricCollectionEncoder.encodeMetrics(collection);
-                    break;
-            }
-            synchronized (System.out) {
-                System.out.println("");
-                System.out.println(s);
-            }
-        } catch (EncoderException ex) {
-            System.err.println("Could not write metrics.");
-        }
-    }
-
-    static void printMetrics(List<Metric> metrics, long startTime) {
-        printMetrics(metrics, startTime, "text");
-    }
-
-    static void printMetrics(List<Metric> metrics, long startTime, String format) {
-        if (startTime == 0) {
-            return;
-        }
-
-        long endTime = System.currentTimeMillis();
-        MetricsCollection collection = new MetricsCollection(startTime, endTime, metrics);
-        printMetrics(collection, format);
-    }
 
     private Options createOptions() {
         CliOptions options = cliHelper.options();
@@ -145,7 +105,7 @@
 
     void printMetrics() {
         List<Metric> metrics = summMetricsHandler.getMetrics();
-        printMetrics(metrics, startTime);
+        MetricUtils.printMetrics(metrics, startTime);
     }
 
     List<Metric> getMetrics() {
--- a/stress-tester/src/main/java/com/passus/st/PcapReporter.java	Wed Jun 10 10:37:17 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/PcapReporter.java	Wed Jun 10 10:48:44 2020 +0200
@@ -32,6 +32,7 @@
 import static com.passus.st.client.http.HttpConsts.*;
 import static com.passus.st.client.http.filter.HttpFlowUtils.createFlowContext;
 import static com.passus.st.client.http.filter.HttpFlowUtils.extractHttpContext;
+import static com.passus.st.metric.MetricUtils.printMetrics;
 import static com.passus.st.utils.CliUtils.option;
 
 /**
--- a/stress-tester/src/main/java/com/passus/st/metric/MetricUtils.java	Wed Jun 10 10:37:17 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/metric/MetricUtils.java	Wed Jun 10 10:48:44 2020 +0200
@@ -1,7 +1,9 @@
 package com.passus.st.metric;
 
 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.st.utils.PeriodFormatter;
 
 import java.io.Serializable;
@@ -13,31 +15,37 @@
     private MetricUtils() {
     }
 
+    public static void printMetrics(MetricsCollection collection, String format) {
+        String s;
+        try {
+            switch (format) {
+                case "json":
+                    s = JsonMetricsCollectionEncoder.encodeMetrics(collection);
+                    break;
+                default:
+                    s = TextMetricCollectionEncoder.encodeMetrics(collection);
+                    break;
+            }
+            synchronized (System.out) {
+                System.out.println("");
+                System.out.println(s);
+            }
+        } catch (EncoderException ex) {
+            System.err.println("Could not write metrics.");
+        }
+    }
+
     public static void printMetrics(List<Metric> metrics, long startTime) {
+        printMetrics(metrics, startTime, "text");
+    }
+
+    public static void printMetrics(List<Metric> metrics, long startTime, String format) {
         if (startTime == 0) {
             return;
         }
 
         long endTime = System.currentTimeMillis();
-        synchronized (System.out) {
-            System.out.println("");
-            try {
-                System.out.println("Elapsed time: " + PeriodFormatter.INSTANCE.reverseTransform(endTime - startTime) + ".");
-            } catch (ConversionException ignored) {
-            }
-            System.out.println("Metrics:");
-
-            String line = "%24s: %s\n";
-            for (Metric metric : metrics) {
-                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);
-                }
-            }
-        }
+        MetricsCollection collection = new MetricsCollection(startTime, endTime, metrics);
+        printMetrics(collection, format);
     }
 }
--- a/stress-tester/src/main/java/com/passus/st/scanner/Scanner.java	Wed Jun 10 10:37:17 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/scanner/Scanner.java	Wed Jun 10 10:48:44 2020 +0200
@@ -29,6 +29,7 @@
     private Options createOptions() {
         CliOptions options = cliHelper.options();
         options.addLogLevelOption();
+        options.addMetricsFormatOption();
         return options;
     }
 
@@ -55,6 +56,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);
@@ -89,7 +92,7 @@
 
             testJobConfigurator.destroy(config, errors, context);
 
-            printMetrics(getMetrics(), startTime);
+            printMetrics(getMetrics(), startTime, metricFormat);
             logger.debug("Scanner stopped");
         } catch (ParseException e) {
             System.out.println(e.getMessage());