PropagationBenchmarkFixture.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #if defined(HAVE_BENCHMARK)
  9. #include <AzToolsFramework/Prefab/PrefabDomUtils.h>
  10. #include <AzToolsFramework/ToolsComponents/EditorInspectorComponent.h>
  11. #include <Prefab/Benchmark/Propagation/PropagationBenchmarkFixture.h>
  12. namespace Benchmark
  13. {
  14. void PropagationBenchmarkFixture::UpdateTemplate()
  15. {
  16. PrefabDom updatedPrefabDom;
  17. PrefabDomUtils::StoreInstanceInPrefabDom(*m_instanceCreated, updatedPrefabDom);
  18. PrefabDom& enclosingTemplatePrefabDom = m_prefabSystemComponent->FindTemplateDom(m_instanceCreated->GetTemplateId());
  19. enclosingTemplatePrefabDom.CopyFrom(updatedPrefabDom, enclosingTemplatePrefabDom.GetAllocator());
  20. m_instanceUpdateExecutorInterface->AddTemplateInstancesToQueue(m_instanceCreated->GetTemplateId(), *m_instanceCreated);
  21. }
  22. void PropagationBenchmarkFixture::UpdateComponent(benchmark::State& state)
  23. {
  24. for ([[maybe_unused]] auto _ : state)
  25. {
  26. float worldX = 0.0f;
  27. AZ::TransformBus::EventResult(worldX, m_entityToModify->GetId(), &AZ::TransformInterface::GetWorldX);
  28. // Move the entity and update the template to capture this transform component change.
  29. AZ::TransformBus::Event(m_entityToModify->GetId(), &AZ::TransformInterface::SetWorldX, worldX + 1);
  30. UpdateTemplate();
  31. m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
  32. }
  33. }
  34. void PropagationBenchmarkFixture::AddComponent(benchmark::State& state)
  35. {
  36. m_entityToModify->Deactivate();
  37. for ([[maybe_unused]] auto _ : state)
  38. {
  39. // Add another component and update the template to capture this change.
  40. AZ::Component* inspectorComponent = m_entityToModify->CreateComponent<AzToolsFramework::Components::EditorInspectorComponent>();
  41. UpdateTemplate();
  42. m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
  43. // Remove the second component added. This will make sure that when multiple iterations are done, we will always be going from
  44. // one components to two components.
  45. m_entityToModify->RemoveComponent(inspectorComponent);
  46. delete inspectorComponent;
  47. inspectorComponent = nullptr;
  48. }
  49. }
  50. void PropagationBenchmarkFixture::RemoveComponent(benchmark::State& state)
  51. {
  52. m_entityToModify->Deactivate();
  53. for ([[maybe_unused]] auto _ : state)
  54. {
  55. AZStd::span<AZ::Component* const> components = m_entityToModify->GetComponents();
  56. AZ::Component* transformComponent = components.front();
  57. m_entityToModify->RemoveComponent(transformComponent);
  58. delete transformComponent;
  59. transformComponent = nullptr;
  60. UpdateTemplate();
  61. m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
  62. // Add the component back. This will make sure that when multiple iterations are done, we will always be going from
  63. // zero components to one component.
  64. m_entityToModify->CreateComponent(AZ::TransformComponentTypeId);
  65. }
  66. }
  67. void PropagationBenchmarkFixture::AddEntity(benchmark::State& state)
  68. {
  69. for ([[maybe_unused]] auto _ : state)
  70. {
  71. // Add an entity and update the template.
  72. AZStd::unique_ptr<AZ::Entity> newEntity = AZStd::make_unique<AZ::Entity>("Added Entity");
  73. AZ::EntityId newEntityId = newEntity->GetId();
  74. m_instanceToModify->AddEntity(AZStd::move(newEntity));
  75. UpdateTemplate();
  76. m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
  77. // Remove the entityadded. This will make sure that when multiple iterations are done, we will always be going from
  78. // 'n' entities to 'n+1' entities.
  79. newEntity = m_instanceToModify->DetachEntity(newEntityId);
  80. }
  81. }
  82. void PropagationBenchmarkFixture::RemoveEntity(benchmark::State& state)
  83. {
  84. for ([[maybe_unused]] auto _ : state)
  85. {
  86. // Remove an entity and update the template.
  87. AZStd::unique_ptr<AZ::Entity> detachedEntity = m_instanceToModify->DetachEntity(m_entityToModify->GetId());
  88. UpdateTemplate();
  89. m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
  90. // Add back the entity removed. This will make sure that when multiple iterations are done, we will always be going from
  91. // 'n' entities to 'n-1' entities.
  92. m_instanceToModify->AddEntity(AZStd::move(detachedEntity));
  93. }
  94. }
  95. void PropagationBenchmarkFixture::AddNestedInstance(benchmark::State& state, TemplateId nestedPrefabTemplateId)
  96. {
  97. for ([[maybe_unused]] auto _ : state)
  98. {
  99. // Add a nested instance and update the template.
  100. AZStd::unique_ptr<Instance> nestedInstance = AZStd::make_unique<Instance>("Added nested instance");
  101. Instance& addedInstance =
  102. m_instanceToModify->AddInstance(AZStd::move(m_prefabSystemComponent->InstantiatePrefab(nestedPrefabTemplateId)));
  103. const InstanceAlias& instanceAlias = addedInstance.GetInstanceAlias();
  104. UpdateTemplate();
  105. m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
  106. // Remove the nested prefab added. This will make sure that when multiple iterations are done, we will always be going from
  107. // 'n' nested prefabs to 'n+1' nested prefabs.
  108. nestedInstance = m_instanceToModify->DetachNestedInstance(instanceAlias);
  109. }
  110. }
  111. void PropagationBenchmarkFixture::RemoveNestedInstance(benchmark::State& state, TemplateId nestedPrefabTemplateId)
  112. {
  113. for ([[maybe_unused]] auto _ : state)
  114. {
  115. AZStd::vector<InstanceAlias> nestedInstanceAliases = m_instanceCreated->GetNestedInstanceAliases(nestedPrefabTemplateId);
  116. AZStd::unique_ptr<Instance> detachedNestedInstance = m_instanceCreated->DetachNestedInstance(nestedInstanceAliases.back());
  117. UpdateTemplate();
  118. m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
  119. // Add back the nested instance removed. This will make sure that when multiple iterations are done, we will always be going
  120. // from 'n' nested instances to 'n-1' nested instances.
  121. m_instanceCreated->AddInstance(AZStd::move(detachedNestedInstance));
  122. }
  123. // After the last iteration, the template should be updated to avoid link deletion failures.
  124. UpdateTemplate();
  125. }
  126. } // namespace Benchmark
  127. #endif