123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- /*
- * Copyright (c) Contributors to the Open 3D Engine Project.
- * For complete copyright and license terms please see the LICENSE at the root of this distribution.
- *
- * SPDX-License-Identifier: Apache-2.0 OR MIT
- *
- */
- #if defined(HAVE_BENCHMARK)
- #include <Prefab/Benchmark/PrefabBenchmarkFixture.h>
- #include <AzToolsFramework/Prefab/PrefabDomUtils.h>
- namespace Benchmark
- {
- using BM_PrefabUpdateInstances = BM_Prefab;
- using namespace AzToolsFramework::Prefab;
- BENCHMARK_DEFINE_F(BM_PrefabUpdateInstances, UpdateInstances_SingeEntityInstances)(::benchmark::State& state)
- {
- const unsigned int numInstances = static_cast<unsigned int>(state.range());
- CreateFakePaths(2);
- const auto& nestedTemplatePath = m_paths.front();
- const auto& enclosingTemplatePath = m_paths.back();
- for ([[maybe_unused]] auto _ : state)
- {
- state.PauseTiming();
- AZ::Entity* entity = CreateEntity("Entity");
- AZStd::unique_ptr<Instance> nestedInstance = m_prefabSystemComponent->CreatePrefab(
- { entity },
- {},
- nestedTemplatePath);
- AZStd::unique_ptr<Instance> enclosingInstance = m_prefabSystemComponent->CreatePrefab(
- {},
- MakeInstanceList(AZStd::move(nestedInstance)),
- enclosingTemplatePath);
- TemplateId templateToInstantiateId = enclosingInstance->GetTemplateId();
- {
- AZStd::vector<AZStd::unique_ptr<Instance>> newInstances;
- newInstances.resize(numInstances);
- for (unsigned int instanceCounter = 0; instanceCounter < numInstances; ++instanceCounter)
- {
- newInstances[instanceCounter] = m_prefabSystemComponent->InstantiatePrefab(templateToInstantiateId);
- }
- entity->SetName("Updated Entity");
- PrefabDom updatedPrefabDom;
- PrefabDomUtils::StoreInstanceInPrefabDom(*enclosingInstance, updatedPrefabDom);
- PrefabDom& enclosingTemplatePrefabDom = m_prefabSystemComponent->FindTemplateDom(templateToInstantiateId);
- enclosingTemplatePrefabDom.CopyFrom(updatedPrefabDom, enclosingTemplatePrefabDom.GetAllocator());
- state.ResumeTiming();
- m_instanceUpdateExecutorInterface->AddTemplateInstancesToQueue(templateToInstantiateId);
- m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
- state.PauseTiming();
- }
- enclosingInstance.reset();
- ResetPrefabSystem();
- state.ResumeTiming();
- }
- state.SetComplexityN(numInstances);
- }
- BENCHMARK_REGISTER_F(BM_PrefabUpdateInstances, UpdateInstances_SingeEntityInstances)
- ->RangeMultiplier(10)
- ->Range(100, 10000)
- ->Unit(benchmark::kMillisecond)
- ->Complexity();
- BENCHMARK_DEFINE_F(BM_PrefabUpdateInstances, UpdateInstances_SingleLinearNestingOfInstances)(::benchmark::State& state)
- {
- const unsigned int maxDepth = static_cast<unsigned int>(state.range());
- CreateFakePaths(maxDepth);
- const unsigned int numInstances = maxDepth;
- for ([[maybe_unused]] auto _ : state)
- {
- state.PauseTiming();
- AZ::Entity* entity = CreateEntity("Entity");
- AZStd::unique_ptr<Instance> currentInstanceRoot = m_prefabSystemComponent->CreatePrefab(
- { entity },
- {},
- m_paths.back());
- for (unsigned int currentDepth = 1; currentDepth < maxDepth; ++currentDepth)
- {
- currentInstanceRoot = m_prefabSystemComponent->CreatePrefab(
- {},
- MakeInstanceList(AZStd::move(currentInstanceRoot)),
- m_paths[currentDepth - 1]);
- }
- entity->SetName("Updated Entity");
- PrefabDom updatedPrefabDom;
- PrefabDomUtils::StoreInstanceInPrefabDom(*currentInstanceRoot, updatedPrefabDom);
- const TemplateId rootTemplateId = currentInstanceRoot->GetTemplateId();
- PrefabDom& rootTemplatePrefabDom = m_prefabSystemComponent->FindTemplateDom(rootTemplateId);
- rootTemplatePrefabDom.CopyFrom(updatedPrefabDom, rootTemplatePrefabDom.GetAllocator());
- state.ResumeTiming();
- m_instanceUpdateExecutorInterface->AddTemplateInstancesToQueue(rootTemplateId);
- m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
- state.PauseTiming();
- currentInstanceRoot.reset();
- ResetPrefabSystem();
- state.ResumeTiming();
- }
- state.SetComplexityN(numInstances);
- }
- BENCHMARK_DEFINE_F(BM_PrefabUpdateInstances, UpdateInstances_MultipleLinearNestingOfInstances)(::benchmark::State& state)
- {
- const unsigned int numRootInstances = static_cast<unsigned int>(state.range());
- const unsigned int maxDepth = static_cast<unsigned int>(state.range());
- CreateFakePaths(maxDepth);
- const unsigned int numInstances = numRootInstances * maxDepth;
- for ([[maybe_unused]] auto _ : state)
- {
- state.PauseTiming();
- AZ::Entity* entity = CreateEntity("Entity");
- AZStd::unique_ptr<Instance> currentInstanceRoot = m_prefabSystemComponent->CreatePrefab(
- { entity },
- {},
- m_paths.back());
- for (unsigned int currentDepth = 0; currentDepth < maxDepth - 1; ++currentDepth)
- {
- currentInstanceRoot = m_prefabSystemComponent->CreatePrefab(
- {},
- MakeInstanceList(AZStd::move(currentInstanceRoot)),
- m_paths[currentDepth]);
- }
- const TemplateId rootTemplateId = currentInstanceRoot->GetTemplateId();
- {
- AZStd::vector<AZStd::unique_ptr<Instance>> newInstances;
- newInstances.resize(numRootInstances - 1);
- for (unsigned int instanceCounter = 0; instanceCounter < numRootInstances - 1; ++instanceCounter)
- {
- newInstances[instanceCounter] = m_prefabSystemComponent->InstantiatePrefab(rootTemplateId);
- }
- entity->SetName("Updated Entity");
- PrefabDom updatedPrefabDom;
- PrefabDomUtils::StoreInstanceInPrefabDom(*currentInstanceRoot, updatedPrefabDom);
- PrefabDom& rootTemplatePrefabDom = m_prefabSystemComponent->FindTemplateDom(rootTemplateId);
- rootTemplatePrefabDom.CopyFrom(updatedPrefabDom, rootTemplatePrefabDom.GetAllocator());
- state.ResumeTiming();
- m_instanceUpdateExecutorInterface->AddTemplateInstancesToQueue(rootTemplateId);
- m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
- state.PauseTiming();
- }
- currentInstanceRoot.reset();
- ResetPrefabSystem();
- state.ResumeTiming();
- }
- state.SetComplexityN(numInstances);
- }
- BENCHMARK_DEFINE_F(BM_PrefabUpdateInstances, UpdateInstances_BinaryTreeNestedInstanceHierarchy)(::benchmark::State& state)
- {
- const unsigned int maxDepth = static_cast<unsigned int>(state.range());
- CreateFakePaths(maxDepth);
- const unsigned int numInstances = (1 << maxDepth) - 1;
- for ([[maybe_unused]] auto _ : state)
- {
- state.PauseTiming();
- AZ::Entity* entity = CreateEntity("Entity");
- AZStd::unique_ptr<Instance> currentInstanceRoot = m_prefabSystemComponent->CreatePrefab(
- { entity },
- {},
- m_paths.back());
- for (unsigned int currentDepth = 0; currentDepth < maxDepth - 1; ++currentDepth)
- {
- AZStd::unique_ptr<Instance> extraNestedInstance =
- m_prefabSystemComponent->InstantiatePrefab(currentInstanceRoot->GetTemplateId());
- currentInstanceRoot = m_prefabSystemComponent->CreatePrefab(
- {},
- MakeInstanceList(AZStd::move(currentInstanceRoot), AZStd::move(extraNestedInstance)),
- m_paths[currentDepth]);
- }
- entity->SetName("Updated Entity");
- PrefabDom updatedPrefabDom;
- PrefabDomUtils::StoreInstanceInPrefabDom(*currentInstanceRoot, updatedPrefabDom);
- const TemplateId rootTemplateId = currentInstanceRoot->GetTemplateId();
- PrefabDom& rootTemplatePrefabDom = m_prefabSystemComponent->FindTemplateDom(rootTemplateId);
- rootTemplatePrefabDom.CopyFrom(updatedPrefabDom, rootTemplatePrefabDom.GetAllocator());
- state.ResumeTiming();
- m_instanceUpdateExecutorInterface->AddTemplateInstancesToQueue(rootTemplateId);
- m_instanceUpdateExecutorInterface->UpdateTemplateInstancesInQueue();
- state.PauseTiming();
- currentInstanceRoot.reset();
- ResetPrefabSystem();
- state.ResumeTiming();
- }
- state.SetComplexityN(numInstances);
- }
- BENCHMARK_REGISTER_F(BM_PrefabUpdateInstances, UpdateInstances_BinaryTreeNestedInstanceHierarchy)
- ->DenseRange(8, 12, 2)
- ->Unit(benchmark::kMillisecond)
- ->Complexity();
- }
- #endif
|