int main(int argc, char* argv[]) { facebook::config::Flags::initFlags(&argc, &argv, true); signal(SIGINT, exit); if (argc != 1) { fprintf(stderr, "error: unhandled arguments:"); for (int n = 1; n < argc; ++n) { fprintf(stderr, " %s", argv[n]); } fprintf(stderr, "\n"); return 1; } // transport and protocol factories std::shared_ptr<TTransportFactory> transportFactory; if (FLAGS_framed) { transportFactory.reset(new TFramedTransportFactory); } else { transportFactory.reset(new TBufferedTransportFactory); } std::shared_ptr<TProtocolFactory> protocolFactory; std::shared_ptr<TDuplexProtocolFactory> duplexProtocolFactory; std::shared_ptr<TDuplexTransportFactory> duplexTransportFactory; if (FLAGS_header) { std::bitset<CLIENT_TYPES_LEN> clientTypes; clientTypes[THRIFT_UNFRAMED_DEPRECATED] = 1; clientTypes[THRIFT_FRAMED_DEPRECATED] = 1; clientTypes[THRIFT_HTTP_SERVER_TYPE] = 1; clientTypes[THRIFT_HEADER_CLIENT_TYPE] = 1; THeaderProtocolFactory *factory = new THeaderProtocolFactory(); factory->setClientTypes(clientTypes); duplexProtocolFactory = std::shared_ptr<TDuplexProtocolFactory>(factory); transportFactory.reset(new TTransportFactory()); duplexTransportFactory = std::shared_ptr<TDuplexTransportFactory>( new TSingleTransportFactory<TTransportFactory>(transportFactory)); } else { protocolFactory.reset(new TBinaryProtocolFactoryT<TBufferBase>); } // server socket std::shared_ptr<TServerSocket> serverSocket(new TServerSocket(FLAGS_port)); // handler and processor std::shared_ptr<LoadHandler> handler(new LoadHandler); typedef TBinaryProtocolT<TBufferBase> ProtocolType; typedef LoadTestProcessorT<ProtocolType> LoadProcessor; std::shared_ptr<LoadProcessor> processor(new LoadProcessor(handler)); // the server itself scoped_ptr<TThreadedServer> server; if (FLAGS_header) { server.reset(new TThreadedServer(processor, serverSocket, duplexTransportFactory, duplexProtocolFactory)); } else { server.reset(new TThreadedServer(processor, serverSocket, transportFactory, protocolFactory)); } // set tunable parameters setTunables(serverSocket.get()); std::unique_ptr<facebook::services::ServiceFramework> fwk; if (FLAGS_enable_service_framework) { fwk.reset( new facebook::services::ServiceFramework("ThreadedServer Load Tester")); fwk->go(false /* waitUntilStop */); } std::cout << "Serving requests on port " << FLAGS_port << "...\n"; server->serve(); return 0; }
int main(int argc, char* argv[]) { facebook::config::Flags::initFlags(&argc, &argv, true); if (argc != 1) { fprintf(stderr, "error: unhandled arguments:"); for (int n = 1; n < argc; ++n) { fprintf(stderr, " %s", argv[n]); } fprintf(stderr, "\n"); return 1; } // Optionally run under ServiceFramework to enable testing stats handlers // Once ServiceFramework is running, it will hook any new thrift services that // get created, and attach a collect/report their fb303 stats. std::unique_ptr<facebook::services::ServiceFramework> fwk; if (FLAGS_enable_service_framework) { fwk.reset( new facebook::services::ServiceFramework("EventServer Load Tester")); fwk->go(false /* waitUntilStop */); } std::shared_ptr<AsyncLoadHandler> handler(new AsyncLoadHandler); std::shared_ptr<LoadHandler> queueHandler(new LoadHandler); typedef TBinaryProtocolT<TBufferBase> ProtocolType; typedef LoadTestAsyncProcessorT<ProtocolType> LoadProcessor; typedef LoadTestProcessorT<ProtocolType> LoadQueueProcessor; typedef LoadTestAsyncProcessorT<THeaderProtocol> HeaderLoadProcessor; typedef LoadTestProcessorT<THeaderProtocol> HeaderLoadQueueProcessor; std::shared_ptr<TAsyncProcessor> processor; std::shared_ptr<LoadQueueProcessor> queueProcessor( new LoadQueueProcessor(queueHandler)); std::shared_ptr<HeaderLoadQueueProcessor> headerQueueProcessor( new HeaderLoadQueueProcessor(queueHandler)); std::shared_ptr<TProtocolFactory> protocolFactory; std::shared_ptr<TDuplexProtocolFactory> duplexProtocolFactory; if (FLAGS_header) { std::bitset<CLIENT_TYPES_LEN> clientTypes; clientTypes[THRIFT_UNFRAMED_DEPRECATED] = 1; clientTypes[THRIFT_FRAMED_DEPRECATED] = 1; clientTypes[THRIFT_HEADER_CLIENT_TYPE] = 1; THeaderProtocolFactory *factory = new THeaderProtocolFactory(); factory->setClientTypes(clientTypes); duplexProtocolFactory = std::shared_ptr<TDuplexProtocolFactory>(factory); if (FLAGS_sync_to_async_mode) { processor.reset(new TSyncToAsyncProcessor(headerQueueProcessor)); } else { processor.reset(new HeaderLoadProcessor(handler)); } } else { protocolFactory.reset(new TBinaryProtocolFactoryT<TBufferBase>); if (FLAGS_sync_to_async_mode) { processor.reset(new TSyncToAsyncProcessor(queueProcessor)); } else { processor.reset(new LoadProcessor(handler)); } } scoped_ptr<TEventServer> server; if (FLAGS_task_queue_mode) { std::shared_ptr<ThreadManager> threadManager( ThreadManager::newSimpleThreadManager( FLAGS_num_queue_threads)); std::shared_ptr<PosixThreadFactory> threadFactory(new PosixThreadFactory()); threadManager->threadFactory(threadFactory); threadManager->start(); if (FLAGS_header) { server.reset(new TEventServer(headerQueueProcessor, duplexProtocolFactory, FLAGS_port, threadManager, FLAGS_num_threads)); } else { server.reset(new TEventServer(queueProcessor, protocolFactory, FLAGS_port, threadManager, FLAGS_num_threads)); } } else { if (FLAGS_header) { server.reset(new TEventServer(processor, duplexProtocolFactory, FLAGS_port, FLAGS_num_threads)); } else { server.reset(new TEventServer(processor, protocolFactory, FLAGS_port, FLAGS_num_threads)); } } if (!FLAGS_framed_transport) { server->setTransportType(TEventServer::TransportType::UNFRAMED_BINARY); } if (FLAGS_cert.length() > 0 && FLAGS_key.length() > 0) { std::shared_ptr<SSLContext> sslContext(new SSLContext()); sslContext->loadCertificate(FLAGS_cert.c_str()); sslContext->loadPrivateKey(FLAGS_key.c_str()); server->setSSLContext(sslContext); } handler->setServer(server.get()); // Set tunable server parameters setTunables(server.get()); g_server = server.get(); signal(SIGINT, sigHandler); cout << "Serving requests on port " << FLAGS_port << "...\n"; server->serve(); cout << "Exiting normally" << std::endl; fwk->stop(); return 0; }
int main(int argc, char **argv) { int port = 9090; string serverType = "simple"; string protocolType = "binary"; size_t workerCount = 4; bool ssl = false; bool header = false; ostringstream usage; usage << argv[0] << " [--port=<port number>] [--server-type=<server-type>] " << "[--protocol-type=<protocol-type>] [--workers=<worker-count>] " << "[--processor-events]" << endl << "\t\tserver-type\t\ttype of server, \"simple\", \"thread-pool\", " << "\"threaded\", or \"event\". Default is " << serverType << endl << "\t\tprotocol-type\t\ttype of protocol, \"binary\", \"header\", " << "\"ascii\", or \"xml\". Default is " << protocolType << endl << "\t\tworkers\t\tNumber of thread pools workers. Only valid for " << "thread-pool server type. Default is " << workerCount << endl; map<string, string> args; for (int ix = 1; ix < argc; ix++) { string arg(argv[ix]); if (arg.compare(0,2, "--") == 0) { size_t end = arg.find_first_of("=", 2); if (end != string::npos) { args[string(arg, 2, end - 2)] = string(arg, end + 1); } else { args[string(arg, 2)] = "true"; } } else { throw invalid_argument("Unexcepted command line token: "+arg); } } try { if (!args["port"].empty()) { port = atoi(args["port"].c_str()); } if (!args["server-type"].empty()) { serverType = args["server-type"]; if (serverType == "simple") { } else if (serverType == "thread-pool") { } else if (serverType == "threaded") { } else if (serverType == "event") { } else { throw invalid_argument("Unknown server type "+serverType); } } if (!args["protocol-type"].empty()) { protocolType = args["protocol-type"]; if (protocolType == "binary") { } else if (protocolType == "header") { header = true; } else if (protocolType == "ascii") { throw invalid_argument("ASCII protocol not supported"); } else if (protocolType == "xml") { throw invalid_argument("XML protocol not supported"); } else { throw invalid_argument("Unknown protocol type "+protocolType); } } if (!args["workers"].empty()) { workerCount = atoi(args["workers"].c_str()); } } catch (std::exception& e) { cerr << e.what() << endl; cerr << usage; } // OpenSSL may trigger SIGPIPE when remote sends connection reset. if (args["ssl"] == "true") { ssl = true; signal(SIGPIPE, SIG_IGN); } // Dispatcher std::shared_ptr<TProtocolFactory> protocolFactory; std::shared_ptr<TDuplexProtocolFactory> duplexProtocolFactory; if (header) { std::bitset<CLIENT_TYPES_LEN> clientTypes; clientTypes[THRIFT_UNFRAMED_DEPRECATED] = 1; clientTypes[THRIFT_FRAMED_DEPRECATED] = 1; clientTypes[THRIFT_HTTP_SERVER_TYPE] = 1; clientTypes[THRIFT_HEADER_CLIENT_TYPE] = 1; THeaderProtocolFactory* factory = new THeaderProtocolFactory(); factory->setClientTypes(clientTypes); duplexProtocolFactory = std::shared_ptr<TDuplexProtocolFactory>(factory); } else { protocolFactory = std::shared_ptr<TProtocolFactory>( new TBinaryProtocolFactoryT<TBufferBase>()); } std::shared_ptr<TestHandler> testHandler(new TestHandler()); std::shared_ptr<TProcessor> testProcessor( new ThriftTestProcessor(testHandler)); if (!args["processor-events"].empty()) { testProcessor->setEventHandler(std::shared_ptr<TProcessorEventHandler>( new TestProcessorEventHandler())); } // Transport std::shared_ptr<TServerSocket> serverSocket; if (ssl) { std::shared_ptr<SSLContext> sslContext(new SSLContext()); sslContext->loadCertificate("./server-certificate.pem"); sslContext->loadPrivateKey("./server-private-key.pem"); sslContext->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH"); std::shared_ptr<TSSLSocketFactory> sslSocketFactory( new TSSLSocketFactory(sslContext)); serverSocket = std::shared_ptr<TServerSocket>( new TSSLServerSocket(port, sslSocketFactory)); } else { serverSocket = std::shared_ptr<TServerSocket>(new TServerSocket(port)); } // Factory std::shared_ptr<TTransportFactory> transportFactory( new TBufferedTransportFactory()); std::shared_ptr<TDuplexTransportFactory> duplexTransportFactory( new TSingleTransportFactory<TTransportFactory>(transportFactory)); std::shared_ptr<TServer> server; if (serverType == "simple") { // Server // "Testing TSimpleServer" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" server = std::shared_ptr<TServer>(new TSimpleServer(testProcessor, serverSocket, transportFactory, protocolFactory)); #pragma GCC diagnostic pop printf("Starting the server on port %d...\n", port); } else if (serverType == "thread-pool") { std::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(workerCount); std::shared_ptr<PosixThreadFactory> threadFactory = std::shared_ptr<PosixThreadFactory>(new PosixThreadFactory()); threadManager->threadFactory(threadFactory); threadManager->start(); // "Testing TThreadPoolServer" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" server = std::shared_ptr<TServer>(new TThreadPoolServer(testProcessor, serverSocket, transportFactory, protocolFactory, threadManager)); #pragma GCC diagnostic pop printf("Starting the server on port %d...\n", port); } else if (serverType == "threaded") { server = std::shared_ptr<TServer>(new TThreadedServer(testProcessor, serverSocket, transportFactory, protocolFactory)); printf("Starting the server on port %d...\n", port); } else if (serverType == "event") { server = std::shared_ptr<TServer>(new TEventServer(testProcessor, protocolFactory, port)); printf("Starting the event server on port %d...\n", port); } if (header) { server->setDuplexTransportFactory(duplexTransportFactory); server->setDuplexProtocolFactory(duplexProtocolFactory); } server->serve(); printf("done.\n"); return 0; }
void runTestCase(ServerType sType, ClientType clientType) { std::bitset<CLIENT_TYPES_LEN> clientTypes; clientTypes[THRIFT_UNFRAMED_DEPRECATED] = 1; clientTypes[THRIFT_FRAMED_DEPRECATED] = 1; clientTypes[THRIFT_HTTP_SERVER_TYPE] = 1; clientTypes[THRIFT_HEADER_CLIENT_TYPE] = 1; clientTypes[THRIFT_FRAMED_COMPACT] = 1; THeaderProtocolFactory* factory = new THeaderProtocolFactory(); factory->setClientTypes(clientTypes); std::shared_ptr<TDuplexProtocolFactory> protocolFactory = std::shared_ptr<TDuplexProtocolFactory>(factory); std::shared_ptr<TestHandler> testHandler(new TestHandler()); std::shared_ptr<TDispatchProcessor> wrappedProcessor( new ServiceProcessor(testHandler)); // Wrap the processor to observe exceptions in processing std::shared_ptr<TProcessor> testProcessor( new TProcessorWrapper(wrappedProcessor)); std::shared_ptr<TAsyncProcessor> testAsyncProcessor( new TSyncToAsyncProcessor(testProcessor)); std::shared_ptr<TDuplexTransportFactory> transportFactory( new TSingleTransportFactory<TBufferedTransportFactory>()); auto event_handler = std::shared_ptr<TProcessorEventHandler>( new HeaderEventHandler()); // set the header-replying event handler wrappedProcessor->addEventHandler(event_handler); testAsyncProcessor->addEventHandler(event_handler); int port = 0; std::shared_ptr<TServer> server; std::shared_ptr<ServerCreatorBase> serverCreator; switch(sType) { case SERVER_TYPE_SIMPLE: // "Testing TSimpleServerCreator" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" serverCreator = std::shared_ptr<ServerCreatorBase>(new TSimpleServerCreator( testProcessor, port, false)); #pragma GCC diagnostic pop break; case SERVER_TYPE_THREADED: serverCreator = std::shared_ptr<ServerCreatorBase>(new TThreadedServerCreator( testProcessor, port, false)); break; case SERVER_TYPE_THREADPOOL: // "Testing TThreadPoolServerCreator" #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" serverCreator = std::shared_ptr<ServerCreatorBase>( new TThreadPoolServerCreator(testProcessor, port, false)); #pragma GCC diagnostic pop break; case SERVER_TYPE_EVENT: serverCreator.reset(new TEventServerCreator(testAsyncProcessor, port)); break; } serverCreator->setDuplexProtocolFactory(protocolFactory); server = serverCreator->createServer(); ScopedServerThread serverThread(server); const folly::SocketAddress* address = serverThread.getAddress(); runClient(clientType, sType, address->getPort()); }