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();