changeset 769:6300f55ac52b

merge
author Devel 1
date Mon, 18 Dec 2017 10:08:00 +0100
parents c01815e24aff (current diff) 68147f3215dd (diff)
children fdff2d086ce5
files stress-tester/src/main/java/com/passus/st/ReaderMain.java
diffstat 3 files changed, 79 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/ReaderMain.java	Fri Dec 15 15:09:36 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/ReaderMain.java	Mon Dec 18 10:08:00 2017 +0100
@@ -1,5 +1,8 @@
 package com.passus.st;
 
+import com.passus.data.DataSourceUtils;
+import com.passus.net.http.HttpRequest;
+import com.passus.net.http.HttpResponse;
 import com.passus.st.client.Event;
 import com.passus.st.client.SessionStatusEvent;
 import com.passus.st.client.http.HttpSessionPayloadEvent;
@@ -7,10 +10,13 @@
 import com.passus.st.source.EventSource;
 import com.passus.st.source.NcEventSource;
 import com.passus.st.source.PcapSessionEventSource;
+import java.io.IOException;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.DefaultParser;
 import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Options;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
 
 /**
  *
@@ -18,6 +24,7 @@
  */
 public class ReaderMain {
 
+    private Logger logger;
     private final CliHelper cliHelper = new CliHelper();
 
     private void printHelp(Options options) {
@@ -51,20 +58,28 @@
             printSessionInfo(payloadEvent.getSessionInfo());
             System.out.println(" payload: ");
 
-            try {
-                if (payloadEvent.getRequest() != null) {
-//                    System.out.println(payloadEvent.getRequest());
-                    payloadEvent.getRequest().writeTo(System.out);
+            HttpRequest req = payloadEvent.getRequest();
+            if (req != null) {
+                try {
+                    req.writeTo(System.out);
                     System.out.println("");
+                } catch (IOException ex) {
+                    logger.debug(ex.getMessage(), ex);
+                } finally {
+                    DataSourceUtils.release(req.getContent());
                 }
+            }
 
-                if (payloadEvent.getResponse() != null) {
-//                    System.out.println(payloadEvent.getResponse());
-                    payloadEvent.getResponse().writeTo(System.out);
+            HttpResponse resp = payloadEvent.getResponse();
+            if (resp != null) {
+                try {
+                    resp.writeTo(System.out);
                     System.out.println("");
+                } catch (IOException ex) {
+                    logger.debug(ex.getMessage(), ex);
+                } finally {
+                    DataSourceUtils.release(resp.getContent());
                 }
-            } catch (Exception ex) {
-                ex.printStackTrace(System.err);
             }
         }
     }
@@ -104,6 +119,8 @@
             }
 
             cliHelper.configureLogger(cl);
+            logger = LogManager.getLogger(ReaderMain.class);
+
             EventSource eventSource = cliHelper.createEventSource(clArgs[0], cl);
             if (eventSource instanceof NcEventSource) {
                 readNcFile((NcEventSource) eventSource);
--- a/stress-tester/src/main/java/com/passus/st/source/NcEventSource.java	Fri Dec 15 15:09:36 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/source/NcEventSource.java	Mon Dec 18 10:08:00 2017 +0100
@@ -3,8 +3,10 @@
 import com.passus.commons.Assert;
 import com.passus.commons.annotations.Plugin;
 import com.passus.commons.service.ServiceException;
+import com.passus.data.Allocator;
 import com.passus.data.ByteBuff;
 import com.passus.data.ByteBuffDataSource;
+import com.passus.data.DefaultAllocator;
 import com.passus.st.client.EventHandler;
 import com.passus.st.client.SessionStatusEvent;
 import com.passus.st.client.http.HttpReqResp;
@@ -17,7 +19,6 @@
 import com.passus.st.reader.nc.NcSessionStatusBlock;
 import java.io.File;
 import java.io.IOException;
-import java.util.Objects;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
@@ -48,6 +49,8 @@
 
     private boolean parallel = true;
 
+    private Allocator allocator = new DefaultAllocator();
+
     public NcEventSource() {
     }
 
@@ -74,6 +77,15 @@
         this.name = name;
     }
 
+    public Allocator getAllocator() {
+        return allocator;
+    }
+
+    public void setAllocator(Allocator allocator) {
+        Assert.notNull(allocator, "allocator");
+        this.allocator = allocator;
+    }
+
     @Override
     public int getLoops() {
         throw new UnsupportedOperationException("Not supported yet.");
@@ -116,6 +128,7 @@
 
         try {
             reader = new NcDataBlockReader(ncFile);
+            reader.setAllocator(allocator);
             reader.open();
 
             if (parallel) {
--- a/stress-tester/src/test/java/com/passus/st/source/NcEventSourceTest.java	Fri Dec 15 15:09:36 2017 +0100
+++ b/stress-tester/src/test/java/com/passus/st/source/NcEventSourceTest.java	Mon Dec 18 10:08:00 2017 +0100
@@ -1,6 +1,8 @@
 package com.passus.st.source;
 
 import static com.passus.commons.utils.ResourceUtils.createTmpFile;
+import com.passus.data.Allocator;
+import com.passus.data.PooledAllocator;
 import com.passus.st.client.ArrayListEventHandler;
 import com.passus.st.client.DataEvents;
 import com.passus.st.client.Event;
@@ -55,6 +57,25 @@
         };
     }
 
+    private void assertEvents(FileEvents expectedEvents, List<Event> events) {
+        assertEquals(expectedEvents.events.size(), events.size());
+        for (int i = 0; i < events.size(); i++) {
+            Event expectedEvent = expectedEvents.events.get(i);
+            Event event = events.get(i);
+
+            assertEquals(expectedEvent.getType(), event.getType());
+            if (event.getType() == HttpSessionPayloadEvent.TYPE) {
+                HttpSessionPayloadEvent expectedPayloadEvent = (HttpSessionPayloadEvent) expectedEvent;
+                HttpSessionPayloadEvent payloadEvent = (HttpSessionPayloadEvent) event;
+
+                assertMessages(expectedPayloadEvent.getRequest(), payloadEvent.getRequest());
+                assertMessagesContent(expectedPayloadEvent.getRequest(), payloadEvent.getRequest());
+                assertMessages(expectedPayloadEvent.getResponse(), payloadEvent.getResponse());
+                assertMessagesContent(expectedPayloadEvent.getResponse(), payloadEvent.getResponse());
+            }
+        }
+    }
+
     @Test(dataProvider = "pcapFiles")
     public void testRead(String pcapFile) throws Exception {
         FileEvents fileEvents = writeEvents(pcapFile);
@@ -66,24 +87,27 @@
             eventSource.start();
 
             List<Event> events = handler.getEvents();
-            assertEquals(fileEvents.events.size(), events.size());
-            for (int i = 0; i < events.size(); i++) {
-                Event expectedEvent = fileEvents.events.get(i);
-                Event event = events.get(i);
+            assertEvents(fileEvents, events);
+        } finally {
+            fileEvents.ncFile.delete();
+        }
+    }
 
-                assertEquals(expectedEvent.getType(), event.getType());
-                if (event.getType() == HttpSessionPayloadEvent.TYPE) {
-                    HttpSessionPayloadEvent expectedPayloadEvent = (HttpSessionPayloadEvent) expectedEvent;
-                    HttpSessionPayloadEvent payloadEvent = (HttpSessionPayloadEvent) event;
+    public void testRead_PooledAllocator() throws Exception {
+        FileEvents fileEvents = writeEvents("pcap/http/http_req_resp.pcap");
+        try {
+            PooledAllocator allocator = new PooledAllocator();
+            ArrayListEventHandler handler = new ArrayListEventHandler();
+            NcEventSource eventSource = new NcEventSource(fileEvents.ncFile);
+            eventSource.setAllocator(allocator);
+            eventSource.setHandler(handler);
+            eventSource.setParallel(false);
+            eventSource.start();
 
-                    assertMessages(expectedPayloadEvent.getRequest(), payloadEvent.getRequest());
-                    assertMessagesContent(expectedPayloadEvent.getRequest(), payloadEvent.getRequest());
-                    assertMessages(expectedPayloadEvent.getResponse(), payloadEvent.getResponse());
-                    assertMessagesContent(expectedPayloadEvent.getResponse(), payloadEvent.getResponse());
-                }
-            }
+            List<Event> events = handler.getEvents();
+            assertEvents(fileEvents, events);
+            assertEquals(2, allocator.usedSize());
         } finally {
-            FileUtils.copyFile(fileEvents.ncFile, new File("c:\\tmp\\a"));
             fileEvents.ncFile.delete();
         }
     }