From a330265d8232c7e095f3fb0a03e001f349b806dd Mon Sep 17 00:00:00 2001 From: Mark Slee Date: Wed, 25 Oct 2006 19:03:32 +0000 Subject: [PATCH] Fix broken thrift test code for new model git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664839 13f79535-47bb-0310-9956-ffa450edef68 --- test/ThriftTest.thrift | 63 ++++++++++++++++++----------------- test/cpp/src/TestClient.cpp | 59 +++++++++++++++++++++++--------- test/cpp/src/TestServer.cpp | 37 +++++++++++--------- test/java/src/TestClient.java | 28 +++++++++++----- test/py/TestClient.py | 47 +++++++++++++++++++++++++- 5 files changed, 163 insertions(+), 71 deletions(-) diff --git a/test/ThriftTest.thrift b/test/ThriftTest.thrift index 414dceab..f5642ac5 100644 --- a/test/ThriftTest.thrift +++ b/test/ThriftTest.thrift @@ -1,4 +1,5 @@ -namespace thrift.test +java_package thrift.test +cpp_namespace thrift.test enum Numberz { @@ -14,61 +15,61 @@ typedef i64 UserId struct Xtruct { - string string_thing = 1, - byte byte_thing = 4, - i32 i32_thing = 9, - i64 i64_thing = 11 + 1: string string_thing, + 4: byte byte_thing, + 9: i32 i32_thing, + 11: i64 i64_thing } struct Xtruct2 { - byte byte_thing, - Xtruct struct_thing, - i32 i32_thing + 1: byte byte_thing, + 2: Xtruct struct_thing, + 3: i32 i32_thing } struct Insanity { - map userMap = 0, - list xtructs = 1 + 1: map userMap, + 2: list xtructs } exception Xception { - i32 errorCode, - string message + 1: i32 errorCode, + 2: string message } exception Xception2 { - i32 errorCode, - Xtruct struct_thing + 1: i32 errorCode, + 2: Xtruct struct_thing } struct EmptyStruct {} struct OneField { - EmptyStruct field + 1: EmptyStruct field } service ThriftTest { void testVoid(), - string testString(string thing = 1), - byte testByte(byte thing = 1), - i32 testI32(i32 thing = 1), - i64 testI64(i64 thing = 1), - double testDouble(double thing = 1), - Xtruct testStruct(Xtruct thing = 1), - Xtruct2 testNest(Xtruct2 thing = 1), - map testMap(map thing = 1), - set testSet(set thing = 1), - list testList(list thing = 1), - Numberz testEnum(Numberz thing = 1), - UserId testTypedef(UserId thing = 1), - - map> testMapMap(i32 hello = 1), + string testString(1: string thing), + byte testByte(1: byte thing), + i32 testI32(1: i32 thing), + i64 testI64(1: i64 thing), + double testDouble(1: double thing), + Xtruct testStruct(1: Xtruct thing), + Xtruct2 testNest(1: Xtruct2 thing), + map testMap(1: map thing), + set testSet(1: set thing), + list testList(1: list thing), + Numberz testEnum(1: Numberz thing), + UserId testTypedef(1: UserId thing), + + map> testMapMap(1: i32 hello), /* So you think you've got this all worked, out eh? */ - map> testInsanity(Insanity argument = 1), + map> testInsanity(1: Insanity argument), /* Multiple parameters */ @@ -80,7 +81,7 @@ service ThriftTest /* Multiple exceptions specifier */ - Xtruct testMultiException(string arg0, string arg1) throws(Xception err1=1, Xception2 err2) + Xtruct testMultiException(string arg0, string arg1) throws(Xception err1, Xception2 err2) } service SecondService diff --git a/test/cpp/src/TestClient.cpp b/test/cpp/src/TestClient.cpp index 83e8f795..e508b0f7 100644 --- a/test/cpp/src/TestClient.cpp +++ b/test/cpp/src/TestClient.cpp @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #include @@ -33,21 +33,50 @@ int main(int argc, char** argv) { string host = "localhost"; int port = 9090; int numTests = 1; - - if (argc > 1) { - host = argv[1]; - } - if (argc > 2) { - port = atoi(argv[2]); - } - if (argc > 3) { - numTests = atoi(argv[3]); + bool framed = false; + bool frameInput = true; + + for (int i = 0; i < argc; ++i) { + if (strcmp(argv[i], "-h") == 0) { + char* pch = strtok(argv[++i], ":"); + if (pch != NULL) { + host = string(pch); + } + pch = strtok(NULL, ":"); + if (pch != NULL) { + port = atoi(pch); + } + } else if (strcmp(argv[i], "-n") == 0) { + numTests = atoi(argv[++i]); + } else if (strcmp(argv[i], "-f") == 0) { + framed = true; + } else if (strcmp(argv[i], "-fo") == 0) { + framed = true; + frameInput = false; + } } + + shared_ptr transport; + shared_ptr socket(new TSocket(host, port)); - shared_ptr bufferedSocket(new TBufferedTransport(socket)); - shared_ptr binaryProtocol(new TBinaryProtocol()); - ThriftTestClient testClient(bufferedSocket, binaryProtocol); + + if (framed) { + shared_ptr framedSocket(new TFramedTransport(socket)); + framedSocket->setRead(frameInput); + transport = framedSocket; + if (frameInput) { + printf("Using bi-directional framed transport mode\n"); + } else { + printf("Using framed output only mode\n"); + } + } else { + shared_ptr bufferedSocket(new TBufferedTransport(socket)); + transport = bufferedSocket; + } + + shared_ptr protocol(new TBinaryProtocol(transport, transport)); + ThriftTestClient testClient(protocol); uint64_t time_min = 0; uint64_t time_max = 0; @@ -57,7 +86,7 @@ int main(int argc, char** argv) { for (test = 0; test < numTests; ++test) { try { - bufferedSocket->open(); + transport->open(); } catch (TTransportException& ttx) { printf("Connect failed: %s\n", ttx.getMessage().c_str()); continue; @@ -396,7 +425,7 @@ int main(int argc, char** argv) { time_max = tot; } - bufferedSocket->close(); + transport->close(); } // printf("\nSocket syscalls: %u", g_socket_syscalls); diff --git a/test/cpp/src/TestServer.cpp b/test/cpp/src/TestServer.cpp index 34c9dfb5..28a40fd0 100644 --- a/test/cpp/src/TestServer.cpp +++ b/test/cpp/src/TestServer.cpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include "ThriftTest.h" #include @@ -264,13 +264,14 @@ int main(int argc, char **argv) { string serverType = "simple"; string protocolType = "binary"; size_t workerCount = 4; + bool frameOutput = true; ostringstream usage; usage << argv[0] << " [--port=] [--server-type=] [--protocol-type=] [--workers=]" << endl << - "\t\tserver-type\t\ttype of server, \"simple\" or \"thread-pool\". Default is " << serverType << endl << + "\t\tserver-type\t\ttype of server, \"simple\", \"thread-pool\", or \"nonblocking\". Default is " << serverType << endl << "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"ascii\", or \"xml\". Default is " << protocolType << endl << @@ -285,9 +286,8 @@ int main(int argc, char **argv) { if (end != string::npos) { args[string(arg, 2, end - 2)] = string(arg, end + 1); } else { - args[string(arg, 2, end - 2)] = "true"; + args[string(arg, 2)] = "true"; } - ix++; } else { throw invalid_argument("Unexcepted command line token: "+arg); } @@ -299,6 +299,10 @@ int main(int argc, char **argv) { port = atoi(args["port"].c_str()); } + if (!args["noframe"].empty()) { + frameOutput = false; + } + if (!args["server-type"].empty()) { serverType = args["server-type"]; if (serverType == "simple") { @@ -330,11 +334,11 @@ int main(int argc, char **argv) { } // Dispatcher - shared_ptr binaryProtocol(new TBinaryProtocol); + shared_ptr protocolFactory(new TBinaryProtocolFactory()); shared_ptr testHandler(new TestHandler()); - shared_ptr testProcessor(new ThriftTestProcessor(testHandler, binaryProtocol)); + shared_ptr testProcessor(new ThriftTestProcessor(testHandler)); // Transport shared_ptr serverSocket(new TServerSocket(port)); @@ -342,17 +346,13 @@ int main(int argc, char **argv) { // Factory shared_ptr transportFactory(new TBufferedTransportFactory()); - // Options - shared_ptr serverOptions(new TServerOptions()); - if (serverType == "simple") { // Server TSimpleServer simpleServer(testProcessor, serverSocket, transportFactory, - serverOptions - ); + protocolFactory); printf("Starting the server on port %d...\n", port); simpleServer.serve(); @@ -372,17 +372,22 @@ int main(int argc, char **argv) { TThreadPoolServer threadPoolServer(testProcessor, serverSocket, transportFactory, - threadManager, - serverOptions); + protocolFactory, + threadManager); printf("Starting the server on port %d...\n", port); threadPoolServer.serve(); } else if (serverType == "nonblocking") { - TNonblockingServer nonblockingServer(testProcessor, - serverOptions, - port); + TNonblockingServer nonblockingServer(testProcessor, port); + nonblockingServer.setFrameResponses(frameOutput); + if (frameOutput) { + printf("Using framed output mode\n"); + } else { + printf("Using non-framed output mode\n"); + } + printf("Starting the nonblocking server on port %d...\n", port); nonblockingServer.serve(); diff --git a/test/java/src/TestClient.java b/test/java/src/TestClient.java index 46a4ca5a..546265c0 100644 --- a/test/java/src/TestClient.java +++ b/test/java/src/TestClient.java @@ -6,6 +6,7 @@ import thrift.test.*; import com.facebook.thrift.transport.TTransport; import com.facebook.thrift.transport.TSocket; import com.facebook.thrift.transport.THttpClient; +import com.facebook.thrift.transport.TFramedTransport; import com.facebook.thrift.transport.TTransportException; import com.facebook.thrift.protocol.TBinaryProtocol; @@ -27,20 +28,24 @@ public class TestClient { int port = 9090; String url = null; int numTests = 1; + boolean framed = false; + boolean framedInput = true; + boolean framedOutput = true; try { for (int i = 0; i < args.length; ++i) { if (args[i].equals("-h")) { - String[] hostport = (args[++i]).split(";"); + String[] hostport = (args[++i]).split(":"); host = hostport[0]; port = Integer.valueOf(hostport[1]); - } - - if (args[i].equals("-u")) { + } else if (args[i].equals("-f") || args[i].equals("-framed")) { + framed = true; + } else if (args[i].equals("-fo")) { + framed = true; + framedInput = false; + } else if (args[i].equals("-u")) { url = args[++i]; - } - - if (args[i].equals("-n")) { + } else if (args[i].equals("-n")) { numTests = Integer.valueOf(args[++i]); } } @@ -53,7 +58,14 @@ public class TestClient { if (url != null) { transport = new THttpClient(url); } else { - transport = new TSocket(host, port); + TSocket socket = new TSocket(host, port); + socket.setTimeout(1000); + transport = socket; + if (framed) { + transport = new TFramedTransport(transport, + framedInput, + framedOutput); + } } TBinaryProtocol binaryProtocol = diff --git a/test/py/TestClient.py b/test/py/TestClient.py index 3edff1cb..a2fa3c71 100755 --- a/test/py/TestClient.py +++ b/test/py/TestClient.py @@ -9,12 +9,49 @@ from thrift.transport import TTransport from thrift.transport import TSocket from thrift.protocol import TBinaryProtocol +import hotshot +from hotshot import stats +prof = None + +# Uncomment if you want to profile this biznizzy +#prof = hotshot.Profile('hotshot_thrift_stats') +#prof.start() + import timing -transport = TTransport.TBufferedTransport(TSocket.TSocket('localhost', 9090)) +host = 'localhost' +port = 9090 +framed = False +framedInput = True +argi = 1 + +# Parse args +while argi < len(sys.argv): + if sys.argv[argi] == '-h': + parts = sys.argv[argi+1].split(':') + host = parts[0] + port = int(parts[1]) + argi += 1 + elif sys.argv[argi] == '-f' or sys.argv[argi] == '-framed': + framed = True + elif sys.argv[argi] == '-fo': + framed = True + framedInput = False + argi += 1 + +# Make socket +socket = TSocket.TSocket(host, port) + +# Frame or buffer depending upon args +if framed: + transport = TTransport.TFramedTransport(socket, framedInput, True) +else: + transport = TTransport.TBufferedTransport(socket) + protocol = TBinaryProtocol.TBinaryProtocol() client = ThriftTest.Client(transport, protocol) +# Connect! transport.open() # Start debug timing @@ -63,4 +100,12 @@ except Xception, x: timing.finish() print "Total time: %d microsecs" % timing.micro() +# Close! transport.close() + +# Profiler output +if prof != None: + prof.stop() + prof.close() + s = stats.load('hotshot_thrift_stats') + s.sort_stats('time').print_stats() -- 2.17.1