changeset 1119:87d16fe4bf92

7901289: Optimize buffers in binary link
author shade
date Thu, 05 Feb 2015 21:39:48 +0300
parents 07e7d6fb6ee1
children 18647f128073
files jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkClient.java jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkServer.java
diffstat 2 files changed, 20 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkClient.java	Thu Feb 05 15:51:58 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkClient.java	Thu Feb 05 21:39:48 2015 +0300
@@ -33,6 +33,8 @@
 import org.openjdk.jmh.util.FileUtils;
 import org.openjdk.jmh.util.Multimap;
 
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
@@ -47,6 +49,8 @@
 
 public final class BinaryLinkClient {
 
+    private static final int BUFFER_SIZE = Integer.getInteger("jmh.link.bufferSize", 64*1024);
+
     private final Object lock;
 
     private final Socket clientSocket;
@@ -60,8 +64,13 @@
     public BinaryLinkClient(String hostName, int hostPort) throws IOException {
         this.lock = new Object();
         this.clientSocket = new Socket(hostName, hostPort);
-        this.oos = new ObjectOutputStream(clientSocket.getOutputStream());
-        this.ois = new ObjectInputStream(clientSocket.getInputStream());
+
+        // Initialize the OOS first, and flush, letting the other party read the stream header.
+        this.oos = new ObjectOutputStream(new BufferedOutputStream(clientSocket.getOutputStream(), BUFFER_SIZE));
+        this.oos.flush();
+
+        this.ois = new ObjectInputStream(new BufferedInputStream(clientSocket.getInputStream(), BUFFER_SIZE));
+
         this.streamErr = new ForwardingPrintStream(OutputFrame.Type.ERR);
         this.streamOut = new ForwardingPrintStream(OutputFrame.Type.OUT);
         this.outputFormat = (OutputFormat) Proxy.newProxyInstance(
--- a/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkServer.java	Thu Feb 05 15:51:58 2015 +0300
+++ b/jmh-core/src/main/java/org/openjdk/jmh/runner/link/BinaryLinkServer.java	Thu Feb 05 21:39:48 2015 +0300
@@ -36,6 +36,8 @@
 import org.openjdk.jmh.util.Multimap;
 import org.openjdk.jmh.util.Utils;
 
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.ObjectInputStream;
@@ -61,6 +63,8 @@
  */
 public final class BinaryLinkServer {
 
+    private static final int BUFFER_SIZE = Integer.getInteger("jmh.link.bufferSize", 64*1024);
+
     private final Options opts;
     private final OutputFormat out;
     private final Map<String, Method> methods;
@@ -259,14 +263,17 @@
             this.socket = socket;
             this.is = socket.getInputStream();
             this.os = socket.getOutputStream();
+
+            // eager OOS initialization, let the other party read the stream header
+            oos = new ObjectOutputStream(new BufferedOutputStream(os, BUFFER_SIZE));
+            oos.flush();
         }
 
         @Override
         public void run() {
             try {
                 // late OIS initialization, otherwise we'll block reading the header
-                ois = new ObjectInputStream(is);
-                oos = new ObjectOutputStream(os);
+                ois = new ObjectInputStream(new BufferedInputStream(is, BUFFER_SIZE));
 
                 Object obj;
                 while ((obj = ois.readObject()) != null) {