Mercurial > stress-tester
changeset 873:c2bbb7c5b635
HttClientListener plugin
author | Devel 2 |
---|---|
date | Fri, 26 Jan 2018 13:20:15 +0100 |
parents | f67a557c1afb |
children | a4f8de13c0c9 |
files | stress-tester/src/main/java/com/passus/st/client/http/HttpClient.java stress-tester/src/main/java/com/passus/st/client/http/HttpClientListenerFactory.java stress-tester/src/main/java/com/passus/st/client/http/SummaryHttpClientListener.java stress-tester/src/main/java/com/passus/st/plugin/PluginConstants.java stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java |
diffstat | 5 files changed, 127 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/HttpClient.java Fri Jan 26 11:36:38 2018 +0100 +++ b/stress-tester/src/main/java/com/passus/st/client/http/HttpClient.java Fri Jan 26 13:20:15 2018 +0100 @@ -6,32 +6,30 @@ import com.passus.config.Configuration; import com.passus.config.ConfigurationContext; import com.passus.config.annotations.NodeDefinitionCreate; +import com.passus.config.schema.DynaKeyValueVaryListNodeDefinition; import com.passus.config.schema.NodeDefinition; import com.passus.config.schema.NodeDefinitionCreator; +import com.passus.config.validation.LongValidator; import com.passus.st.client.Client; import com.passus.st.client.Event; import com.passus.st.client.http.filter.HttpFilter; import com.passus.st.client.http.filter.HttpFilterAware; import com.passus.st.client.http.filter.HttpFilterChain; +import com.passus.st.client.http.filter.HttpFiltersNodeDefinitionCreator; import com.passus.st.emitter.Emitter; import com.passus.st.metric.MetricsContainer; import com.passus.st.plugin.PluginConstants; -import java.util.ArrayList; -import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef; -import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef; -import static com.passus.config.schema.ConfigurationSchemaBuilder.valueDefInteger; -import static com.passus.config.schema.ConfigurationSchemaBuilder.valueDefBool; -import com.passus.config.validation.LongValidator; -import com.passus.st.client.http.filter.HttpFiltersNodeDefinitionCreator; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.List; + +import static com.passus.config.schema.ConfigurationSchemaBuilder.*; /** - * * @author Mirosław Hawrot */ @SuppressWarnings("ALL") @@ -81,20 +79,41 @@ this.emitter = emitter; } + @Override public Emitter getEmitter() { return emitter; } + @Override public void setEmitter(Emitter emitter) { this.emitter = emitter; } + public void setListeners(Collection<HttpClientListener> listeners) { + Assert.notContainsNull(listeners, "listeners"); + synchronized (listeners) { + this.listeners.addAll(listeners); + } + } + public void addListener(HttpClientListener listener) { synchronized (listeners) { listeners.add(listener); } } + public void removeListener(HttpClientListener listener) { + synchronized (listeners) { + listeners.remove(listener); + } + } + + public List<HttpClientListener> getListeners() { + synchronized (listeners) { + return Collections.unmodifiableList(listeners); + } + } + @Override public List<HttpFilter> getFilters() { return filterChain.getFilters(); @@ -201,6 +220,7 @@ @Override public void configure(Configuration config, ConfigurationContext context) { setFilters((List<HttpFilter>) config.get("filters", Collections.EMPTY_LIST)); + setListeners((List<HttpClientListener>) config.get("listeners", Collections.EMPTY_LIST)); setWorkersNum(config.getInteger("workers", DEFAULT_WORKERS_NUM)); connectPartialSession = config.getBoolean("connectPartialSession", DEFAULT_CONNECT_PARTIAL_SESSION); collectMetrics = config.getBoolean("collectMetrics", DEFAULT_COLLECT_METRICS); @@ -332,6 +352,10 @@ ).setRequired(false), tupleDef("filters", HttpFiltersNodeDefinitionCreator.createFiltersList(true) + ).setRequired(false), + tupleDef("listeners", + new DynaKeyValueVaryListNodeDefinition("type", HttpClientListenerFactory.getInstance()) + .setTransformToPluginObject(true) ).setRequired(false) ); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/client/http/HttpClientListenerFactory.java Fri Jan 26 13:20:15 2018 +0100 @@ -0,0 +1,22 @@ +package com.passus.st.client.http; + +import com.passus.commons.plugin.PluginFactory; +import com.passus.st.plugin.PluginConstants; + +public class HttpClientListenerFactory extends PluginFactory<HttpClientListener> { + + private static HttpClientListenerFactory instance; + + public HttpClientListenerFactory() { + super(PluginConstants.CATEGORY_HTTP_CLIENT_LISTENER, HttpClientListener.class); + } + + public static synchronized HttpClientListenerFactory getInstance() { + if (instance == null) { + instance = new HttpClientListenerFactory(); + } + + return instance; + } + +}
--- a/stress-tester/src/main/java/com/passus/st/client/http/SummaryHttpClientListener.java Fri Jan 26 11:36:38 2018 +0100 +++ b/stress-tester/src/main/java/com/passus/st/client/http/SummaryHttpClientListener.java Fri Jan 26 13:20:15 2018 +0100 @@ -1,31 +1,64 @@ package com.passus.st.client.http; +import com.passus.commons.annotations.Plugin; +import com.passus.config.Configurable; +import com.passus.config.Configuration; +import com.passus.config.ConfigurationContext; +import com.passus.config.ConfigurationException; +import com.passus.config.schema.NodeDefinition; +import com.passus.config.schema.NodeDefinitionCreator; import com.passus.data.ByteString; import com.passus.net.http.HttpHeaders; import com.passus.net.http.HttpRequest; import com.passus.net.http.HttpResponse; -import java.io.BufferedOutputStream; -import java.io.Closeable; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.*; + +import static com.passus.config.schema.ConfigurationSchemaBuilder.*; +import static com.passus.st.plugin.PluginConstants.CATEGORY_HTTP_CLIENT_LISTENER; /** - * * @author mikolaj.podbielski */ -public class SummaryHttpClientListener implements HttpClientListener, Closeable { +@Plugin(name = SummaryHttpClientListener.TYPE, category = CATEGORY_HTTP_CLIENT_LISTENER) +public final class SummaryHttpClientListener implements HttpClientListener, Closeable, Configurable { - private final BufferedOutputStream os; + private static final Logger LOGGER = LogManager.getLogger(SummaryHttpClientListener.class); + + public static final String TYPE = "summary"; + + private BufferedOutputStream os; + + public SummaryHttpClientListener() { + } public SummaryHttpClientListener(File file) throws FileNotFoundException { + createStream(file); + } + + private void createStream(File file) throws FileNotFoundException { FileOutputStream fos = new FileOutputStream(file); os = new BufferedOutputStream(fos); } @Override + public void configure(Configuration config, ConfigurationContext context) { + try { + String fileName = config.getString("fileName"); + createStream(new File(fileName)); + } catch (Exception ex) { + throw new ConfigurationException(ex.getMessage(), ex); + } + } + + @Override public void responseReceived(HttpRequest request, HttpResponse response, HttpFlowContext context) { + if (os == null) { + return; + } + try { HttpHeaders responseHeaders = response.getHeaders(); ByteString contentType = responseHeaders.get(HttpHeaders.CONTENT_TYPE); @@ -45,6 +78,9 @@ os.write('\n'); } } catch (IOException ex) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug(ex.getMessage(), ex); + } } } @@ -62,4 +98,12 @@ os.close(); } + public static class SummaryHttpClientListenerNodeDefCreator implements NodeDefinitionCreator { + @Override + public NodeDefinition create() { + return mapDef( + tupleDef("fileName", valueDef()) + ); + } + } }
--- a/stress-tester/src/main/java/com/passus/st/plugin/PluginConstants.java Fri Jan 26 11:36:38 2018 +0100 +++ b/stress-tester/src/main/java/com/passus/st/plugin/PluginConstants.java Fri Jan 26 13:20:15 2018 +0100 @@ -1,7 +1,6 @@ package com.passus.st.plugin; /** - * * @author Mirosław Hawrot */ public class PluginConstants { @@ -10,6 +9,8 @@ public static final String CATEGORY_HTTP_FILTER = "HttpFilter"; + public static final String CATEGORY_HTTP_CLIENT_LISTENER = "HttpClientListener"; + public static final String CATEGORY_CREDENTIALS_PROVIDER = "CredentialsProvider"; public static final String CATEGORY_EVENT_SOURCE = "EventSource";
--- a/stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java Fri Jan 26 11:36:38 2018 +0100 +++ b/stress-tester/src/test/java/com/passus/st/config/ClientConfiguratorTest.java Fri Jan 26 13:20:15 2018 +0100 @@ -9,15 +9,16 @@ 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 java.util.List; -import static org.testng.Assert.*; - import com.passus.st.utils.ConfigurationContextConsts; import org.testng.annotations.Test; +import java.util.List; + +import static org.testng.Assert.*; + /** - * * @author Mirosław Hawrot */ public class ClientConfiguratorTest { @@ -26,9 +27,9 @@ public void testConfigure_InvalidFilter() throws Exception { String configStr = "clients:\n" - + " - type: http\n" - + " filters:\n" - + " - type: unknownFilter\n"; + + " - type: http\n" + + " filters:\n" + + " - type: unknownFilter\n"; Configuration config = YamlConfigurationReader.readFromString(configStr); ClientConfigurator configurator = new ClientConfigurator(); @@ -58,7 +59,11 @@ + " provider:\n" + " UsernamePassword:\n" + " username: user2\n" - + " password: 'Qwe!23'"; + + " password: 'Qwe!23'\n" + + " listeners:\n" + + " - type: summary\n" + + " fileName: '/tmp/file.ext'\n"; + Configuration config = YamlConfigurationReader.readFromString(configStr); ClientConfigurator configurator = new ClientConfigurator(); @@ -86,6 +91,10 @@ UsernamePasswordCredentialsProvider credentialsProvider = (UsernamePasswordCredentialsProvider) multiCredentialsProvider.getProviders().get(0); assertEquals("user2", credentialsProvider.getUsername()); + + assertEquals(1, httpClient.getListeners().size()); + assertTrue(httpClient.getListeners().get(0) instanceof SummaryHttpClientListener); + } }