benchmark.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. PeerConnection pc1(config1);
  38. Configuration config2;
  39. // config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
  40. // config2.mtu = 1500;
  41. PeerConnection pc2(config2);
  42. pc1.onLocalDescription([&pc2](Description sdp) {
  43. cout << "Description 1: " << sdp << endl;
  44. pc2.setRemoteDescription(std::move(sdp));
  45. });
  46. pc1.onLocalCandidate([&pc2](Candidate candidate) {
  47. cout << "Candidate 1: " << candidate << endl;
  48. pc2.addRemoteCandidate(std::move(candidate));
  49. });
  50. pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  51. pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
  52. cout << "Gathering state 1: " << state << endl;
  53. });
  54. pc2.onLocalDescription([&pc1](Description sdp) {
  55. cout << "Description 2: " << sdp << endl;
  56. pc1.setRemoteDescription(std::move(sdp));
  57. });
  58. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  59. cout << "Candidate 2: " << candidate << endl;
  60. pc1.addRemoteCandidate(std::move(candidate));
  61. });
  62. pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  63. pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
  64. cout << "Gathering state 2: " << state << endl;
  65. });
  66. const size_t messageSize = 65535;
  67. binary messageData(messageSize);
  68. fill(messageData.begin(), messageData.end(), byte(0xFF));
  69. atomic<size_t> receivedSize = 0;
  70. steady_clock::time_point startTime, openTime, receivedTime, endTime;
  71. shared_ptr<DataChannel> dc2;
  72. pc2.onDataChannel([&dc2, &receivedSize, &receivedTime](shared_ptr<DataChannel> dc) {
  73. dc->onMessage([&receivedTime, &receivedSize](variant<binary, string> message) {
  74. if (holds_alternative<binary>(message)) {
  75. const auto &bin = get<binary>(message);
  76. if (receivedSize == 0)
  77. receivedTime = steady_clock::now();
  78. receivedSize += bin.size();
  79. }
  80. });
  81. dc->onClosed([]() { cout << "DataChannel closed." << endl; });
  82. std::atomic_store(&dc2, dc);
  83. });
  84. startTime = steady_clock::now();
  85. auto dc1 = pc1.createDataChannel("benchmark");
  86. dc1->onOpen([wdc1 = make_weak_ptr(dc1), &messageData, &openTime]() {
  87. auto dc1 = wdc1.lock();
  88. if (!dc1)
  89. return;
  90. openTime = steady_clock::now();
  91. cout << "DataChannel open, sending data..." << endl;
  92. try {
  93. while (dc1->bufferedAmount() == 0) {
  94. dc1->send(messageData);
  95. }
  96. } catch (const std::exception &e) {
  97. std::cout << "Send failed: " << e.what() << std::endl;
  98. }
  99. });
  100. // When sent data is buffered in the DataChannel,
  101. // wait for onBufferedAmountLow callback to continue
  102. dc1->onBufferedAmountLow([wdc1 = make_weak_ptr(dc1), &messageData]() {
  103. auto dc1 = wdc1.lock();
  104. if (!dc1)
  105. return;
  106. // Continue sending
  107. try {
  108. while (dc1->isOpen() && dc1->bufferedAmount() == 0) {
  109. dc1->send(messageData);
  110. }
  111. } catch (const std::exception &e) {
  112. std::cout << "Send failed: " << e.what() << std::endl;
  113. }
  114. });
  115. const int steps = 10;
  116. const auto stepDuration = duration / 10;
  117. for (int i = 0; i < steps; ++i) {
  118. this_thread::sleep_for(stepDuration);
  119. cout << "Received: " << receivedSize.load() / 1000 << " KB" << endl;
  120. }
  121. dc1->close();
  122. endTime = steady_clock::now();
  123. auto connectDuration = duration_cast<milliseconds>(dc1->isOpen() ? openTime - startTime
  124. : steady_clock::duration(0));
  125. auto transferDuration = duration_cast<milliseconds>(endTime - receivedTime);
  126. cout << "Test duration: " << duration.count() << " ms" << endl;
  127. cout << "Connect duration: " << connectDuration.count() << " ms" << endl;
  128. size_t received = receivedSize.load();
  129. size_t goodput = transferDuration.count() > 0 ? received / transferDuration.count() : 0;
  130. cout << "Goodput: " << goodput * 0.001 << " MB/s"
  131. << " (" << goodput * 0.001 * 8 << " Mbit/s)" << endl;
  132. pc1.close();
  133. pc2.close();
  134. rtc::Cleanup();
  135. return goodput;
  136. }
  137. #ifdef BENCHMARK_MAIN
  138. int main(int argc, char **argv) {
  139. try {
  140. size_t goodput = benchmark(30s);
  141. if (goodput == 0)
  142. throw runtime_error("No data received");
  143. return 0;
  144. } catch (const std::exception &e) {
  145. cerr << "Benchmark failed: " << e.what() << endl;
  146. return -1;
  147. }
  148. }
  149. #endif