Mercurial > stress-tester
changeset 772:b2e54c963007
ReaderMain destination file parameter
author | Devel 2 |
---|---|
date | Mon, 18 Dec 2017 11:59:04 +0100 |
parents | 8bac181dfdfb |
children | 5fd3d7d7a21e |
files | stress-tester/src/main/java/com/passus/st/ReaderMain.java |
diffstat | 1 files changed, 42 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/ReaderMain.java Mon Dec 18 11:58:39 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/ReaderMain.java Mon Dec 18 11:59:04 2017 +0100 @@ -10,7 +10,10 @@ import com.passus.st.source.EventSource; import com.passus.st.source.NcEventSource; import com.passus.st.source.PcapSessionEventSource; +import static com.passus.st.utils.CliUtils.option; +import java.io.File; import java.io.IOException; +import java.io.PrintStream; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.HelpFormatter; @@ -39,6 +42,11 @@ options.addAllowPartialSessionOption(); options.addHttpPortsOption(); + options.addOption(option("f", "file").desc("Write output to file.") + .hasArg().argName("file") + .build() + ); + return options; } @@ -47,7 +55,7 @@ System.out.print(session); } - private void printEvent(Event event) { + private void printEvent(Event event, PrintStream ps) { if (event.getType() == SessionStatusEvent.TYPE) { SessionStatusEvent statusEvent = (SessionStatusEvent) event; printSessionInfo(statusEvent.getSessionInfo()); @@ -61,7 +69,7 @@ HttpRequest req = payloadEvent.getRequest(); if (req != null) { try { - req.writeTo(System.out); + req.writeTo(ps); System.out.println(""); } catch (IOException ex) { logger.debug(ex.getMessage(), ex); @@ -73,7 +81,7 @@ HttpResponse resp = payloadEvent.getResponse(); if (resp != null) { try { - resp.writeTo(System.out); + resp.writeTo(ps); System.out.println(""); } catch (IOException ex) { logger.debug(ex.getMessage(), ex); @@ -84,9 +92,9 @@ } } - private void readNcFile(NcEventSource src) throws Exception { + private void readNcFile(NcEventSource src, PrintStream ps) throws Exception { src.setParallel(false); - src.setHandler((event) -> printEvent(event)); + src.setHandler((event) -> printEvent(event, ps)); try { src.start(); @@ -95,8 +103,8 @@ } } - private void readPcapFile(PcapSessionEventSource eventSrc) throws Exception { - eventSrc.setHandler((event) -> printEvent(event)); + private void readPcapFile(PcapSessionEventSource eventSrc, PrintStream ps) throws Exception { + eventSrc.setHandler((event) -> printEvent(event, ps)); try { eventSrc.start(); eventSrc.join(); @@ -122,13 +130,34 @@ logger = LogManager.getLogger(ReaderMain.class); EventSource eventSource = cliHelper.createEventSource(clArgs[0], cl); - if (eventSource instanceof NcEventSource) { - readNcFile((NcEventSource) eventSource); - } else if (eventSource instanceof PcapSessionEventSource) { - readPcapFile((PcapSessionEventSource) eventSource); - } else { - cliHelper.printError("Not supported event source type '" + eventSource.getType() + "'."); + PrintStream ps = System.out; + File outFile = null; + if (cl.hasOption("f")) { + outFile = new File(cl.getOptionValue("f")); + if (outFile.exists()) { + outFile.delete(); + } + + ps = new PrintStream(outFile); } + + try { + if (eventSource instanceof NcEventSource) { + readNcFile((NcEventSource) eventSource, ps); + } else if (eventSource instanceof PcapSessionEventSource) { + readPcapFile((PcapSessionEventSource) eventSource, ps); + } else { + cliHelper.printError("Not supported event source type '" + eventSource.getType() + "'."); + } + } finally { + if (outFile != null) { + try { + ps.close(); + } catch (Exception ignore) { + } + } + } + } catch (Exception e) { e.printStackTrace(System.err); } finally {