Thrift TTransportFactory model for servers

Summary: Servers need to create bufferedtransports etc. around the transports they get in a user-definable way. So use a factory pattern to allow the user to supply an object to the server that defines this behavior.


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664792 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/test/cpp/src/TestClient.cc b/test/cpp/src/TestClient.cc
index e12b65b..6334f08 100644
--- a/test/cpp/src/TestClient.cc
+++ b/test/cpp/src/TestClient.cc
@@ -45,23 +45,28 @@
   }
 
   shared_ptr<TSocket> socket(new TSocket(host, port));
-  shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket, 2048));
+  shared_ptr<TBufferedTransport> bufferedSocket(new TBufferedTransport(socket));
   shared_ptr<TBinaryProtocol> binaryProtocol(new TBinaryProtocol());
   ThriftTestClient testClient(bufferedSocket, binaryProtocol);
-  
+
+  uint64_t time_min = 0;
+  uint64_t time_max = 0;
+  uint64_t time_tot = 0;
+ 
   int test = 0;
   for (test = 0; test < numTests; ++test) {
 
-    /**
-     * CONNECT TEST
-     */
-    printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
     try {
       bufferedSocket->open();
     } catch (TTransportException& ttx) {
       printf("Connect failed: %s\n", ttx.getMessage().c_str());
       continue;
     }
+    
+    /**
+     * CONNECT TEST
+     */
+    printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port);
 
     uint64_t start = now();
     
@@ -379,12 +384,29 @@
     }
     
     uint64_t stop = now();
+    uint64_t tot = stop-start;
+
     printf("Total time: %lu us\n", stop-start);
     
+    time_tot += tot;
+    if (time_min == 0 || tot < time_min) {
+      time_min = tot;
+    }
+    if (tot > time_max) {
+      time_max = tot;
+    }
+
     bufferedSocket->close();
   }
 
   //  printf("\nSocket syscalls: %u", g_socket_syscalls);
   printf("\nAll tests done.\n");
+
+  uint64_t time_avg = time_tot / numTests;
+
+  printf("Min time: %lu us\n", time_min);
+  printf("Max time: %lu us\n", time_max);
+  printf("Avg time: %lu us\n", time_avg);
+
   return 0;
 }
diff --git a/test/cpp/src/TestServer.cc b/test/cpp/src/TestServer.cc
index 97d3440..f743aea 100644
--- a/test/cpp/src/TestServer.cc
+++ b/test/cpp/src/TestServer.cc
@@ -4,6 +4,7 @@
 #include <server/TSimpleServer.h>
 #include <server/TThreadPoolServer.h>
 #include <transport/TServerSocket.h>
+#include <transport/TBufferedTransportFactory.h>
 #include "ThriftTest.h"
 
 #include <iostream>
@@ -53,23 +54,13 @@
   }
 
   Xtruct testStruct(Xtruct thing) {
-    printf("testStruct({\"%s\", %d, %d, %ld})\n",
-           thing.string_thing.c_str(),
-           (int)thing.byte_thing,
-           thing.i32_thing,
-           thing.i64_thing);
+    printf("testStruct({\"%s\", %d, %d, %ld})\n", thing.string_thing.c_str(), (int)thing.byte_thing, thing.i32_thing, thing.i64_thing);
     return thing;
   }
 
   Xtruct2 testNest(Xtruct2 nest) {
     Xtruct thing = nest.struct_thing;
-    printf("testNest({%d, {\"%s\", %d, %d, %ld}, %d})\n",
-           (int)nest.byte_thing,
-           thing.string_thing.c_str(),
-           (int)thing.byte_thing,
-           thing.i32_thing,
-           thing.i64_thing,
-           nest.i32_thing);
+    printf("testNest({%d, {\"%s\", %d, %d, %ld}, %d})\n", (int)nest.byte_thing, thing.string_thing.c_str(), (int)thing.byte_thing, thing.i32_thing, thing.i64_thing, nest.i32_thing);
     return nest;
   }
 
@@ -205,11 +196,7 @@
         list<Xtruct>::const_iterator x;
         printf("{");
         for (x = xtructs.begin(); x != xtructs.end(); ++x) {
-          printf("{\"%s\", %d, %d, %ld}, ",
-                 x->string_thing.c_str(),
-                 (int)x->byte_thing,
-                 x->i32_thing,
-                 x->i64_thing);
+          printf("{\"%s\", %d, %d, %ld}, ", x->string_thing.c_str(), (int)x->byte_thing, x->i32_thing, x->i64_thing);
         }
         printf("}");
 
@@ -347,18 +334,23 @@
 
   shared_ptr<ThriftTestServer> testServer(new ThriftTestServer(testHandler, binaryProtocol));
 
-  // Options
-  shared_ptr<TServerOptions> serverOptions(new TServerOptions());
-
   // Transport
   shared_ptr<TServerSocket> serverSocket(new TServerSocket(port));
 
+  // Factory
+  shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
+
+  // Options
+  shared_ptr<TServerOptions> serverOptions(new TServerOptions());
+
   if (serverType == "simple") {
 
     // Server
     TSimpleServer simpleServer(testServer,
-			       serverOptions,
-			       serverSocket);
+			       serverSocket,
+                               transportFactory,
+			       serverOptions
+                               );
 
     printf("Starting the server on port %d...\n", port);
     simpleServer.run();
