MultiplayerDebugByteReporter.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "MultiplayerDebugByteReporter.h"
  9. #include <AzCore/Casting/numeric_cast.h>
  10. #include <iomanip> // for std::setfill
  11. #include <sstream>
  12. #include <AzCore/std/sort.h>
  13. namespace Multiplayer
  14. {
  15. MultiplayerDebugByteReporter::MultiplayerDebugByteReporter()
  16. {
  17. MultiplayerDebugByteReporter::Reset();
  18. }
  19. void MultiplayerDebugByteReporter::ReportBytes(size_t byteSize)
  20. {
  21. m_count++;
  22. m_totalBytes += byteSize;
  23. m_totalBytesThisSecond += byteSize;
  24. m_minBytes = AZStd::min(m_minBytes, byteSize);
  25. m_maxBytes = AZStd::max(m_maxBytes, byteSize);
  26. }
  27. void MultiplayerDebugByteReporter::AggregateBytes(size_t byteSize)
  28. {
  29. m_aggregateBytes += byteSize;
  30. }
  31. void MultiplayerDebugByteReporter::ReportAggregateBytes()
  32. {
  33. ReportBytes(m_aggregateBytes);
  34. m_aggregateBytes = 0;
  35. }
  36. float MultiplayerDebugByteReporter::GetAverageBytes() const
  37. {
  38. if (m_count == 0)
  39. {
  40. return 0.0f;
  41. }
  42. return aznumeric_cast<float>(m_totalBytes) / aznumeric_cast<float>(m_count);
  43. }
  44. size_t MultiplayerDebugByteReporter::GetMaxBytes() const
  45. {
  46. return m_maxBytes;
  47. }
  48. size_t MultiplayerDebugByteReporter::GetMinBytes() const
  49. {
  50. return m_minBytes;
  51. }
  52. size_t MultiplayerDebugByteReporter::GetTotalBytes() const
  53. {
  54. return m_totalBytes;
  55. }
  56. float MultiplayerDebugByteReporter::GetKbitsPerSecond()
  57. {
  58. const auto now = AZStd::chrono::steady_clock::now();
  59. // Check the amount of time elapsed and update totals if necessary.
  60. // Time here is measured in whole seconds from the epoch, providing synchronization in
  61. // reporting intervals across all byte reporters.
  62. const AZStd::chrono::seconds nowSeconds = AZStd::chrono::duration_cast<AZStd::chrono::seconds>(now.time_since_epoch());
  63. const AZStd::chrono::seconds secondsSinceLastUpdate = nowSeconds -
  64. AZStd::chrono::duration_cast<AZStd::chrono::seconds>(m_lastUpdateTime.time_since_epoch());
  65. if (secondsSinceLastUpdate.count())
  66. {
  67. // normalize over elapsed milliseconds
  68. constexpr int k_millisecondsPerSecond = 1000;
  69. const auto msSinceLastUpdate = AZStd::chrono::duration_cast<AZStd::chrono::milliseconds>(now - m_lastUpdateTime);
  70. m_totalBytesLastSecond = k_millisecondsPerSecond * aznumeric_cast<float>(m_totalBytesThisSecond) / aznumeric_cast<float>(msSinceLastUpdate.count());
  71. m_totalBytesThisSecond = 0;
  72. m_lastUpdateTime = now;
  73. }
  74. constexpr float bitsPerByte = 8.0f;
  75. constexpr int bitsPerKilobit = 1024;
  76. return bitsPerByte * m_totalBytesLastSecond / bitsPerKilobit;
  77. }
  78. void MultiplayerDebugByteReporter::Combine(const MultiplayerDebugByteReporter& other)
  79. {
  80. m_count += other.m_count;
  81. m_totalBytes += other.m_totalBytes;
  82. m_totalBytesThisSecond += other.m_totalBytesThisSecond;
  83. m_minBytes = AZStd::GetMin(m_minBytes, other.m_minBytes);
  84. m_maxBytes = AZStd::GetMax(m_maxBytes, other.m_maxBytes);
  85. }
  86. void MultiplayerDebugByteReporter::Reset()
  87. {
  88. m_count = 0;
  89. m_totalBytes = 0;
  90. m_totalBytesThisSecond = 0;
  91. m_totalBytesLastSecond = 0;
  92. m_minBytes = std::numeric_limits<decltype(m_minBytes)>::max();
  93. m_maxBytes = 0;
  94. m_aggregateBytes = 0;
  95. }
  96. void MultiplayerDebugComponentReporter::ReportField(const char* fieldName, size_t byteSize)
  97. {
  98. MultiplayerDebugByteReporter::AggregateBytes(byteSize);
  99. m_fieldReports[fieldName].ReportBytes(byteSize);
  100. }
  101. void MultiplayerDebugComponentReporter::ReportFragmentEnd()
  102. {
  103. MultiplayerDebugByteReporter::ReportAggregateBytes();
  104. m_componentDirtyBytes.ReportAggregateBytes();
  105. }
  106. AZStd::vector<MultiplayerDebugComponentReporter::Report> MultiplayerDebugComponentReporter::GetFieldReports()
  107. {
  108. AZStd::vector<Report> copy;
  109. for (auto field = m_fieldReports.begin(); field != m_fieldReports.end(); ++field)
  110. {
  111. copy.emplace_back(field->first, &field->second);
  112. }
  113. auto sortByFrequency = [](const Report& a, const Report& b)
  114. {
  115. return a.second->GetTotalCount() > b.second->GetTotalCount();
  116. };
  117. AZStd::sort(copy.begin(), copy.end(), sortByFrequency);
  118. return copy;
  119. }
  120. void MultiplayerDebugComponentReporter::Combine(const MultiplayerDebugComponentReporter& other)
  121. {
  122. MultiplayerDebugByteReporter::Combine(other);
  123. for (const auto& fieldIterator : other.m_fieldReports)
  124. {
  125. m_fieldReports[fieldIterator.first].Combine(fieldIterator.second);
  126. }
  127. m_componentDirtyBytes.Combine(other.m_componentDirtyBytes);
  128. }
  129. void MultiplayerDebugEntityReporter::ReportField(AZ::u32 index, const char* componentName,
  130. const char* fieldName, size_t byteSize)
  131. {
  132. if (m_currentComponentReport == nullptr)
  133. {
  134. std::stringstream component;
  135. component << "[" << std::setw(2) << std::setfill('0') << aznumeric_cast<int>(index) << "]" << " " << componentName;
  136. m_currentComponentReport = &m_componentReports[component.str().c_str()];
  137. }
  138. m_currentComponentReport->ReportField(fieldName, byteSize);
  139. MultiplayerDebugByteReporter::AggregateBytes(byteSize);
  140. }
  141. void MultiplayerDebugEntityReporter::ReportFragmentEnd()
  142. {
  143. if (m_currentComponentReport)
  144. {
  145. m_currentComponentReport->ReportFragmentEnd();
  146. m_currentComponentReport = nullptr;
  147. }
  148. MultiplayerDebugByteReporter::ReportAggregateBytes();
  149. }
  150. void MultiplayerDebugEntityReporter::Combine(const MultiplayerDebugEntityReporter& other)
  151. {
  152. MultiplayerDebugByteReporter::Combine(other);
  153. for (const auto& componentIterator : other.m_componentReports)
  154. {
  155. m_componentReports[componentIterator.first].Combine(componentIterator.second);
  156. }
  157. SetEntityName(other.GetEntityName());
  158. }
  159. void MultiplayerDebugEntityReporter::Reset()
  160. {
  161. MultiplayerDebugByteReporter::Reset();
  162. m_componentReports.clear();
  163. }
  164. AZStd::map<AZStd::string, MultiplayerDebugComponentReporter>& MultiplayerDebugEntityReporter::GetComponentReports()
  165. {
  166. return m_componentReports;
  167. }
  168. }