void RemoteUDPListener::run() { poco_assert (_stopped); Poco::Buffer<char> buffer(BUFFER_SIZE); _stopped = false; Poco::Timespan waitTime(WAITTIME_MILLISEC* 1000); while (!_stopped) { try { if (_socket.poll(waitTime, Socket::SELECT_READ)) { int byteCnt = _socket.receiveBytes(buffer.begin(), BUFFER_SIZE); if (byteCnt > 0) { _queue.enqueueNotification(new MessageNotification(std::string(buffer.begin(), byteCnt))); } } } catch (...) { // lazy exception catching } } }
void DatagramSocketTest::testUnbound() { UDPEchoServer echoServer; DatagramSocket ss; char buffer[256]; ss.connect(SocketAddress("localhost", echoServer.port())); int n = ss.sendBytes("hello", 5); assert (n == 5); n = ss.receiveBytes(buffer, sizeof(buffer)); assert (n == 5); assert (std::string(buffer, n) == "hello"); ss.close(); }
void DatagramLocalSocketTest::testDatagramSocketPerformance() { Timestamp::TimeDiff local, net; std::size_t initBufSize = 1; std::size_t initReps = 1; double locData = 0.0, locTime = 0.0, netData = 0.0, netTime = 0.0; std::cout << std::endl << "OS Name: " << Environment::osName() << std::endl; std::cout << "OS Version: " << Environment::osVersion() << std::endl; std::cout << "OS Architecture: " << Environment::osArchitecture() << std::endl; std::ostringstream os; os << Environment::osName() << '-' << Environment::osVersion() << '-' << Environment::osArchitecture() << "-UDP.csv"; File f(os.str()); if (f.exists()) f.remove(); FileOutputStream fos(os.str()); for (std::size_t repetitions = initReps; repetitions <= 100000; repetitions *= 10) { for (std::size_t bufSize = initBufSize; bufSize < 20000; bufSize *= 2) { char* pBuf = new char[bufSize]; { UDPLocalEchoServer echoServer(bufSize); DatagramSocket ss(SocketAddress("/tmp/poco.client.udp.sock"), true); SocketAddress addr(echoServer.address().toString()); ss.connect(addr); int recv = 0, sent = 0; Stopwatch sw; int i = 0; for (; i < repetitions; ++i) { sent = 0; recv = 0; local = 0; do { int s; sw.restart(); s = ss.sendBytes(pBuf + sent, bufSize - sent); sw.stop(); local += sw.elapsed(); sent += s; } while (sent < bufSize); do { int r; sw.restart(); r = ss.receiveBytes(pBuf + recv, bufSize - recv); sw.stop(); local += sw.elapsed(); recv += r; } while (recv < bufSize); locData += sent; locData += recv; locTime += local; poco_assert (sent == bufSize && recv == bufSize); } std::cout << "Local UDP socket, " << i << " repetitions, " << bufSize << " bytes, " << local << " [us]." << std::endl; ss.close(); } { UDPEchoServer echoServer(bufSize); DatagramSocket ss; ss.connect(SocketAddress("localhost", echoServer.port())); int recv = 0, sent = 0; Stopwatch sw; int i = 0; for (; i < repetitions; ++i) { sent = 0; recv = 0; net = 0; do { int s; sw.restart(); s = ss.sendBytes(pBuf + sent, bufSize - sent); sw.stop(); net += sw.elapsed(); sent += s; } while (sent < bufSize); do { int r; sw.restart(); r = ss.receiveBytes(pBuf + recv, bufSize - recv); sw.stop(); net += sw.elapsed(); recv += r; } while (recv < bufSize); netData += sent; netData += recv; netTime += net; poco_assert (sent == bufSize && recv == bufSize); } std::cout << "Network UDP socket, " << i << " repetitions, " << bufSize << " bytes, " << net << " [us]." << std::endl; fos << i << ',' << bufSize << ','; ss.close(); } delete pBuf; double ratio = local ? ((double) net) / ((double) local) : (double) net; double diff = ((double) net) - ((double) local); std::cout << "Ratio: " << ratio << "(" << diff / 1000.0 << "[us/msg]" << ')' << std::endl; fos << ratio << std::endl; } } poco_assert (locData == netData); double locDTR = ((locData / (locTime / Timestamp::resolution())) * 8) / 1000000; double netDTR = ((netData / (netTime / Timestamp::resolution())) * 8) / 1000000; std::cout << "=================================" << std::endl << "Local DTR: " << locDTR << " [Mbit/s]" << std::endl << "Network DTR: " << netDTR << " [Mbit/s]" << std::endl << "Local sockets speedup: " << ((locDTR / netDTR) * 100) - 100 << '%' << std::endl << "=================================" << std::endl; fos << "=================================" << std::endl << "Local DTR: " << locDTR << " [Mbit/s]" << std::endl << "Network DTR: " << netDTR << " [Mbit/s]" << std::endl << "Local sockets speedup: " << ((locDTR / netDTR) * 100) - 100 << '%' << std::endl << "=================================" << std::endl; fos.close(); }