benchmark.cpp 5.2 KB

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