PrefabBenchmarkFixture.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <Prefab/Benchmark/PrefabBenchmarkFixture.h>
  10. #include <AzCore/Component/TransformBus.h> //for create entity
  11. #include <Prefab/PrefabTestDomUtils.h>
  12. #include <Prefab/PrefabTestDataUtils.h>
  13. namespace Benchmark
  14. {
  15. void BM_Prefab::SetupPrefabSystem()
  16. {
  17. m_app = AZStd::make_unique<AzToolsFramework::ToolsApplication>();
  18. ASSERT_TRUE(m_app != nullptr);
  19. AZ::ComponentApplication::StartupParameters startupParameters;
  20. startupParameters.m_loadSettingsRegistry = false;
  21. m_app->Start(AzFramework::Application::Descriptor(), startupParameters);
  22. AZ::Entity* systemEntity = m_app->FindEntity(AZ::SystemEntityId);
  23. ASSERT_TRUE(systemEntity != nullptr);
  24. m_prefabSystemComponent = systemEntity->FindComponent<AzToolsFramework::Prefab::PrefabSystemComponent>();
  25. ASSERT_TRUE(m_prefabSystemComponent != nullptr);
  26. m_mockIOActionValidator = AZStd::make_unique<UnitTest::MockPrefabFileIOActionValidator>();
  27. ASSERT_TRUE(m_mockIOActionValidator != nullptr);
  28. m_instanceUpdateExecutorInterface = AZ::Interface<AzToolsFramework::Prefab::InstanceUpdateExecutorInterface>::Get();
  29. ASSERT_TRUE(m_instanceUpdateExecutorInterface != nullptr);
  30. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  31. }
  32. void BM_Prefab::TearDownPrefabSystem()
  33. {
  34. m_mockIOActionValidator.reset();
  35. m_app.reset();
  36. }
  37. void BM_Prefab::ResetPrefabSystem()
  38. {
  39. TearDownPrefabSystem();
  40. SetupPrefabSystem();
  41. }
  42. void BM_Prefab::SetupHarness(const benchmark::State& state)
  43. {
  44. AZ::Debug::TraceMessageBus::Handler::BusConnect();
  45. UnitTest::AllocatorsBenchmarkFixture::SetUp(state);
  46. SetupPrefabSystem();
  47. }
  48. void BM_Prefab::TeardownHarness(const benchmark::State& state)
  49. {
  50. m_paths = {};
  51. TearDownPrefabSystem();
  52. UnitTest::AllocatorsBenchmarkFixture::TearDown(state);
  53. AZ::Debug::TraceMessageBus::Handler::BusDisconnect();
  54. }
  55. AZ::Entity* BM_Prefab::CreateEntity(const char* entityName, const AZ::EntityId& parentId)
  56. {
  57. // Circumvent the EntityContext system and generate a new entity with a transformcomponent
  58. AZ::Entity* newEntity = aznew AZ::Entity(entityName);
  59. newEntity->CreateComponent(AZ::TransformComponentTypeId);
  60. newEntity->Init();
  61. newEntity->Activate();
  62. SetEntityParent(newEntity->GetId(), parentId);
  63. return newEntity;
  64. }
  65. void BM_Prefab::CreateEntities(const unsigned int entityCount, AZStd::vector<AZ::Entity*>& entities)
  66. {
  67. for (unsigned int entityIndex = 0; entityIndex < entityCount; ++entityIndex)
  68. {
  69. AZStd::string entityName = "TestEntity";
  70. entityName = entityName + AZStd::to_string(entityIndex);
  71. entities.emplace_back(CreateEntity(entityName.c_str()));
  72. }
  73. }
  74. void BM_Prefab::SetEntityParent(const AZ::EntityId& entityId, const AZ::EntityId& parentId)
  75. {
  76. AZ::TransformBus::Event(entityId, &AZ::TransformBus::Events::SetParent, parentId);
  77. }
  78. void BM_Prefab::CreateFakePaths(const unsigned int pathCount)
  79. {
  80. //setup fake paths
  81. for (unsigned int number = 0; number < pathCount; ++number)
  82. {
  83. AZStd::string path = m_pathString;
  84. m_paths.push_back(path + AZStd::to_string(number) + "_" + AZStd::to_string(pathCount));
  85. }
  86. }
  87. void BM_Prefab::SetUpMockValidatorForReadPrefab()
  88. {
  89. const size_t pathCount = m_paths.size();
  90. for (size_t number = 0; number < pathCount; ++number)
  91. {
  92. m_mockIOActionValidator->ReadPrefabDom(
  93. m_paths[number], UnitTest::PrefabTestDomUtils::CreatePrefabDom());
  94. }
  95. }
  96. void BM_Prefab::DeleteInstances(const AzToolsFramework::Prefab::InstanceList& instancesToDelete)
  97. {
  98. for (AzToolsFramework::Prefab::Instance* instanceToDelete : instancesToDelete)
  99. {
  100. ASSERT_TRUE(instanceToDelete);
  101. delete instanceToDelete;
  102. instanceToDelete = nullptr;
  103. }
  104. }
  105. }
  106. #endif