Mercurial > stress-tester
changeset 964:d1e54d1d3e1e
Refactorization in progress
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/FlowExecutor.java Fri May 31 10:53:30 2019 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/FlowExecutor.java Fri May 31 11:37:28 2019 +0200 @@ -177,11 +177,18 @@ @Override public void configure(Configuration config, ConfigurationContext context) { setFilters((List<FlowFilter>) config.get("filters", Collections.EMPTY_LIST)); - //setListeners((List<ClientListener>) config.get("listeners", Collections.EMPTY_LIST)); + + List<ClientListener> cfgListeners = config.get("listeners", Collections.EMPTY_LIST); + if (cfgListeners.size() == 1) { + setListener(cfgListeners.get(0)); + } else if (cfgListeners.size() > 1) { + setListener(new MultipleClientListener(cfgListeners)); + } + setWorkersNum(config.getInteger("workers", DEFAULT_WORKERS_NUM)); connectPartialSession = config.getBoolean("connectPartialSession", DEFAULT_CONNECT_PARTIAL_SESSION); collectMetrics = config.getBoolean("collectMetrics", DEFAULT_COLLECT_METRICS); - dispatcher = (FlowWorkerDispatcher) config.get("dispatcher", null); + dispatcher = config.get("dispatcher", null); } @Override
--- a/stress-tester/src/main/java/com/passus/st/client/FlowFiltersConfigurator.java Fri May 31 10:53:30 2019 +0200 +++ b/stress-tester/src/main/java/com/passus/st/client/FlowFiltersConfigurator.java Fri May 31 11:37:28 2019 +0200 @@ -4,7 +4,6 @@ import com.passus.config.*; import com.passus.config.schema.NodeDefinition; import com.passus.config.validation.Errors; -import com.passus.st.client.http.filter.HttpFilter; import com.passus.st.plugin.PluginConstants; import com.passus.st.utils.ConfigurationContextConsts; @@ -56,8 +55,8 @@ nodeDef.transform(rootNode, errors, context); if (!errors.hasError()) { try { - List<HttpFilter> filters = ConfigurationUtils.convertToList(rootNode, HttpFilter.class); - context.add(ConfigurationContextConsts.HTTP_FILTERS, filters); + List<FlowFilter> filters = ConfigurationUtils.convertToList(rootNode, FlowFilter.class); + context.add(ConfigurationContextConsts.FLOW_FILTERS, filters); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); }
--- a/stress-tester/src/main/java/com/passus/st/config/ClientConfigurator.java Fri May 31 10:53:30 2019 +0200 +++ b/stress-tester/src/main/java/com/passus/st/config/ClientConfigurator.java Fri May 31 11:37:28 2019 +0200 @@ -1,6 +1,5 @@ package com.passus.st.config; -import com.passus.commons.Assert; import com.passus.commons.annotations.Plugin; import com.passus.config.CNode; import com.passus.config.Configuration; @@ -9,14 +8,10 @@ import com.passus.config.schema.NodeDefinition; import com.passus.config.schema.NodeDefinitionCreator; import com.passus.config.validation.Errors; -import com.passus.st.client.Client; -import com.passus.st.client.http.HttpClient; +import com.passus.st.client.FlowExecutor; import com.passus.st.plugin.PluginConstants; import com.passus.st.utils.ConfigurationContextConsts; -import java.util.ArrayList; -import java.util.List; - /** * @author Mirosław Hawrot */ @@ -25,7 +20,7 @@ public static final String DOMAIN = "client"; - private ClientNodeDefinitionCreator creator = new ClientNodeDefinitionCreator(); + private NodeDefinitionCreator creator = new FlowExecutor.FlowExecutorNodeDefCreator(); private NodeDefinition nodeDef = creator.create(); @@ -34,41 +29,21 @@ return DOMAIN; } - public void setClientNodeDefinitionCreator(ClientNodeDefinitionCreator creator) { - Assert.notNull(creator, "creator"); - this.creator = creator; - } - - public NodeDefinitionCreator getClientNodeDefinitionCreator() { - return creator; - } - @Override public void validate(Configuration config, Errors errors, ConfigurationContext context) { CNode rootNode = config.getRootNode(); nodeDef.validate(rootNode, errors, context); } - private Client createDefaultClient() { - return new HttpClient(); - } - @Override public void configure(Configuration config, Errors errors, ConfigurationContext context) { CNode rootNode = config.getRootNode(); nodeDef.transform(rootNode, errors, context); if (!errors.hasError()) { try { - List<Client> clients = (List<Client>) config.get("clients"); - if (clients == null) { - clients = new ArrayList<>(4); - } - - if (clients.isEmpty()) { - clients.add(createDefaultClient()); - } - - context.add(ConfigurationContextConsts.CLIENT_CLIENTS, clients); + FlowExecutor flowExecutor = new FlowExecutor(); + flowExecutor.configure(config, context); + context.add(ConfigurationContextConsts.FLOW_EXECUTOR, flowExecutor); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); }
--- a/stress-tester/src/main/java/com/passus/st/job/TestJob.java Fri May 31 10:53:30 2019 +0200 +++ b/stress-tester/src/main/java/com/passus/st/job/TestJob.java Fri May 31 11:37:28 2019 +0200 @@ -3,13 +3,8 @@ import com.passus.commons.Assert; import com.passus.config.ConfigurationContext; import com.passus.filter.ValueExtractor; -import com.passus.st.client.Client; -import com.passus.st.client.ClientListener; -import com.passus.st.client.FlowExecutor; -import com.passus.st.client.http.HttpClient; -import com.passus.st.client.http.HttpSourceNameAwareClientWorkerDispatcher; +import com.passus.st.client.*; import com.passus.st.client.http.ReporterDestination; -import com.passus.st.client.http.filter.HttpFilter; import com.passus.st.client.http.filter.HttpVarsFilter; import com.passus.st.emitter.Emitter; import com.passus.st.emitter.PassThroughSessionMapper; @@ -39,15 +34,13 @@ private FlowExecutor flowExecutor = new FlowExecutor(); - private List<Client> clients = new ArrayList<>(); - private List<Emitter> emitters = new ArrayList<>(); private List<ReporterDestination> reporterDestinations = new ArrayList<>(); private Emitter defaultEmitter; - private List<ClientListener> clientListener; + private List<ClientListener> clientListeners; private MetricsCollector metricsCollector; @@ -130,13 +123,12 @@ List<EventSource> cfgEventSources = context.get(EVENT_SOURCE_SOURCES); populateIfNotNull(cfgEventSources, testJob.eventSources); - List<Client> cfgClients = context.get(CLIENT_CLIENTS); - populateIfNotNull(cfgClients, testJob.clients); + testJob.flowExecutor = context.get(FLOW_EXECUTOR); List<Emitter> cfgEmitters = context.get(EMITTER_EMITTERS); populateIfNotNull(cfgEmitters, testJob.emitters); - testJob.defaultEmitter = (Emitter) context.get(EMITTER_DEFAULT_EMITTER); + testJob.defaultEmitter = context.get(EMITTER_DEFAULT_EMITTER); testJob.metricsCollector = context.get(METRICS_COLLECTOR, MetricsCollector.class); @@ -165,28 +157,27 @@ } }); - clients.forEach(client -> { - if (client.getEmitter() == null) { - client.setEmitter(defaultEmitter); - } - - if (clientListener != null - && !clientListener.isEmpty() - && client instanceof HttpClient) { - clientListener.forEach(((HttpClient) client)::addListener); - } + if (flowExecutor.getEmitter() == null) { + flowExecutor.setEmitter(defaultEmitter); + } - if (metricsCollector != null) { - if (client.isCollectMetrics()) { - metricsCollector.register(client); - } + if (clientListeners != null + && !clientListeners.isEmpty()) { + if (clientListeners.size() == 1) { + flowExecutor.setListener(clientListeners.get(0)); + } else { + flowExecutor.setListener(new MultipleClientListener(clientListeners)); } - }); + } - // TODO: co z wieloma klientami?? - Client firstClient = clients.get(0); + if (metricsCollector != null) { + if (flowExecutor.isCollectMetrics()) { + metricsCollector.register(flowExecutor); + } + } + eventSources.forEach(src -> { - src.setHandler(firstClient); + src.setHandler(flowExecutor); if (src instanceof MetricSource) { MetricSource es = (MetricSource) src; if (es.isCollectMetrics()) { @@ -196,35 +187,23 @@ }); // TODO: quickfix - clients.forEach((client) -> { - if (client instanceof HttpClient) { - HttpClient hc = (HttpClient) client; - if (hc.getDispatcher() instanceof HttpSourceNameAwareClientWorkerDispatcher) { - hc.setWorkersNum(eventSources.size()); - LOGGER.debug("Forcibly changing workersNum to {}", eventSources.size()); - } - } - }); + if (flowExecutor.getDispatcher() instanceof SourceNameAwareFlowWorkerDispatcher) { + flowExecutor.setWorkersNum(eventSources.size()); + LOGGER.debug("Forcibly changing workersNum to {}", eventSources.size()); + } Map<String, ValueExtractor> appVars = context.get( ConfigurationContextConsts.APP_VARS); if (appVars != null) { HttpVarsFilter httpVarsFilter = new HttpVarsFilter(appVars); - List<HttpFilter> filters = context.get( - ConfigurationContextConsts.HTTP_FILTERS); + List<FlowFilter> filters = context.get(FLOW_FILTERS); if (filters != null) { filters.add(httpVarsFilter); } // TODO: czy dodać filtry top-level do klienta??? - - clients.forEach((client) -> { - if (client instanceof HttpClient) { - HttpClient hc = (HttpClient) client; - hc.addFilter(httpVarsFilter); - } - }); + flowExecutor.addFilter(httpVarsFilter); } }
--- a/stress-tester/src/main/java/com/passus/st/job/TestJobBuilder.java Fri May 31 10:53:30 2019 +0200 +++ b/stress-tester/src/main/java/com/passus/st/job/TestJobBuilder.java Fri May 31 11:37:28 2019 +0200 @@ -4,10 +4,9 @@ import com.passus.config.ConfigurationContext; import com.passus.config.validation.Errors; import com.passus.st.Builder; -import com.passus.st.client.Client; +import com.passus.st.client.ClientListener; import com.passus.st.client.FlowExecutor; -import com.passus.st.client.http.HttpClient; -import com.passus.st.client.http.HttpClientListener; +import com.passus.st.client.MultipleClientListener; import com.passus.st.client.http.ReporterDestination; import com.passus.st.config.TestJobConfigurator; import com.passus.st.emitter.Emitter; @@ -35,7 +34,7 @@ private final List<EventSource> eventSources = new ArrayList<>(); - private final List<Client> clients = new ArrayList<>(); + private FlowExecutor flowExecutor; private final List<Emitter> emitters = new ArrayList<>(); @@ -43,7 +42,7 @@ private Emitter defaultEmitter; - private List<HttpClientListener> httpClientListener; + private List<ClientListener> clientListeners; private MetricsCollector metricsCollector = new ScheduledMetricsCollector(); @@ -166,8 +165,7 @@ List<EventSource> cfgEventSources = (List<EventSource>) context.get(EVENT_SOURCE_SOURCES); populateIfNotNull(cfgEventSources, eventSources); - List<Client> cfgClients = (List<Client>) context.get(CLIENT_CLIENTS); - populateIfNotNull(cfgClients, clients); + flowExecutor = context.get(FLOW_EXECUTOR); List<Emitter> cfgEmitters = (List<Emitter>) context.get(EMITTER_EMITTERS); populateIfNotNull(cfgEmitters, emitters); @@ -202,17 +200,18 @@ } } - clients.forEach((client) -> { - if (client.getEmitter() == null) { - client.setEmitter(defaultEmitter); - } + if (flowExecutor.getEmitter() == null) { + flowExecutor.setEmitter(defaultEmitter); + } - if (httpClientListener != null - && !httpClientListener.isEmpty() - && client instanceof HttpClient) { - httpClientListener.forEach(((HttpClient) client)::addListener); + if (clientListeners != null + && !clientListeners.isEmpty()) { + if (clientListeners.size() == 1) { + flowExecutor.setListener(clientListeners.get(0)); + } else { + flowExecutor.setListener(new MultipleClientListener(clientListeners)); } - }); + } return job; }
--- a/stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java Fri May 31 10:53:30 2019 +0200 +++ b/stress-tester/src/main/java/com/passus/st/utils/ConfigurationContextConsts.java Fri May 31 11:37:28 2019 +0200 @@ -10,8 +10,8 @@ * Lista źródeł eventów zdefiniowanych w konfiguracji. */ public static final String EVENT_SOURCE_SOURCES = "eventSource.sources"; - - public static final String CLIENT_CLIENTS = "client.clients"; + + public static final String FLOW_EXECUTOR = "client.flowExecutor"; /** * Domyślny mapper ustawiany jeżeli emitter nie ma zdefiniowanego własnego. @@ -40,7 +40,7 @@ public static final String PROJECT = "project"; - public static final String HTTP_FILTERS = "httpFilters"; + public static final String FLOW_FILTERS = "flowFilters"; public static final String LOOKUPS = "lookups";
--- a/stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java Fri May 31 10:53:30 2019 +0200 +++ b/stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java Fri May 31 11:37:28 2019 +0200 @@ -5,17 +5,14 @@ import com.passus.config.ConfigurationContextImpl; import com.passus.config.YamlConfigurationReader; import com.passus.config.validation.Errors; -import com.passus.st.client.Client; +import com.passus.st.client.FlowExecutor; import com.passus.st.client.credentials.MultiCredentialsProvider; import com.passus.st.client.credentials.UsernamePasswordCredentialsProvider; -import com.passus.st.client.http.HttpClient; import com.passus.st.client.http.SummaryHttpClientListener; import com.passus.st.client.http.filter.HttpFormLoginFilter; import com.passus.st.utils.ConfigurationContextConsts; import org.testng.annotations.Test; -import java.util.List; - import static org.testng.Assert.*; /** @@ -25,11 +22,8 @@ @Test public void testConfigure_InvalidFilter() throws Exception { - String configStr = - "clients:\n" - + " - type: http\n" - + " filters:\n" - + " - type: unknownFilter\n"; + String configStr = "filters:\n" + + " - type: unknownFilter\n"; Configuration config = YamlConfigurationReader.readFromString(configStr); ClientConfigurator configurator = new ClientConfigurator(); @@ -43,26 +37,24 @@ @SuppressWarnings("unchecked") @Test public void testConfigure() throws Exception { - String configStr = "clients:\n" - + " - type: http\n" - + " connectPartialSession: true\n" - + " collectMetrics: true\n" - + " workers: 4\n" - + " filters:\n" - + " - type: formLogin\n" - + " active: true\n" - + " loginCheckUrl: /login\n" - + " userField: username\n" - + " passwordField: password\n" - + " applyIf:\n" - + " \"@req.url\": {$contains: \"/login\"}\n" - + " provider:\n" - + " UsernamePassword:\n" - + " username: user2\n" - + " password: 'Qwe!23'\n" - + " listeners:\n" - + " - type: summary\n" - + " fileName: 'file.ext'\n"; + String configStr = "connectPartialSession: true\n" + + "collectMetrics: true\n" + + "workers: 4\n" + + "filters:\n" + + " - type: formLogin\n" + + " active: true\n" + + " loginCheckUrl: /login\n" + + " userField: username\n" + + " passwordField: password\n" + + " applyIf:\n" + + " \"@req.url\": {$contains: \"/login\"}\n" + + " provider:\n" + + " UsernamePassword:\n" + + " username: user2\n" + + " password: 'Qwe!23'\n" + + "listeners:\n" + + " - type: summary\n" + + " fileName: 'file.ext'\n"; Configuration config = YamlConfigurationReader.readFromString(configStr); @@ -74,16 +66,15 @@ assertFalse(errors.hasError()); - List<Client> clients = (List<Client>) context.get(ConfigurationContextConsts.CLIENT_CLIENTS); - assertTrue(clients.get(0) instanceof HttpClient); - HttpClient httpClient = (HttpClient) clients.get(0); - assertTrue(httpClient.isCollectMetrics()); - assertTrue(httpClient.isConnectPartialSession()); - assertEquals(4, httpClient.getWorkersNum()); - assertEquals(1, httpClient.getFilters().size()); - assertTrue(httpClient.getFilters().get(0) instanceof HttpFormLoginFilter); + FlowExecutor flowExecutor = context.get(ConfigurationContextConsts.FLOW_EXECUTOR); - HttpFormLoginFilter formLoginFilter = (HttpFormLoginFilter) httpClient.getFilters().get(0); + assertTrue(flowExecutor.isCollectMetrics()); + assertTrue(flowExecutor.isConnectPartialSession()); + assertEquals(4, flowExecutor.getWorkersNum()); + assertEquals(1, flowExecutor.getFilters().size()); + assertTrue(flowExecutor.getFilters().get(0) instanceof HttpFormLoginFilter); + + HttpFormLoginFilter formLoginFilter = (HttpFormLoginFilter) flowExecutor.getFilters().get(0); assertTrue(formLoginFilter.getCredentialsProvider() instanceof MultiCredentialsProvider); MultiCredentialsProvider multiCredentialsProvider = (MultiCredentialsProvider) formLoginFilter.getCredentialsProvider(); @@ -92,8 +83,9 @@ UsernamePasswordCredentialsProvider credentialsProvider = (UsernamePasswordCredentialsProvider) multiCredentialsProvider.getProviders().get(0); assertEquals("user2", credentialsProvider.getUsername()); - assertEquals(1, httpClient.getListeners().size()); - assertTrue(httpClient.getListeners().get(0) instanceof SummaryHttpClientListener); + assertTrue(flowExecutor.getListener() instanceof SummaryHttpClientListener); + + //MultipleClientListener listeners = (MultipleClientListener) flowExecutor.getListener(); }
--- a/stress-tester/src/test/java/com/passus/st/project/ProjectConfiguratorTest.java Fri May 31 10:53:30 2019 +0200 +++ b/stress-tester/src/test/java/com/passus/st/project/ProjectConfiguratorTest.java Fri May 31 11:37:28 2019 +0200 @@ -1,10 +1,12 @@ package com.passus.st.project; import com.passus.commons.utils.ResourceUtils; -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.client.Client; -import com.passus.st.client.http.HttpClient; +import com.passus.st.client.FlowExecutor; import com.passus.st.source.EventSource; import com.passus.st.source.NcEventSource; import com.passus.st.source.PcapSessionEventSource; @@ -14,9 +16,7 @@ import java.io.File; import java.util.List; -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.AssertJUnit.assertFalse; -import static org.testng.AssertJUnit.assertTrue; +import static org.testng.AssertJUnit.*; public class ProjectConfiguratorTest { @@ -32,16 +32,15 @@ configurator.configure(config, errors, context); assertFalse(errors.hasError()); - Project project = (Project) context.get(ConfigurationContextConsts.PROJECT); + Project project = context.get(ConfigurationContextConsts.PROJECT); assertEquals("testName", project.getName()); assertEquals(false, project.isActive()); assertEquals("testDescription", project.getDescription()); - List<Client> clients = (List<Client>) context.get(ConfigurationContextConsts.CLIENT_CLIENTS); - assertEquals(1, clients.size()); - assertTrue(clients.get(0) instanceof HttpClient); + FlowExecutor flowExecutor = context.get(ConfigurationContextConsts.FLOW_EXECUTOR); + assertNotNull(flowExecutor); - List<EventSource> sources = (List<EventSource>) context.get(ConfigurationContextConsts.EVENT_SOURCE_SOURCES); + List<EventSource> sources = context.get(ConfigurationContextConsts.EVENT_SOURCE_SOURCES); assertEquals(2, sources.size()); assertTrue(sources.get(0) instanceof PcapSessionEventSource); assertTrue(sources.get(1) instanceof NcEventSource);
--- a/stress-tester/src/test/resources/com/passus/st/config/test_job_config.yml Fri May 31 10:53:30 2019 +0200 +++ b/stress-tester/src/test/resources/com/passus/st/config/test_job_config.yml Fri May 31 11:37:28 2019 +0200 @@ -31,11 +31,9 @@ loops: 1 client: - clients: - - type: http - connectPartialSession: false - collectMetrics: true - workers: 4 + connectPartialSession: false + collectMetrics: true + workers: 4 filters: - type: requestCacheHeadersCleaner
--- a/stress-tester/src/test/resources/com/passus/st/project/test_project_config.yml Fri May 31 10:53:30 2019 +0200 +++ b/stress-tester/src/test/resources/com/passus/st/project/test_project_config.yml Fri May 31 11:37:28 2019 +0200 @@ -34,8 +34,6 @@ loops: 1 client: - clients: - - type: http - connectPartialSession: false - collectMetrics: true - workers: 4 + connectPartialSession: false + collectMetrics: true + workers: 4