PrefabUpdateInstancesBenchmarks.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <AzToolsFramework/Prefab/PrefabDomUtils.h>
  11. namespace Benchmark
  12. {
  13. using BM_PrefabUpdateInstances = BM_Prefab;
  14. using namespace AzToolsFramework::Prefab;
  15. BENCHMARK_DEFINE_F(BM_PrefabUpdateInstances, UpdateInstances_SingeEntityInstances)(::benchmark::State& state)
  16. {
  17. const unsigned int numInstances = static_cast<unsigned int>(state.range());
  18. CreateFakePaths(2);
  19. const auto& nestedTemplatePath = m_paths.front();
  20. const auto& enclosingTemplatePath = m_paths.back();
  21. for ([[maybe_unused]] auto _ : state)
  22. {
  23. state.PauseTiming();
  24. AZ::Entity* entity = CreateEntity("Entity");
  25. AZStd::unique_ptr<Instance> nestedInstance = m_prefabSystemComponent->CreatePrefab(
  26. { entity },
  27. {},
  28. nestedTemplatePath);
  29. AZStd::unique_ptr<Instance> enclosingInstance = m_prefabSystemComponent->CreatePrefab(
  30. {},
  31. MakeInstanceList(AZStd::move(nestedInstance)),
  32. enclosingTemplatePath);
  33. TemplateId templateToInstantiateId = enclosingInstance->GetTemplateId();
  34. {
  35. AZStd::vector<AZStd::unique_ptr<Instance>> newInstances;
  36. newInstances.resize(numInstances);
  37. for (unsigned int instanceCounter = 0; instanceCounter < numInstances; ++instanceCounter)
  38. {
  39. newInstances[instanceCounter] = m_prefabSystemComponent->InstantiatePrefab(templateToInstantiateId);
  40. }
  41. entity->SetName("Updated Entity");
  42. PrefabDom updatedPrefabDom;
  43. PrefabDomUtils::StoreInstanceInPrefabDom(*enclosingInstance, updatedPrefabDom);
  44. PrefabDom& enclosingTemplatePrefabDom = m_prefabSystemComponent->FindTemplateDom(templateToInstantiateId);
  45. enclosingTemplatePrefabDom.CopyFrom(updatedPrefabDom, enclosingTemplatePrefabDom.GetAllocator());
  46. state.ResumeTiming();
  47. m_instanceUpdateExecutorInterface->AddTemplateInstancesToQueue(templateToInstantiateId);
  48. m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
  49. state.PauseTiming();
  50. }
  51. enclosingInstance.reset();
  52. ResetPrefabSystem();
  53. state.ResumeTiming();
  54. }
  55. state.SetComplexityN(numInstances);
  56. }
  57. BENCHMARK_REGISTER_F(BM_PrefabUpdateInstances, UpdateInstances_SingeEntityInstances)
  58. ->RangeMultiplier(10)
  59. ->Range(100, 10000)
  60. ->Unit(benchmark::kMillisecond)
  61. ->Complexity();
  62. BENCHMARK_DEFINE_F(BM_PrefabUpdateInstances, UpdateInstances_SingleLinearNestingOfInstances)(::benchmark::State& state)
  63. {
  64. const unsigned int maxDepth = static_cast<unsigned int>(state.range());
  65. CreateFakePaths(maxDepth);
  66. const unsigned int numInstances = maxDepth;
  67. for ([[maybe_unused]] auto _ : state)
  68. {
  69. state.PauseTiming();
  70. AZ::Entity* entity = CreateEntity("Entity");
  71. AZStd::unique_ptr<Instance> currentInstanceRoot = m_prefabSystemComponent->CreatePrefab(
  72. { entity },
  73. {},
  74. m_paths.back());
  75. for (unsigned int currentDepth = 1; currentDepth < maxDepth; ++currentDepth)
  76. {
  77. currentInstanceRoot = m_prefabSystemComponent->CreatePrefab(
  78. {},
  79. MakeInstanceList(AZStd::move(currentInstanceRoot)),
  80. m_paths[currentDepth - 1]);
  81. }
  82. entity->SetName("Updated Entity");
  83. PrefabDom updatedPrefabDom;
  84. PrefabDomUtils::StoreInstanceInPrefabDom(*currentInstanceRoot, updatedPrefabDom);
  85. const TemplateId rootTemplateId = currentInstanceRoot->GetTemplateId();
  86. PrefabDom& rootTemplatePrefabDom = m_prefabSystemComponent->FindTemplateDom(rootTemplateId);
  87. rootTemplatePrefabDom.CopyFrom(updatedPrefabDom, rootTemplatePrefabDom.GetAllocator());
  88. state.ResumeTiming();
  89. m_instanceUpdateExecutorInterface->AddTemplateInstancesToQueue(rootTemplateId);
  90. m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
  91. state.PauseTiming();
  92. currentInstanceRoot.reset();
  93. ResetPrefabSystem();
  94. state.ResumeTiming();
  95. }
  96. state.SetComplexityN(numInstances);
  97. }
  98. BENCHMARK_DEFINE_F(BM_PrefabUpdateInstances, UpdateInstances_MultipleLinearNestingOfInstances)(::benchmark::State& state)
  99. {
  100. const unsigned int numRootInstances = static_cast<unsigned int>(state.range());
  101. const unsigned int maxDepth = static_cast<unsigned int>(state.range());
  102. CreateFakePaths(maxDepth);
  103. const unsigned int numInstances = numRootInstances * maxDepth;
  104. for ([[maybe_unused]] auto _ : state)
  105. {
  106. state.PauseTiming();
  107. AZ::Entity* entity = CreateEntity("Entity");
  108. AZStd::unique_ptr<Instance> currentInstanceRoot = m_prefabSystemComponent->CreatePrefab(
  109. { entity },
  110. {},
  111. m_paths.back());
  112. for (unsigned int currentDepth = 0; currentDepth < maxDepth - 1; ++currentDepth)
  113. {
  114. currentInstanceRoot = m_prefabSystemComponent->CreatePrefab(
  115. {},
  116. MakeInstanceList(AZStd::move(currentInstanceRoot)),
  117. m_paths[currentDepth]);
  118. }
  119. const TemplateId rootTemplateId = currentInstanceRoot->GetTemplateId();
  120. {
  121. AZStd::vector<AZStd::unique_ptr<Instance>> newInstances;
  122. newInstances.resize(numRootInstances - 1);
  123. for (unsigned int instanceCounter = 0; instanceCounter < numRootInstances - 1; ++instanceCounter)
  124. {
  125. newInstances[instanceCounter] = m_prefabSystemComponent->InstantiatePrefab(rootTemplateId);
  126. }
  127. entity->SetName("Updated Entity");
  128. PrefabDom updatedPrefabDom;
  129. PrefabDomUtils::StoreInstanceInPrefabDom(*currentInstanceRoot, updatedPrefabDom);
  130. PrefabDom& rootTemplatePrefabDom = m_prefabSystemComponent->FindTemplateDom(rootTemplateId);
  131. rootTemplatePrefabDom.CopyFrom(updatedPrefabDom, rootTemplatePrefabDom.GetAllocator());
  132. state.ResumeTiming();
  133. m_instanceUpdateExecutorInterface->AddTemplateInstancesToQueue(rootTemplateId);
  134. m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
  135. state.PauseTiming();
  136. }
  137. currentInstanceRoot.reset();
  138. ResetPrefabSystem();
  139. state.ResumeTiming();
  140. }
  141. state.SetComplexityN(numInstances);
  142. }
  143. BENCHMARK_DEFINE_F(BM_PrefabUpdateInstances, UpdateInstances_BinaryTreeNestedInstanceHierarchy)(::benchmark::State& state)
  144. {
  145. const unsigned int maxDepth = static_cast<unsigned int>(state.range());
  146. CreateFakePaths(maxDepth);
  147. const unsigned int numInstances = (1 << maxDepth) - 1;
  148. for ([[maybe_unused]] auto _ : state)
  149. {
  150. state.PauseTiming();
  151. AZ::Entity* entity = CreateEntity("Entity");
  152. AZStd::unique_ptr<Instance> currentInstanceRoot = m_prefabSystemComponent->CreatePrefab(
  153. { entity },
  154. {},
  155. m_paths.back());
  156. for (unsigned int currentDepth = 0; currentDepth < maxDepth - 1; ++currentDepth)
  157. {
  158. AZStd::unique_ptr<Instance> extraNestedInstance =
  159. m_prefabSystemComponent->InstantiatePrefab(currentInstanceRoot->GetTemplateId());
  160. currentInstanceRoot = m_prefabSystemComponent->CreatePrefab(
  161. {},
  162. MakeInstanceList(AZStd::move(currentInstanceRoot), AZStd::move(extraNestedInstance)),
  163. m_paths[currentDepth]);
  164. }
  165. entity->SetName("Updated Entity");
  166. PrefabDom updatedPrefabDom;
  167. PrefabDomUtils::StoreInstanceInPrefabDom(*currentInstanceRoot, updatedPrefabDom);
  168. const TemplateId rootTemplateId = currentInstanceRoot->GetTemplateId();
  169. PrefabDom& rootTemplatePrefabDom = m_prefabSystemComponent->FindTemplateDom(rootTemplateId);
  170. rootTemplatePrefabDom.CopyFrom(updatedPrefabDom, rootTemplatePrefabDom.GetAllocator());
  171. state.ResumeTiming();
  172. m_instanceUpdateExecutorInterface->AddTemplateInstancesToQueue(rootTemplateId);
  173. m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
  174. state.PauseTiming();
  175. currentInstanceRoot.reset();
  176. ResetPrefabSystem();
  177. state.ResumeTiming();
  178. }
  179. state.SetComplexityN(numInstances);
  180. }
  181. BENCHMARK_REGISTER_F(BM_PrefabUpdateInstances, UpdateInstances_BinaryTreeNestedInstanceHierarchy)
  182. ->DenseRange(8, 12, 2)
  183. ->Unit(benchmark::kMillisecond)
  184. ->Complexity();
  185. }
  186. #endif