2
0

benchmark.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * Copyright (c) 2019 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #include "rtc/rtc.hpp"
  9. #include <atomic>
  10. #include <chrono>
  11. #include <iostream>
  12. #include <memory>
  13. #include <thread>
  14. using namespace rtc;
  15. using namespace std;
  16. using namespace chrono_literals;
  17. using chrono::duration_cast;
  18. using chrono::milliseconds;
  19. using chrono::steady_clock;
  20. template <class T> weak_ptr<T> make_weak_ptr(shared_ptr<T> ptr) { return ptr; }
  21. size_t benchmark(milliseconds duration) {
  22. rtc::InitLogger(LogLevel::Warning);
  23. rtc::Preload();
  24. Configuration config1;
  25. // config1.iceServers.emplace_back("stun:stun.l.google.com:19302");
  26. // config1.mtu = 1500;
  27. PeerConnection pc1(config1);
  28. Configuration config2;
  29. // config2.iceServers.emplace_back("stun:stun.l.google.com:19302");
  30. // config2.mtu = 1500;
  31. PeerConnection pc2(config2);
  32. pc1.onLocalDescription([&pc2](Description sdp) {
  33. cout << "Description 1: " << sdp << endl;
  34. pc2.setRemoteDescription(std::move(sdp));
  35. });
  36. pc1.onLocalCandidate([&pc2](Candidate candidate) {
  37. cout << "Candidate 1: " << candidate << endl;
  38. pc2.addRemoteCandidate(std::move(candidate));
  39. });
  40. pc1.onStateChange([](PeerConnection::State state) { cout << "State 1: " << state << endl; });
  41. pc1.onGatheringStateChange([](PeerConnection::GatheringState state) {
  42. cout << "Gathering state 1: " << state << endl;
  43. });
  44. pc2.onLocalDescription([&pc1](Description sdp) {
  45. cout << "Description 2: " << sdp << endl;
  46. pc1.setRemoteDescription(std::move(sdp));
  47. });
  48. pc2.onLocalCandidate([&pc1](Candidate candidate) {
  49. cout << "Candidate 2: " << candidate << endl;
  50. pc1.addRemoteCandidate(std::move(candidate));
  51. });
  52. pc2.onStateChange([](PeerConnection::State state) { cout << "State 2: " << state << endl; });
  53. pc2.onGatheringStateChange([](PeerConnection::GatheringState state) {
  54. cout << "Gathering state 2: " << state << endl;
  55. });
  56. const size_t messageSize = 65535;
  57. binary messageData(messageSize);
  58. fill(messageData.begin(), messageData.end(), byte(0xFF));
  59. atomic<size_t> receivedSize = 0;
  60. steady_clock::time_point startTime, openTime, receivedTime, endTime;
  61. shared_ptr<DataChannel> dc2;
  62. pc2.onDataChannel([&dc2, &receivedSize, &receivedTime](shared_ptr<DataChannel> dc) {
  63. dc->onMessage([&receivedTime, &receivedSize](variant<binary, string> message) {
  64. if (holds_alternative<binary>(message)) {
  65. const auto &bin = get<binary>(message);
  66. if (receivedSize == 0)
  67. receivedTime = steady_clock::now();
  68. receivedSize += bin.size();
  69. }
  70. });
  71. dc->onClosed([]() { cout << "DataChannel closed." << endl; });
  72. std::atomic_store(&dc2, dc);
  73. });
  74. startTime = steady_clock::now();
  75. auto dc1 = pc1.createDataChannel("benchmark");
  76. dc1->onOpen([wdc1 = make_weak_ptr(dc1), &messageData, &openTime]() {
  77. auto dc1 = wdc1.lock();
  78. if (!dc1)
  79. return;
  80. openTime = steady_clock::now();
  81. cout << "DataChannel open, sending data..." << endl;
  82. try {
  83. while (dc1->bufferedAmount() == 0) {
  84. dc1->send(messageData);
  85. }
  86. } catch (const std::exception &e) {
  87. std::cout << "Send failed: " << e.what() << std::endl;
  88. }
  89. });
  90. // When sent data is buffered in the DataChannel,
  91. // wait for onBufferedAmountLow callback to continue
  92. dc1->onBufferedAmountLow([wdc1 = make_weak_ptr(dc1), &messageData]() {
  93. auto dc1 = wdc1.lock();
  94. if (!dc1)
  95. return;
  96. // Continue sending
  97. try {
  98. while (dc1->isOpen() && dc1->bufferedAmount() == 0) {
  99. dc1->send(messageData);
  100. }
  101. } catch (const std::exception &e) {
  102. std::cout << "Send failed: " << e.what() << std::endl;
  103. }
  104. });
  105. const int steps = 10;
  106. const auto stepDuration = duration / 10;
  107. for (int i = 0; i < steps; ++i) {
  108. this_thread::sleep_for(stepDuration);
  109. cout << "Received: " << receivedSize.load() / 1000 << " KB" << endl;
  110. }
  111. dc1->close();
  112. endTime = steady_clock::now();
  113. auto connectDuration = duration_cast<milliseconds>(dc1->isOpen() ? openTime - startTime
  114. : steady_clock::duration(0));
  115. auto transferDuration = duration_cast<milliseconds>(endTime - receivedTime);
  116. cout << "Test duration: " << duration.count() << " ms" << endl;
  117. cout << "Connect duration: " << connectDuration.count() << " ms" << endl;
  118. size_t received = receivedSize.load();
  119. size_t goodput = transferDuration.count() > 0 ? received / transferDuration.count() : 0;
  120. cout << "Goodput: " << goodput * 0.001 << " MB/s"
  121. << " (" << goodput * 0.001 * 8 << " Mbit/s)" << endl;
  122. pc1.close();
  123. pc2.close();
  124. rtc::Cleanup();
  125. return goodput;
  126. }
  127. #ifdef BENCHMARK_MAIN
  128. int main(int argc, char **argv) {
  129. try {
  130. size_t goodput = benchmark(30s);
  131. if (goodput == 0)
  132. throw runtime_error("No data received");
  133. return 0;
  134. } catch (const std::exception &e) {
  135. cerr << "Benchmark failed: " << e.what() << endl;
  136. return -1;
  137. }
  138. }
  139. #endif