MultiplayerDebugByteReporter.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #pragma once
  9. #include <AzCore/std/string/string.h>
  10. #include <AzCore/std/chrono/chrono.h>
  11. #include <AzCore/std/containers/map.h>
  12. #include <AzCore/std/containers/vector.h>
  13. namespace Multiplayer
  14. {
  15. class MultiplayerDebugByteReporter
  16. {
  17. public:
  18. MultiplayerDebugByteReporter();
  19. virtual ~MultiplayerDebugByteReporter() = default;
  20. void ReportBytes(size_t byteSize);
  21. void AggregateBytes(size_t byteSize);
  22. void ReportAggregateBytes();
  23. float GetAverageBytes() const;
  24. size_t GetMaxBytes() const;
  25. size_t GetMinBytes() const;
  26. size_t GetTotalBytes() const;
  27. float GetKbitsPerSecond();
  28. void Combine(const MultiplayerDebugByteReporter& other);
  29. virtual void Reset();
  30. size_t GetTotalCount() const { return m_count; }
  31. private:
  32. size_t m_count;
  33. size_t m_totalBytes;
  34. size_t m_totalBytesThisSecond;
  35. float m_totalBytesLastSecond;
  36. size_t m_minBytes;
  37. size_t m_maxBytes;
  38. size_t m_aggregateBytes;
  39. AZStd::chrono::steady_clock::time_point m_lastUpdateTime;
  40. };
  41. class MultiplayerDebugComponentReporter final
  42. : public MultiplayerDebugByteReporter
  43. {
  44. public:
  45. MultiplayerDebugComponentReporter() = default;
  46. void ReportField(const char* fieldName, size_t byteSize);
  47. void ReportFragmentEnd();
  48. using Report = AZStd::pair<AZStd::string, MultiplayerDebugByteReporter*>;
  49. AZStd::vector<Report> GetFieldReports();
  50. void Combine(const MultiplayerDebugComponentReporter& other);
  51. private:
  52. AZStd::map<AZStd::string, MultiplayerDebugByteReporter> m_fieldReports;
  53. MultiplayerDebugByteReporter m_componentDirtyBytes;
  54. };
  55. class MultiplayerDebugEntityReporter final
  56. : public MultiplayerDebugByteReporter
  57. {
  58. public:
  59. MultiplayerDebugEntityReporter() = default;
  60. void ReportField(AZ::u32 index, const char* componentName, const char* fieldName, size_t byteSize);
  61. void ReportFragmentEnd();
  62. void Combine(const MultiplayerDebugEntityReporter& other);
  63. void Reset() override;
  64. const char* GetEntityName() const { return m_entityName.c_str(); }
  65. void SetEntityName(const char* entityName)
  66. {
  67. // copying because the entity might go away
  68. m_entityName = entityName;
  69. }
  70. AZStd::map<AZStd::string, MultiplayerDebugComponentReporter>& GetComponentReports();
  71. private:
  72. MultiplayerDebugComponentReporter* m_currentComponentReport = nullptr;
  73. AZStd::map<AZStd::string, MultiplayerDebugComponentReporter> m_componentReports;
  74. AZStd::string m_entityName;
  75. };
  76. }