benchmark.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <Poco/Net/ServerSocket.h>
  2. #include <Poco/Net/HTTPServer.h>
  3. #include <Poco/Net/HTTPRequestHandler.h>
  4. #include <Poco/Net/HTTPRequestHandlerFactory.h>
  5. #include <Poco/Net/HTTPResponse.h>
  6. #include <Poco/Net/HTTPServerRequest.h>
  7. #include <Poco/Net/HTTPServerResponse.h>
  8. #include <Poco/Util/ServerApplication.h>
  9. #include <Poco/Timespan.h>
  10. #include <Poco/Thread.h>
  11. #include <iostream>
  12. #include <string>
  13. #include <vector>
  14. #define PLAIN_URL_PATH "/plaintext"
  15. #define PLAIN_CONTENT_TYPE "text/plain"
  16. #define RES_BODY "Hello, World!"
  17. #define SERVER_NAME "poco"
  18. #define MAX_CONNECTIONS 16384
  19. using namespace Poco;
  20. using namespace Poco::Net;
  21. using namespace Poco::Util;
  22. using namespace std;
  23. class MyRequestHandler : public HTTPRequestHandler {
  24. public:
  25. virtual void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp) {
  26. resp.setStatusAndReason(HTTPResponse::HTTP_OK, "OK");
  27. resp.setContentType(PLAIN_CONTENT_TYPE);
  28. resp.add("Server", SERVER_NAME);
  29. resp.sendBuffer(RES_BODY, sizeof(RES_BODY)-1);
  30. return;
  31. }
  32. };
  33. class NotFoundRequestHandler : public HTTPRequestHandler {
  34. public:
  35. virtual void handleRequest(HTTPServerRequest &req, HTTPServerResponse &resp) {
  36. resp.setStatusAndReason(HTTPResponse::HTTP_NOT_FOUND, "NOT_FOUND");
  37. resp.setContentType(PLAIN_CONTENT_TYPE);
  38. resp.add("Server", SERVER_NAME);
  39. resp.sendBuffer("", 0);
  40. return;
  41. }
  42. };
  43. class MyRequestHandlerFactory : public HTTPRequestHandlerFactory {
  44. public:
  45. virtual HTTPRequestHandler* createRequestHandler(const HTTPServerRequest &req) {
  46. if (req.getMethod() == "GET" && req.getURI() == PLAIN_URL_PATH)
  47. return new MyRequestHandler;
  48. else
  49. return new NotFoundRequestHandler;
  50. }
  51. };
  52. class MyServerApp : public ServerApplication {
  53. protected:
  54. int main(const vector<string> &args) {
  55. HTTPServerParams* hsp = new HTTPServerParams;
  56. hsp->setMaxThreads(stoi(args[1]));
  57. hsp->setKeepAlive(true);
  58. hsp->setMaxKeepAliveRequests(MAX_CONNECTIONS);
  59. hsp->setMaxQueued(MAX_CONNECTIONS);
  60. hsp->setThreadPriority(Thread::PRIO_HIGHEST);
  61. ServerSocket socket(stoi(args[0]), MAX_CONNECTIONS);
  62. socket.setBlocking(false);
  63. HTTPServer s(new MyRequestHandlerFactory, socket, hsp);
  64. s.start();
  65. waitForTerminationRequest();
  66. s.stop();
  67. return Application::EXIT_OK;
  68. }
  69. };
  70. int main(int argc, char** argv) {
  71. if (argc != 3) {
  72. std::cerr << "Usage: " << argv[0] << " port nthreads" << std::endl;
  73. return 1;
  74. }
  75. MyServerApp app;
  76. return app.run(argc, argv);
  77. }