ServerHierarchyBenchmarks.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #ifdef HAVE_BENCHMARK
  9. #include <CommonBenchmarkSetup.h>
  10. #include <AzCore/std/smart_ptr/make_shared.h>
  11. namespace Multiplayer
  12. {
  13. /*
  14. * Hierarchy of 16 entities: Parent -> Child_2 -> .. -> Child_16
  15. * By default the maximum size of a hierarchy is defined by bg_hierarchyEntityMaxLimit (16).
  16. */
  17. class ServerDeepHierarchyBenchmark : public HierarchyBenchmarkBase
  18. {
  19. public:
  20. const NetEntityId RootNetEntityId = NetEntityId{ 1 };
  21. const NetEntityId ChildNetEntityId = NetEntityId{ 2 };
  22. const NetEntityId ChildOfChildNetEntityId = NetEntityId{ 3 };
  23. void internalSetUp() override
  24. {
  25. HierarchyBenchmarkBase::internalSetUp();
  26. m_root = AZStd::make_unique<EntityInfo>((1), "root", RootNetEntityId, EntityInfo::Role::Root);
  27. CreateParent(*m_root);
  28. m_children = AZStd::make_unique<AZStd::vector<AZStd::shared_ptr<EntityInfo>>>();
  29. EntityInfo* parent = m_root.get();
  30. for (int i = 0; i < 15; ++i)
  31. {
  32. m_children->push_back(AZStd::make_shared<EntityInfo>((i + 2), "child", ChildNetEntityId, EntityInfo::Role::Child));
  33. CreateChildForParent(*m_children->back(), *parent);
  34. m_children->back()->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(parent->m_entity->GetId());
  35. parent = m_children->back().get();
  36. }
  37. }
  38. void internalTearDown() override
  39. {
  40. m_children.reset();
  41. m_root.reset();
  42. HierarchyBenchmarkBase::internalTearDown();
  43. }
  44. AZStd::unique_ptr<EntityInfo> m_root;
  45. AZStd::unique_ptr<AZStd::vector<AZStd::shared_ptr<EntityInfo>>> m_children;
  46. };
  47. BENCHMARK_DEFINE_F(ServerDeepHierarchyBenchmark, RebuildHierarchy)(benchmark::State& state)
  48. {
  49. for ([[maybe_unused]] auto value : state)
  50. {
  51. ForceRebuildHierarchy(m_root->m_entity);
  52. }
  53. }
  54. BENCHMARK_REGISTER_F(ServerDeepHierarchyBenchmark, RebuildHierarchy)
  55. ->Unit(benchmark::kMicrosecond)
  56. ;
  57. // Should be roughly twice the time of @RebuildHierarchy benchmark
  58. BENCHMARK_DEFINE_F(ServerDeepHierarchyBenchmark, RebuildHierarchyRemoveAndAddLastChild)(benchmark::State& state)
  59. {
  60. const AZ::EntityId parentOfLastChild = m_children->at(m_children->size() - 2)->m_entity->GetId();
  61. for ([[maybe_unused]] auto value : state)
  62. {
  63. m_children->back()->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
  64. m_children->back()->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(parentOfLastChild);
  65. }
  66. }
  67. BENCHMARK_REGISTER_F(ServerDeepHierarchyBenchmark, RebuildHierarchyRemoveAndAddLastChild)
  68. ->Unit(benchmark::kMicrosecond)
  69. ;
  70. // Should be roughly twice the time of @RebuildHierarchy benchmark
  71. BENCHMARK_DEFINE_F(ServerDeepHierarchyBenchmark, RebuildHierarchyRemoveAndAddMiddleChild)(benchmark::State& state)
  72. {
  73. const AZ::EntityId parentOfMiddleChild = m_children->at(4)->m_entity->GetId();
  74. for ([[maybe_unused]] auto value : state)
  75. {
  76. m_children->at(5)->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
  77. m_children->at(5)->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(parentOfMiddleChild);
  78. }
  79. }
  80. BENCHMARK_REGISTER_F(ServerDeepHierarchyBenchmark, RebuildHierarchyRemoveAndAddMiddleChild)
  81. ->Unit(benchmark::kMicrosecond)
  82. ;
  83. // Should be roughly twice the time of @RebuildHierarchy benchmark
  84. BENCHMARK_DEFINE_F(ServerDeepHierarchyBenchmark, RebuildHierarchyRemoveAndAddFirstChild)(benchmark::State& state)
  85. {
  86. const AZ::EntityId rootId = m_children->front()->m_entity->GetId();
  87. for ([[maybe_unused]] auto value : state)
  88. {
  89. m_children->at(1)->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(AZ::EntityId());
  90. m_children->at(1)->m_entity->FindComponent<AzFramework::TransformComponent>()->SetParent(rootId);
  91. }
  92. }
  93. BENCHMARK_REGISTER_F(ServerDeepHierarchyBenchmark, RebuildHierarchyRemoveAndAddFirstChild)
  94. ->Unit(benchmark::kMicrosecond)
  95. ;
  96. }
  97. #endif