thrift_server.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include "gen-cpp/BenchmarkThrift.h"
  2. #include <thrift/protocol/TBinaryProtocol.h>
  3. #include <thrift/transport/TServerSocket.h>
  4. #include <thrift/server/TNonblockingServer.h>
  5. #include <thrift/concurrency/ThreadManager.h>
  6. #include <thrift/concurrency/PosixThreadFactory.h>
  7. #include <thrift/transport/TBufferTransports.h>
  8. using namespace ::apache::thrift;
  9. using namespace ::apache::thrift::protocol;
  10. using namespace ::apache::thrift::transport;
  11. using namespace ::apache::thrift::server;
  12. using namespace ::apache::thrift::concurrency;
  13. using boost::shared_ptr;
  14. class BenchmarkThriftHandler : virtual public BenchmarkThriftIf {
  15. public:
  16. void echo_thrift(const std::string& msg) { }
  17. void slow_thrift(const std::string& msg) {
  18. usleep(15000);
  19. }
  20. };
  21. int main(int argc, char **argv) {
  22. int port = 8811;
  23. shared_ptr<BenchmarkThriftHandler> handler(new BenchmarkThriftHandler());
  24. shared_ptr<TProcessor> processor(new BenchmarkThriftProcessor(handler));
  25. shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
  26. boost::shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager(16);
  27. boost::shared_ptr<PosixThreadFactory> threadFactory = boost::shared_ptr<PosixThreadFactory>(new PosixThreadFactory());
  28. TNonblockingServer server(processor, protocolFactory, port, threadManager);
  29. server.setMaxConnections(2048);
  30. server.setNumIOThreads(16);
  31. threadManager->threadFactory(threadFactory);
  32. threadManager->start();
  33. server.serve();
  34. return 0;
  35. }