Mercurial > stress-tester
changeset 1187:013fb325d040
flow analyzer - in progress
author | Devel 1 |
---|---|
date | Wed, 17 Jun 2020 15:21:15 +0200 |
parents | 8b605b57e68d |
children | 223dbbdd7e40 |
files | stress-tester/src/main/java/com/passus/st/scanner/HttpUrlSequencePayloadAnalyzer.java |
diffstat | 1 files changed, 96 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/scanner/HttpUrlSequencePayloadAnalyzer.java Wed Jun 17 13:33:53 2020 +0200 +++ b/stress-tester/src/main/java/com/passus/st/scanner/HttpUrlSequencePayloadAnalyzer.java Wed Jun 17 15:21:15 2020 +0200 @@ -1,5 +1,6 @@ package com.passus.st.scanner; +import com.opencsv.CSVWriter; import com.passus.commons.annotations.Plugin; import com.passus.config.Configurable; import com.passus.config.Configuration; @@ -7,7 +8,10 @@ import com.passus.config.annotations.NodeDefinitionCreate; import com.passus.config.schema.NodeDefinition; import com.passus.config.schema.NodeDefinitionCreator; +import com.passus.data.ByteString; +import com.passus.net.http.HttpCookie; import com.passus.net.http.HttpHeaders; +import com.passus.net.http.HttpMessageHelper; import com.passus.net.http.HttpRequest; import com.passus.net.http.HttpResponse; import com.passus.st.client.DataEvents; @@ -16,6 +20,10 @@ import com.passus.st.metric.MetricSource; import com.passus.st.metric.MetricsContainer; import com.passus.st.plugin.PluginConstants; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; import java.util.Date; @@ -30,43 +38,59 @@ public static final String TYPE = "httpUrlSequence"; + private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss"); + private HttpUrlSequences metric; - private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH_mm_ss"); + private UserIdExtractor userIdExtractor; - private UserIdExtractor userIdExtractor; - + private String dataFile; + + private CSVWriter dataWriter; + public String getType() { return TYPE; } @Override public void configure(Configuration config, ConfigurationContext context) { - String userIdSource = config.getString("userIdSource"); + String userIdSource = config.getString("userIdSource", "ip"); userIdExtractor = resolveUserIdExtractor(userIdSource); - System.out.println(""); + dataFile = config.getString("dataFile", "sequence_data.csv"); } @Override public void activate() { metric = new HttpUrlSequences(); metric.activate(); + + try { + FileOutputStream fos = new FileOutputStream(dataFile); + OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8); + dataWriter = new CSVWriter(osw); + } catch (IOException ex) { + throw new RuntimeException(ex); // TODO + } } @Override public void deactivate() { metric.deactivate(); metric = null; + + try { + dataWriter.close(); + } catch (IOException ignore) { + } } @Override public boolean isCollectMetrics() { - return false; + return true; } @Override public void setCollectMetrics(boolean collectMetrics) { - } @Override @@ -99,12 +123,14 @@ int qidx = uri.indexOf('?'); String path; - String query; + String query = ""; if (qidx < 0) { path = uri; - query = null; } else { path = uri.substring(0, qidx); + if (qidx < uri.length()) { + query = uri.substring(qidx + 1).trim(); + } } String method = req.getMethod().toString(); @@ -113,25 +139,80 @@ String userAgent = req.getHeaders().get(HttpHeaders.USER_AGENT).toString(); // unused String status = resp == null ? "" : resp.getStatus().toString(); // unused String protocol = Integer.toString(req.getVersion()); // unused, eg. "HTTP/1.1" -//pathBase pathArgs request_method datetime id agent status protocol + + String[] record = {path, query, method, datetime, userId, userAgent, status, protocol}; + dataWriter.writeNext(record); } } + static UserIdExtractor resolveUserIdExtractor(String spec) { + int idx = spec.indexOf(':'); + String type; + String arg; + if (idx < 0) { + type = spec; + arg = null; + } else { + type = spec.substring(0, idx); + arg = spec.substring(idx + 1); + } + + switch (type) { + case "ip": + return (event) -> event.getSessionInfo().getSrcIp().toString(); + case "cookie": + return new CookieExtractor(arg); + case "header": + return new HeaderExtr(arg); + default: + return (event) -> "null"; + } + } public interface UserIdExtractor { + String extract(SessionPayloadEvent event); } - - static UserIdExtractor resolveUserIdExtractor(String userIdSource) { - return (event) -> "id"; + + static class CookieExtractor implements UserIdExtractor { + + private final String name; + + public CookieExtractor(String name) { + this.name = name; + } + + @Override + public String extract(SessionPayloadEvent event) { + HttpRequest req = (HttpRequest) event.getRequest(); + HttpCookie cookie = HttpMessageHelper.get().getCookie(req, name); + return cookie == null ? "null" : cookie.getValue().toString(); + } } - + + static class HeaderExtr implements UserIdExtractor { + + private final String name; + + public HeaderExtr(String name) { + this.name = name; + } + + @Override + public String extract(SessionPayloadEvent event) { + HttpRequest req = (HttpRequest) event.getRequest(); + ByteString bs = req.getHeaders().get(name); + return bs == null ? "null" : bs.toString(); + } + } + public static class NodeDefCreator implements NodeDefinitionCreator { @Override public NodeDefinition create() { return mapDef( - tupleDef("userIdSource", STRING_DEF).setRequired(false) + tupleDef("userIdSource", STRING_DEF).setRequired(false), + tupleDef("dataFile", STRING_DEF).setRequired(false) ); } }