SpawnableBenchmarkFixture.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/Spawnable/SpawnableUtils.h>
  10. #include <Prefab/Benchmark/Spawnable/SpawnableBenchmarkFixture.h>
  11. namespace Benchmark
  12. {
  13. void BM_Spawnable::SetUp(const benchmark::State& state)
  14. {
  15. SetUpHelper(state);
  16. }
  17. void BM_Spawnable::SetUp(benchmark::State& state)
  18. {
  19. SetUpHelper(state);
  20. }
  21. void BM_Spawnable::SetUpHelper(const benchmark::State& state)
  22. {
  23. BM_Prefab::SetUp(state);
  24. m_rootSpawnableInterface = AzFramework::RootSpawnableInterface::Get();
  25. AZ_Assert(m_rootSpawnableInterface != nullptr, "RootSpawnableInterface isn't found.");
  26. }
  27. void BM_Spawnable::TearDown(const benchmark::State& state)
  28. {
  29. TearDownHelper(state);
  30. }
  31. void BM_Spawnable::TearDown(benchmark::State& state)
  32. {
  33. TearDownHelper(state);
  34. }
  35. void BM_Spawnable::TearDownHelper(const benchmark::State& state)
  36. {
  37. m_spawnableAsset.Release();
  38. BM_Prefab::TearDown(state);
  39. }
  40. void BM_Spawnable::SetUpSpawnableAsset(uint64_t entityCount)
  41. {
  42. AZStd::vector<AZ::Entity*> entities;
  43. entities.reserve(entityCount);
  44. for (uint64_t i = 0; i < entityCount; i++)
  45. {
  46. entities.emplace_back(CreateEntity("Entity"));
  47. }
  48. AZStd::unique_ptr<Instance> instance = m_prefabSystemComponent->CreatePrefab(AZStd::move(entities), {}, m_pathString);
  49. const PrefabDom& prefabDom = m_prefabSystemComponent->FindTemplateDom(instance->GetTemplateId());
  50. // Lifecycle of spawnable is managed by the asset that's created using it.
  51. AzFramework::Spawnable* spawnable = new AzFramework::Spawnable(
  52. AZ::Data::AssetId::CreateString("{612F2AB1-30DF-44BB-AFBE-17A85199F09E}:0"), AZ::Data::AssetData::AssetStatus::Ready);
  53. AzToolsFramework::Prefab::SpawnableUtils::CreateSpawnable(*spawnable, prefabDom);
  54. m_spawnableAsset = AZ::Data::Asset<AzFramework::Spawnable>(spawnable, AZ::Data::AssetLoadBehavior::Default);
  55. }
  56. } // namespace Benchmark
  57. #endif