| 39 | } |
| 40 | |
| 41 | int main(int argc, char** argv) |
| 42 | { |
| 43 | if (argc < 3) |
| 44 | { |
| 45 | std::cout << "./benchwebsocket host port [ connections workers ]"; |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | const char* host = argv[1]; |
| 50 | int port = std::atoi(argv[2]); |
| 51 | int connections = argc > 3 ? std::atoi(argv[3]) : 200; |
| 52 | size_t workers = argc > 4 ? std::atoi(argv[4]) : std::thread::hardware_concurrency(); |
| 53 | |
| 54 | std::cout << "host: " << host << ':' << port << " | connections: " << connections << " | workers: " << workers << std::endl; |
| 55 | |
| 56 | auto enterCallback = [host](const HttpSession::Ptr& httpSession, HttpSessionHandlers& handlers) { |
| 57 | HttpRequest request; |
| 58 | request.setMethod(HttpRequest::HTTP_METHOD::HTTP_METHOD_GET); |
| 59 | request.setUrl("/ws"); |
| 60 | request.addHeadValue("Host", host); |
| 61 | request.addHeadValue("Upgrade", "websocket"); |
| 62 | request.addHeadValue("Connection", "Upgrade"); |
| 63 | request.addHeadValue("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ=="); |
| 64 | request.addHeadValue("Sec-WebSocket-Version", "13"); |
| 65 | |
| 66 | std::string requestStr = request.getResult(); |
| 67 | httpSession->send(requestStr.c_str(), requestStr.size()); |
| 68 | |
| 69 | handlers.setWSConnected([](const HttpSession::Ptr& session, const HTTPParser&) { |
| 70 | for (int i = 0; i < 200; i++) |
| 71 | { |
| 72 | sendPacket(session, "hello, world!", 13); |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | handlers.setWSCallback([](const HttpSession::Ptr& session, |
| 77 | WebSocketFormat::WebSocketFrameType, const std::string& payload) { |
| 78 | std::cout << payload << std::endl; |
| 79 | sendPacket(session, "hello, world!", 13); |
| 80 | count += 1; |
| 81 | }); |
| 82 | }; |
| 83 | |
| 84 | auto service = IOThreadTcpService::Create(); |
| 85 | service->startWorkerThread(workers); |
| 86 | |
| 87 | auto connector = AsyncConnector::Create(); |
| 88 | connector->startWorkerThread(); |
| 89 | |
| 90 | wrapper::HttpConnectionBuilder connectionBuilder; |
| 91 | connectionBuilder.WithService(service) |
| 92 | .WithConnector(connector) |
| 93 | .WithMaxRecvBufferSize(1024 * 1024); |
| 94 | |
| 95 | for (int i = 0; i < connections; i++) |
| 96 | { |
| 97 | connectionBuilder |
| 98 | .WithAddr(host, port) |
nothing calls this directly
no test coverage detected