SingleInstanceMultiplePatchesBenchmarks.cpp 5.8 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. #if defined(HAVE_BENCHMARK)
  9. #include <AzToolsFramework/Prefab/PrefabDomUtils.h>
  10. #include <Prefab/Benchmark/Link/SingleInstanceMultiplePatchesBenchmarks.h>
  11. #define REGISTER_MULTIPLE_PATCHES_BENCHMARK(BaseClass, Method) \
  12. BENCHMARK_REGISTER_F(BaseClass, Method) \
  13. ->RangeMultiplier(10) \
  14. ->Range(100, 10000) \
  15. ->ArgNames({ "PatchesCount" }) \
  16. ->Unit(benchmark::kMillisecond);
  17. namespace Benchmark
  18. {
  19. using namespace AzToolsFramework::Prefab;
  20. void SingleInstanceMultiplePatchesBenchmarks::SetupHarness(const benchmark::State& state)
  21. {
  22. BM_Prefab::SetupHarness(state);
  23. const unsigned int numEntities = static_cast<unsigned int>(state.range());
  24. AZStd::vector<AZ::Entity*> entities;
  25. // The patch array generated in this function on transform component has 2 elements. To keep the patch count match the range,
  26. // we are only creating half the number of entities; (1 entity * 2 patches * n/2) = n patches, where n is the range.
  27. for (unsigned int i = 0; i < numEntities / 2; i++)
  28. {
  29. entities.emplace_back(CreateEntity("Entity"));
  30. }
  31. CreateFakePaths(2);
  32. AZStd::unique_ptr<AzToolsFramework::Prefab::Instance> nestedInstance =
  33. m_prefabSystemComponent->CreatePrefab(entities, {}, m_paths.front());
  34. m_parentInstance = m_prefabSystemComponent->CreatePrefab({}, MakeInstanceList(AZStd::move(nestedInstance)), m_paths.back());
  35. m_linkDomToSet = AZStd::make_unique<PrefabDom>();
  36. m_linkDomToSet->SetObject();
  37. PrefabDomValue patchesArray;
  38. patchesArray.SetArray();
  39. m_parentInstance->GetNestedInstances(
  40. [this, &patchesArray](AZStd::unique_ptr<Instance>& nestedInstance)
  41. {
  42. m_linkId = nestedInstance->GetLinkId();
  43. nestedInstance->GetEntities(
  44. [this, &patchesArray](AZStd::unique_ptr<AZ::Entity>& entity)
  45. {
  46. PrefabDom entityDomBefore;
  47. InstanceToTemplateInterface* instanceToTemplateInterface = AZ::Interface<InstanceToTemplateInterface>::Get();
  48. AZ_Assert(instanceToTemplateInterface, "Could not retrieve instance of InstanceToTemplateInterface");
  49. instanceToTemplateInterface->GenerateEntityDomBySerializing(entityDomBefore, *(entity.get()));
  50. AZ::TransformBus::Event(entity->GetId(), &AZ::TransformBus::Events::SetWorldX, 10.0f);
  51. PrefabDom entityDomAfter;
  52. instanceToTemplateInterface->GenerateEntityDomBySerializing(entityDomAfter, *(entity.get()));
  53. PrefabDom patch;
  54. instanceToTemplateInterface->GeneratePatch(patch, entityDomBefore, entityDomAfter);
  55. instanceToTemplateInterface->PrependEntityAliasPathToPatchPaths(patch, entity->GetId());
  56. for (auto& entry : patch.GetArray())
  57. {
  58. PrefabDomValue patchEntryCopy;
  59. patchEntryCopy.CopyFrom(entry, m_linkDomToSet->GetAllocator());
  60. patchesArray.PushBack(patchEntryCopy.Move(), m_linkDomToSet->GetAllocator());
  61. }
  62. return true;
  63. });
  64. });
  65. LinkReference link = m_prefabSystemComponent->FindLink(m_linkId);
  66. AZ_Assert(link.has_value(), "Link between prefabs is missing.");
  67. m_linkDomToSet->AddMember(
  68. rapidjson::StringRef(PrefabDomUtils::SourceName), rapidjson::StringRef(m_paths.front().c_str()), m_linkDomToSet->GetAllocator());
  69. m_linkDomToSet->AddMember(rapidjson::StringRef(PrefabDomUtils::PatchesName), patchesArray, m_linkDomToSet->GetAllocator());
  70. link->get().SetLinkDom(*m_linkDomToSet);
  71. }
  72. void SingleInstanceMultiplePatchesBenchmarks::TeardownHarness(const benchmark::State& state)
  73. {
  74. m_linkDomToSet.reset();
  75. m_parentInstance.reset();
  76. BM_Prefab::TeardownHarness(state);
  77. }
  78. BENCHMARK_DEFINE_F(SingleInstanceMultiplePatchesBenchmarks, GetLinkDom)(benchmark::State& state)
  79. {
  80. for ([[maybe_unused]] auto _ : state)
  81. {
  82. LinkReference link = m_prefabSystemComponent->FindLink(m_linkId);
  83. AZ_Assert(link.has_value(), "Link between prefabs is missing.");
  84. PrefabDom linkDom;
  85. link->get().GetLinkDom(linkDom, linkDom.GetAllocator());
  86. }
  87. }
  88. REGISTER_MULTIPLE_PATCHES_BENCHMARK(SingleInstanceMultiplePatchesBenchmarks, GetLinkDom);
  89. BENCHMARK_DEFINE_F(SingleInstanceMultiplePatchesBenchmarks, SetLinkDom)(benchmark::State& state)
  90. {
  91. for ([[maybe_unused]] auto _ : state)
  92. {
  93. LinkReference link = m_prefabSystemComponent->FindLink(m_linkId);
  94. AZ_Assert(link.has_value(), "Link between prefabs is missing.");
  95. link->get().SetLinkDom(*m_linkDomToSet);
  96. }
  97. }
  98. REGISTER_MULTIPLE_PATCHES_BENCHMARK(SingleInstanceMultiplePatchesBenchmarks, SetLinkDom);
  99. } // namespace Benchmark
  100. #endif