@@ -376,9 +368,10 @@
     threadManager->start();
 
     TThreadPoolServer threadPoolServer(testServer,
-				       serverOptions,
 				       serverSocket,
-				       threadManager);
+                                       transportFactory,
+				       threadManager,
+				       serverOptions);
 
     printf("Starting the server on port %d...\n", port);
     threadPoolServer.run();
diff --git a/test/java/src/TestClient.java b/test/java/src/TestClient.java
index 74fbfef..fda37de 100644
--- a/test/java/src/TestClient.java
+++ b/test/java/src/TestClient.java
@@ -42,13 +42,16 @@
       ThriftTest.Client testClient =
         new ThriftTest.Client(tSocket, binaryProtocol);
 
+      long timeMin = 0;
+      long timeMax = 0;
+      long timeTot = 0;
+
       for (int test = 0; test < numTests; ++test) {
 
         /**
          * CONNECT TEST
          */
-        System.out.println("Test #" + (test+1) + ", " +
-                           "connect " + host + ":" + port);
+        System.out.println("Test #" + (test+1) + ", " + "connect " + host + ":" + port);
         try {
           tSocket.open();
         } catch (TTransportException ttx) {
@@ -56,7 +59,7 @@
           continue;
         }
 
-        long start = System.currentTimeMillis();
+        long start = System.nanoTime();
     
         /**
          * VOID TEST
@@ -110,11 +113,7 @@
         out.i32_thing = -3;
         out.i64_thing = -5;
         Xtruct in = testClient.testStruct(out);
-        System.out.print(" = {" +
-                         "\"" + in.string_thing + "\", " +
-                         in.byte_thing + ", " +
-                         in.i32_thing + ", " +
-                         in.i64_thing + "}\n");
+        System.out.print(" = {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}\n");
 
         /**
          * NESTED STRUCT TEST
@@ -126,13 +125,7 @@
         out2.i32_thing = 5;
         Xtruct2 in2 = testClient.testNest(out2);
         in = in2.struct_thing;
-        System.out.print(" = {" +
-                         in2.byte_thing + ", {" +
-                         "\"" + in.string_thing + "\", " +
-                         in.byte_thing + ", " +
-                         in.i32_thing + ", " +
-                         in.i64_thing + "}, " +
-                         in2.i32_thing + "}\n");
+        System.out.print(" = {" + in2.byte_thing + ", {" + "\"" + in.string_thing + "\", " + in.byte_thing + ", " + in.i32_thing + ", " + in.i64_thing + "}, " + in2.i32_thing + "}\n");
 
         /**
          * MAP TEST
@@ -299,19 +292,14 @@
             HashMap<Integer, Long> userMap = v2.userMap;
             System.out.print("{");
             for (int k3 : userMap.keySet()) {
-              System.out.print(k3 + " => " +
-                               userMap.get(k3) + ", ");
+              System.out.print(k3 + " => " + userMap.get(k3) + ", ");
             }
             System.out.print("}, ");
 
             ArrayList<Xtruct> xtructs = v2.xtructs;
             System.out.print("{");
             for (Xtruct x : xtructs) {
-              System.out.print("{" +
-                               "\"" + x.string_thing + "\", " +
-                               x.byte_thing + ", " +
-                               x.i32_thing + ", "+
-                               x.i64_thing + "}, ");
+              System.out.print("{" + "\"" + x.string_thing + "\", " + x.byte_thing + ", " + x.i32_thing + ", "+ x.i64_thing + "}, ");
             }
             System.out.print("}");
 
@@ -321,14 +309,32 @@
         }
         System.out.print("}\n");
 
-        long stop = System.currentTimeMillis();
-        System.out.println("Total time: " + (stop-start) + "ms");
+        long stop = System.nanoTime();
+        long tot = stop-start;
+
+        System.out.println("Total time: " + tot/1000 + "us");
+
+        if (timeMin == 0 || tot < timeMin) {
+          timeMin = tot;
+        }
+        if (tot > timeMax) {
+          timeMax = tot;
+        }
+        timeTot += tot;
 
         tSocket.close();
       }
+
+      long timeAvg = timeTot / numTests;
       
+      System.out.println("Min time: " + timeMin/1000 + "us");
+      System.out.println("Max time: " + timeMax/1000 + "us");
+      System.out.println("Avg time: " + timeAvg/1000 + "us");
+            
     } catch (Exception x) {
       x.printStackTrace();
-    }
+    }  
+
   }
+
 }
diff --git a/test/py/TestServer.py b/test/py/TestServer.py
index 525ffee..db2ad81 100755
--- a/test/py/TestServer.py
+++ b/test/py/TestServer.py
@@ -5,6 +5,7 @@
 
 import ThriftTest
 from ThriftTest_types import *
+from thrift.transport import TTransport
 from thrift.transport import TSocket
 from thrift.protocol import TBinaryProtocol
 from thrift.server import TServer
@@ -54,5 +55,6 @@
 protocol = TBinaryProtocol.TBinaryProtocol()
 handler = TestHandler()
 iface = ThriftTest.Server(handler, protocol)
-server = TServer.TSimpleServer(iface, transport)
+factory = TTransport.TBufferedTransportFactory()
+server = TServer.TSimpleServer(iface, transport, factory)
 server.run()