benchmark.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "rtc/rtc.hpp"
  19. #include <atomic>
  20. #include <chrono>
  21. #include <iostream>
  22. #include <memory>
  23. #include <thread>
  24. using namespace rtc;
  25. using namespace std;
  26. using namespace chrono_literals;
  27. using chrono::duration_cast;
  28. using chrono::milliseconds;
  29. using chrono::steady_clock;
  30. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  31. size_t benchmark(milliseconds duration) {
  32. rtc::InitLogger(LogLevel::Warning);
  33. rtc::Preload();
  34. Configuration config1;
  35. // config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
  36. // config1.mtu = 1500;
  37. auto pc1 = std::make_shared<PeerConnection>(config1);
  38. Configuration config2;
  39. // config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
  40. // config2.mtu = 1500;
  41. auto pc2 = std::make_shared<PeerConnection>(config2);
  42. pc1->onLocalDescription([wpc2 = make_weak_ptr(pc2)](Description sdp) {
  43. auto pc2 = wpc2.lock();
  44. if (!pc2)
  45. return;
  46. cout << "Description 1: " << sdp << endl;
  47. pc2->setRemoteDescription(std::move(sdp));
  48. });
  49. pc1->onLocalCandidate([wpc2 = make_weak_ptr(pc2)](Candidate candidate) {
  50. auto pc2 = wpc2.lock();
  51. if (!pc2)
  52. return;
  53. cout << "Candidate 1: " << candidate << endl;
  54. pc2->addRemoteCandidate(std::move(candidate));
  55. });
  56. pc1->onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  57. pc1->onGatheringStateChange([](PeerConnection::GatheringState state) {
  58. cout << "Gathering state 1: " << state << endl;
  59. });
  60. pc2->onLocalDescription([wpc1 = make_weak_ptr(pc1)](Description sdp) {
  61. auto pc1 = wpc1.lock();
  62. if (!pc1)
  63. return;
  64. cout << "Description 2: " << sdp << endl;
  65. pc1->setRemoteDescription(std::move(sdp));
  66. });
  67. pc2->onLocalCandidate([wpc1 = make_weak_ptr(pc1)](Candidate candidate) {
  68. auto pc1 = wpc1.lock();
  69. if (!pc1)
  70. return;
  71. cout << "Candidate 2: " << candidate << endl;
  72. pc1->addRemoteCandidate(std::move(candidate));
  73. });
  74. pc2->onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  75. pc2->onGatheringStateChange([](PeerConnection::GatheringState state) {
  76. cout << "Gathering state 2: " << state << endl;
  77. });
  78. const size_t messageSize = 65535;
  79. binary messageData(messageSize);
  80. fill(messageData.begin(), messageData.end(), byte(0xFF));
  81. atomic<size_t> receivedSize = 0;
  82. steady_clock::time_point startTime, openTime, receivedTime, endTime;
  83. shared_ptr<DataChannel> dc2;
  84. pc2->onDataChannel([&dc2, &receivedSize, &receivedTime](shared_ptr<DataChannel> dc) {
  85. dc->onMessage([&receivedTime, &receivedSize](variant<binary, string> message) {
  86. if (holds_alternative<binary>(message)) {
  87. const auto &bin = get<binary>(message);
  88. if (receivedSize == 0)
  89. receivedTime = steady_clock::now();
  90. receivedSize += bin.size();
  91. }
  92. });
  93. dc->onClosed([]() { cout << "DataChannel closed." << endl; });
  94. std::atomic_store(&dc2, dc);
  95. });
  96. startTime = steady_clock::now();
  97. auto dc1 = pc1->createDataChannel("benchmark");
  98. dc1->onOpen([wdc1 = make_weak_ptr(dc1), &messageData, &openTime]() {
  99. auto dc1 = wdc1.lock();
  100. if (!dc1)
  101. return;
  102. openTime = steady_clock::now();
  103. cout << "DataChannel open, sending data..." << endl;
  104. try {
  105. while (dc1->bufferedAmount() == 0) {
  106. dc1->send(messageData);
  107. }
  108. } catch (const std::exception &e) {
  109. std::cout << "Send failed: " << e.what() << std::endl;
  110. }
  111. // When sent data is buffered in the DataChannel,
  112. // wait for onBufferedAmountLow callback to continue
  113. });
  114. dc1->onBufferedAmountLow([wdc1 = make_weak_ptr(dc1), &messageData]() {
  115. auto dc1 = wdc1.lock();
  116. if (!dc1)
  117. return;
  118. // Continue sending
  119. try {
  120. while (dc1->isOpen() && dc1->bufferedAmount() == 0) {
  121. dc1->send(messageData);
  122. }
  123. } catch (const std::exception &e) {
  124. std::cout << "Send failed: " << e.what() << std::endl;
  125. }
  126. });
  127. const int steps = 10;
  128. const auto stepDuration = duration / 10;
  129. for (int i = 0; i < steps; ++i) {
  130. this_thread::sleep_for(stepDuration);
  131. cout << "Received: " << receivedSize.load() / 1000 << " KB" << endl;
  132. }
  133. dc1->close();
  134. endTime = steady_clock::now();
  135. auto connectDuration = duration_cast<milliseconds>(dc1->isOpen() ? openTime - startTime
  136. : steady_clock::duration(0));
  137. auto transferDuration = duration_cast<milliseconds>(endTime - receivedTime);
  138. cout << "Test duration: " << duration.count() << " ms" << endl;
  139. cout << "Connect duration: " << connectDuration.count() << " ms" << endl;
  140. size_t received = receivedSize.load();
  141. size_t goodput = transferDuration.count() > 0 ? received / transferDuration.count() : 0;
  142. cout << "Goodput: " << goodput * 0.001 << " MB/s"
  143. << " (" << goodput * 0.001 * 8 << " Mbit/s)" << endl;
  144. pc1->close();
  145. pc2->close();
  146. rtc::Cleanup();
  147. this_thread::sleep_for(1s);
  148. return goodput;
  149. }
  150. #ifdef BENCHMARK_MAIN
  151. int main(int argc, char **argv) {
  152. try {
  153. size_t goodput = benchmark(30s);
  154. if (goodput == 0)
  155. throw runtime_error("No data received");
  156. return 0;
  157. } catch (const std::exception &e) {
  158. cerr << "Benchmark failed: " << e.what() << endl;
  159. return -1;
  160. }
  161. }
  162. #endif