changeset 832:879035bd33f3

Global configuration in progress
author Devel 2
date Fri, 19 Jan 2018 14:10:16 +0100
parents 598bb5f76f4f
children 5935b04ed679
files stress-tester/src/main/java/com/passus/st/client/http/ReporterFileDestination.java stress-tester/src/main/java/com/passus/st/config/MetricsConfigurator.java stress-tester/src/main/java/com/passus/st/metric/MetricsNodeDefinitionCreator.java stress-tester/src/main/java/com/passus/st/plugin/PluginConstants.java stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java stress-tester/src/test/java/com/passus/st/config/MetricsConfiguratorTest.java stress-tester/src/test/java/com/passus/st/emitter/EmitterConfiguratorTest.java
diffstat 7 files changed, 88 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/ReporterFileDestination.java	Fri Jan 19 13:32:18 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/client/http/ReporterFileDestination.java	Fri Jan 19 14:10:16 2018 +0100
@@ -1,5 +1,6 @@
 package com.passus.st.client.http;
 
+import com.passus.commons.annotations.Plugin;
 import com.passus.commons.metric.MapMetric;
 import com.passus.commons.metric.Metric;
 import com.passus.commons.metric.MetricsCollection;
@@ -11,9 +12,9 @@
 import static com.passus.st.client.http.HttpConsts.TAG_HEADER_SIZE;
 import static com.passus.st.client.http.HttpConsts.TAG_TIME_END;
 import static com.passus.st.client.http.HttpConsts.TAG_TIME_START;
-import static com.passus.st.client.http.ReporterDestination.ALLOWED_REQ_HEADERS;
-import static com.passus.st.client.http.ReporterDestination.ALLOWED_RESP_HEADERS;
+
 import com.passus.st.emitter.SessionInfo;
+import com.passus.st.plugin.PluginConstants;
 import com.passus.st.reporter.ReporterImpl;
 import static com.passus.st.reporter.ReporterImpl.addValue;
 import java.io.File;
@@ -23,12 +24,16 @@
 import java.util.HashMap;
 import java.util.Map;
 
