changeset 609:53d0d102a8b5

thread-related bugs fixed
author Devel 1
date Mon, 09 Oct 2017 11:49:13 +0200
parents 765556dd7c80
children 299e971b600a
files stress-tester/src/main/java/com/passus/st/client/http/DumperHttpClientListener.java stress-tester/src/main/java/com/passus/st/client/http/HttpMessageWriter.java stress-tester/src/main/java/com/passus/st/client/http/SummaryHttpClientListener.java stress-tester/src/test/java/com/passus/st/client/http/DumperHttpClientListenerTest.java
diffstat 4 files changed, 35 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/client/http/DumperHttpClientListener.java	Mon Oct 09 09:58:55 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/DumperHttpClientListener.java	Mon Oct 09 11:49:13 2017 +0200
@@ -7,6 +7,7 @@
 import java.io.File;
 import java.text.SimpleDateFormat;
 import java.util.Date;
+import java.util.concurrent.atomic.AtomicInteger;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
@@ -36,8 +37,7 @@
     private int indexWidth = 6;
     private boolean requestsOnly;
 
-    private int index = 0;
-    private String indexString = FormatUtils.padStart(String.valueOf(index), indexWidth, '0');
+    private final AtomicInteger index = new AtomicInteger();
 
     public DumperHttpClientListener(File parent) {
         this(parent, "", ".txt");
@@ -67,19 +67,18 @@
 
     @Override
     public void responseReceived(HttpRequest request, HttpResponse response, HttpFlowContext context) {
-        ++index;
-        indexString = FormatUtils.padStart(String.valueOf(index), indexWidth, '0');
+        String indexString = FormatUtils.padStart(String.valueOf(index.incrementAndGet()), indexWidth, '0');
         String id = request.getId();
 
         HttpResponse origReponse = context.origReponse();
-        writer.writeSilently(request, getFile(MSG_REQ_PROC, request, id));
+        writer.writeSilently(request, getFile(MSG_REQ_PROC, request, id, indexString));
         if (!requestsOnly) {
-            writer.writeSilently(response, getFile(MSG_RESP_REAL, response, id));
-            writer.writeSilently(origReponse, getFile(MSG_RESP_ORIG, origReponse, id));
+            writer.writeSilently(response, getFile(MSG_RESP_REAL, response, id, indexString));
+            writer.writeSilently(origReponse, getFile(MSG_RESP_ORIG, origReponse, id, indexString));
         }
     }
 
-    File getFile(int msgType, HttpMessage msg, String requestId) {
+    File getFile(int msgType, HttpMessage msg, String requestId, String indexString) {
         StringBuilder sb = new StringBuilder();
         sb.append(prefix);
         sb.append(indexString).append('.');
--- a/stress-tester/src/main/java/com/passus/st/client/http/HttpMessageWriter.java	Mon Oct 09 09:58:55 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/HttpMessageWriter.java	Mon Oct 09 11:49:13 2017 +0200
@@ -48,13 +48,15 @@
     }
 
     public void writeSilently(HttpMessage message, File file) {
+        String path = file.getAbsolutePath();
         try {
             if (file.exists()) {
-                LOGGER.warn("File '{}' already exists.", file.getAbsolutePath());
+                LOGGER.warn("File '{}' already exists.", path);
             }
             write(message, file);
+            LOGGER.trace("File '{}' written", path);
         } catch (IOException ex) {
-            LOGGER.debug("Could not save file '{}'.", file.getAbsolutePath());
+            LOGGER.debug("Could not save file '{}'.", path);
         }
 
     }
--- a/stress-tester/src/main/java/com/passus/st/client/http/SummaryHttpClientListener.java	Mon Oct 09 09:58:55 2017 +0200
+++ b/stress-tester/src/main/java/com/passus/st/client/http/SummaryHttpClientListener.java	Mon Oct 09 11:49:13 2017 +0200
@@ -4,6 +4,7 @@
 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;
@@ -16,26 +17,33 @@
  */
 public class SummaryHttpClientListener implements HttpClientListener, Closeable {
 
-    private final FileOutputStream fos;
+    private final BufferedOutputStream os;
 
     public SummaryHttpClientListener(File file) throws FileNotFoundException {
-        this.fos = new FileOutputStream(file);
+        FileOutputStream fos = new FileOutputStream(file);
+        os = new BufferedOutputStream(fos);
     }
 
     @Override
     public void responseReceived(HttpRequest request, HttpResponse response, HttpFlowContext context) {
         try {
-            write(request.getMethod().getName());
-            write(request.getUri());
-            write(request.getVersion());
-            write("=".getBytes());
+            HttpHeaders responseHeaders = response.getHeaders();
+            ByteString contentType = responseHeaders.get(HttpHeaders.CONTENT_TYPE);
+            ByteString contentLength = responseHeaders.get(HttpHeaders.CONTENT_LENGTH);
 
-            write(response.getStatus().toByteString());
-            HttpHeaders responseHeaders = response.getHeaders();
-            write(responseHeaders.get(HttpHeaders.CONTENT_TYPE));
-            write(responseHeaders.get(HttpHeaders.CONTENT_LENGTH));
-            fos.write('\r');
-            fos.write('\n');
+            synchronized (os) {
+                write(request.getMethod().getName());
+                write(request.getUri());
+                write(request.getVersion());
+                os.write('=');
+                os.write(' ');
+
+                write(response.getStatus().toByteString());
+                write(contentType);
+                write(contentLength);
+                os.write('\r');
+                os.write('\n');
+            }
         } catch (IOException ex) {
         }
     }
@@ -45,13 +53,13 @@
     }
 
     private void write(byte[] b1) throws IOException {
-        fos.write(b1);
-        fos.write(' ');
+        os.write(b1);
+        os.write(' ');
     }
 
     @Override
     public void close() throws IOException {
-        fos.close();
+        os.close();
     }
 
 }
--- a/stress-tester/src/test/java/com/passus/st/client/http/DumperHttpClientListenerTest.java	Mon Oct 09 09:58:55 2017 +0200
+++ b/stress-tester/src/test/java/com/passus/st/client/http/DumperHttpClientListenerTest.java	Mon Oct 09 11:49:13 2017 +0200
@@ -19,7 +19,7 @@
         request.setTimestamp(1500_000000000L);
         String id = request.getId();
 
-        File file = listener.getFile(1, request, id);
+        File file = listener.getFile(1, request, id, "000000");
 
         assertEquals(file.getName(), "aa000000.req." + id + ".txt");
     }