+@Plugin(name = ReporterFileDestination.TYPE, category = PluginConstants.CATEGORY_REPORTER_DESTINATION)
 public class ReporterFileDestination implements ReporterDestination {
 
+    public static final String TYPE = "file";
+
     private final PrintWriter reqFile;
     private final PrintWriter respFile;
     private final PrintWriter emitterFile;
 
+
     public ReporterFileDestination(String directory) throws IOException {
         this(new File(directory));
     }
--- a/stress-tester/src/main/java/com/passus/st/config/MetricsConfigurator.java	Fri Jan 19 13:32:18 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/config/MetricsConfigurator.java	Fri Jan 19 14:10:16 2018 +0100
@@ -1,25 +1,24 @@
 package com.passus.st.config;
 
 import com.passus.commons.annotations.Plugin;
+import com.passus.config.CCompositeNode;
 import com.passus.config.Configuration;
 import com.passus.config.ConfigurationContext;
 import com.passus.config.DomainConfigurator;
-import com.passus.config.schema.ConfigurationSchema;
-import static com.passus.config.schema.ConfigurationSchemaBuilder.dynaKeyValueVaryListDef;
-import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
-import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
-import com.passus.config.schema.ConfigurationSchemaImpl;
 import com.passus.config.schema.NodeDefinition;
 import com.passus.config.validation.Errors;
 import com.passus.st.metric.MetricsCollectionAppender;
-import com.passus.st.metric.MetricsCollectionAppenderFactory;
+import com.passus.st.metric.MetricsNodeDefinitionCreator;
+import com.passus.st.metric.ScheduledMetricsCollector;
 import com.passus.st.plugin.PluginConstants;
-import java.util.ArrayList;
+
 import java.util.List;
-import java.util.Set;
+
+import static com.passus.st.utils.ConfigurationContextConsts.METRICS_ACTIVE;
+import static com.passus.st.utils.ConfigurationContextConsts.METRICS_COLLECTION_APPENDERS;
+import static com.passus.st.utils.ConfigurationContextConsts.METRICS_COLLECTOR;
 
 /**
- *
  * @author Mirosław Hawrot
  */
 @Plugin(name = MetricsConfigurator.DOMAIN, category = PluginConstants.CATEGORY_DOMAIN_CONFIGURATOR)
@@ -27,56 +26,33 @@
 
     public static final String DOMAIN = "metrics";
 
-    private final MetricsCollectionAppenderFactory factory = MetricsCollectionAppenderFactory.getInstance();
-
-    private final ConfigurationSchema schema;
-
-    private List<MetricsCollectionAppender> appenders;
-
-    public MetricsConfigurator() {
-        schema = new ConfigurationSchemaImpl(createNodeDef());
-    }
+    private final NodeDefinition nodeDefinition = MetricsNodeDefinitionCreator.createDef();
 
     @Override
     public String getDomain() {
         return DOMAIN;
     }
 
-    public List<MetricsCollectionAppender> getAppenders() {
-        return appenders;
-    }
-
-    private NodeDefinition createNodeDef() {
-        return mapDef(
-                tupleDef("destinations",
-                        dynaKeyValueVaryListDef("type", factory)
-                )
-        );
-    }
-
     @Override
     public void validate(Configuration config, Errors errors, ConfigurationContext context) {
-        schema.validate(config, errors, null);
+        CCompositeNode rootNode = config.getRootNode();
+        nodeDefinition.validate(rootNode, errors, context);
     }
 
     @Override
     public void configure(Configuration config, Errors errors, ConfigurationContext context) {
-        Configuration tConfig = schema.transform(config, errors, null);
-        if (errors.getErrorCount() == 0) {
-            if (appenders == null) {
-                appenders = new ArrayList<>();
-            } else {
-                appenders.clear();
-            }
+        CCompositeNode rootNode = config.getRootNode();
+        nodeDefinition.transform(rootNode, errors, context);
 
-            Set<String> keys = tConfig.keys("destinations", false);
-            for (String key : keys) {
-                Configuration appenderConfig = tConfig.subConfiguration("destinations." + key);
-                MetricsCollectionAppender appender = factory.getInstanceByName(appenderConfig.getString("type"));
-                appender.configure(appenderConfig);
-                appenders.add(appender);
-            }
+        if (!errors.hasError()) {
+            boolean active = config.getBoolean("active", true);
+            List<MetricsCollectionAppender> appenders = (List<MetricsCollectionAppender>) config.get("destinations");
+
+            context.add(METRICS_ACTIVE, active);
+            context.add(METRICS_COLLECTION_APPENDERS, appenders);
+            context.add(METRICS_COLLECTOR, new ScheduledMetricsCollector());
         }
+
     }
 
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/metric/MetricsNodeDefinitionCreator.java	Fri Jan 19 14:10:16 2018 +0100
@@ -0,0 +1,27 @@
+package com.passus.st.metric;
+
+import com.passus.config.schema.DynaKeyValueVaryListNodeDefinition;
+import com.passus.config.schema.NodeDefinition;
+import com.passus.config.schema.NodeDefinitionCreator;
+
+import static com.passus.config.schema.ConfigurationSchemaBuilder.*;
+
+public class MetricsNodeDefinitionCreator implements NodeDefinitionCreator {
+
+    public static NodeDefinition createCollectionAppendersDef() {
+        return new DynaKeyValueVaryListNodeDefinition("type", MetricsCollectionAppenderFactory.getInstance())
+                .setTransformToPluginObject(true);
+    }
+
+    public static NodeDefinition createDef() {
+        return mapDef(
+                tupleDef("active", valueDefBool()).setRequired(false),
+                tupleDef("destinations", createCollectionAppendersDef())
+        );
+    }
+
+    @Override
+    public NodeDefinition create() {
+        return createDef();
+    }
+}
--- a/stress-tester/src/main/java/com/passus/st/plugin/PluginConstants.java	Fri Jan 19 13:32:18 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/plugin/PluginConstants.java	Fri Jan 19 14:10:16 2018 +0100
@@ -24,6 +24,8 @@
 
     public static final String CATEGORY_HTTP_CLIENT_WORKER = "ClientWorker";
 
+    public static final String CATEGORY_REPORTER_DESTINATION = "ReporterDestination";
+
     private PluginConstants() {
     }
 
--- a/stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java	Fri Jan 19 13:32:18 2018 +0100
+++ b/stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java	Fri Jan 19 14:10:16 2018 +0100
@@ -25,6 +25,12 @@
      */
     public static final String EMITTER_DEFAULT_EMITTER = "emitter.defaultEmitter";
 
+    public static final String METRICS_ACTIVE = "metrics.active";
+
+    public static final String METRICS_COLLECTION_APPENDERS = "metrics.collection_appenders";
+
+    public static final String METRICS_COLLECTOR = "metrics.collector";
+
     private ConfigurationContextConsts() {
     }
 
--- a/stress-tester/src/test/java/com/passus/st/config/MetricsConfiguratorTest.java	Fri Jan 19 13:32:18 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/config/MetricsConfiguratorTest.java	Fri Jan 19 14:10:16 2018 +0100
@@ -1,13 +1,19 @@
 package com.passus.st.config;
 
 import com.passus.config.Configuration;
+import com.passus.config.ConfigurationContext;
+import com.passus.config.ConfigurationContextImpl;
 import com.passus.config.YamlConfigurationReader;
 import com.passus.config.validation.Errors;
 import com.passus.st.metric.FileMetricsCollectionAppender;
 import com.passus.st.metric.MetricsCollectionAppender;
 import java.util.List;
+
+import static com.passus.st.utils.ConfigurationContextConsts.METRICS_COLLECTION_APPENDERS;
 import static org.testng.AssertJUnit.assertEquals;
 import static org.testng.AssertJUnit.assertTrue;
+
+import org.testng.annotations.AfterMethod;
 import org.testng.annotations.Test;
 
 /**
@@ -16,6 +22,13 @@
  */
 public class MetricsConfiguratorTest {
 
+    private final Errors errors = new Errors();
+
+    @AfterMethod
+    public void afterMethod() {
+        errors.clear();
+    }
+
     @Test
     public void testValidate() throws Exception {
         String metricsCfg = "destinations:\n"
@@ -25,27 +38,27 @@
         MetricsConfigurator configurator = new MetricsConfigurator();
         Configuration config = YamlConfigurationReader.readFromString(metricsCfg);
 
-        Errors errors = new Errors();
-        configurator.validate(config, errors, null);
+        ConfigurationContext context = new ConfigurationContextImpl();
+        configurator.validate(config, errors, context);
 
-        errors.getAllErrors().forEach(System.out::println);
         assertEquals(0, errors.getErrorCount());
     }
 
     @Test
     public void testConfigure() throws Exception {
-        String metricsCfg = "destinations:\n"
+        String metricsCfg = "active: false\n" +
+                "destinations:\n"
                 + "  - type: file\n"
                 + "    maxFileSize: 10 KiB\n"
                 + "    fileName: /file\n";
         MetricsConfigurator configurator = new MetricsConfigurator();
         Configuration config = YamlConfigurationReader.readFromString(metricsCfg);
 
-        Errors errors = new Errors();
-        configurator.configure(config, errors, null);
+        ConfigurationContext context = new ConfigurationContextImpl();
+        configurator.configure(config, errors, context);
         assertEquals(0, errors.getErrorCount());
 
-        List<MetricsCollectionAppender> appenders = configurator.getAppenders();
+        List<MetricsCollectionAppender> appenders = (List<MetricsCollectionAppender>) context.get(METRICS_COLLECTION_APPENDERS);
         assertEquals(1, appenders.size());
 
         assertTrue(appenders.get(0) instanceof FileMetricsCollectionAppender);
--- a/stress-tester/src/test/java/com/passus/st/emitter/EmitterConfiguratorTest.java	Fri Jan 19 13:32:18 2018 +0100
+++ b/stress-tester/src/test/java/com/passus/st/emitter/EmitterConfiguratorTest.java	Fri Jan 19 14:10:16 2018 +0100
@@ -1,17 +1,17 @@
 package com.passus.st.emitter;
 
-import com.passus.config.*;
+import com.passus.config.Configuration;
+import com.passus.config.ConfigurationContext;
+import com.passus.config.ConfigurationContextImpl;
+import com.passus.config.YamlConfigurationReader;
 import com.passus.config.validation.Errors;
 import com.passus.st.emitter.nio.NioEmitter;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.Test;
 
-import java.io.IOException;
 import java.util.List;
 
-import static com.passus.st.utils.ConfigurationContextConsts.EMITTER_DEFAULT_EMITTER;
-import static com.passus.st.utils.ConfigurationContextConsts.EMITTER_EMITTERS;
-import static com.passus.st.utils.ConfigurationContextConsts.EMITTER_SESSION_MAPPER;
+import static com.passus.st.utils.ConfigurationContextConsts.*;
 import static org.testng.Assert.*;
 
 public class EmitterConfiguratorTest {