SpawnAllEntitiesBenchmarks.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/Spawnable/SpawnableBenchmarkFixture.h>
  10. #include <AzFramework/Spawnable/SpawnableEntitiesInterface.h>
  11. #include <AzToolsFramework/Prefab/Spawnable/SpawnableUtils.h>
  12. namespace Benchmark
  13. {
  14. using BM_SpawnAllEntities = BM_Spawnable;
  15. BENCHMARK_DEFINE_F(BM_SpawnAllEntities, SingleEntitySpawnable_SpawnCallVariable)(::benchmark::State& state)
  16. {
  17. const uint64_t spawnAllEntitiesCallCount = aznumeric_cast<uint64_t>(state.range());
  18. const uint64_t entityCountInSourcePrefab = 1;
  19. SetUpSpawnableAsset(entityCountInSourcePrefab);
  20. for ([[maybe_unused]] auto _ : state)
  21. {
  22. state.PauseTiming();
  23. m_spawnTicket = aznew AzFramework::EntitySpawnTicket(m_spawnableAsset);
  24. state.ResumeTiming();
  25. for (uint64_t spwanableCounter = 0; spwanableCounter < spawnAllEntitiesCallCount; spwanableCounter++)
  26. {
  27. AzFramework::SpawnableEntitiesInterface::Get()->SpawnAllEntities(*m_spawnTicket);
  28. }
  29. m_rootSpawnableInterface->ProcessSpawnableQueue();
  30. // Destroy the ticket so that this queues a request to delete all the entities spawned with this ticket.
  31. state.PauseTiming();
  32. delete m_spawnTicket;
  33. m_spawnTicket = nullptr;
  34. // This will process the request to delete all entities spawned with the ticket
  35. m_rootSpawnableInterface->ProcessSpawnableQueue();
  36. state.ResumeTiming();
  37. }
  38. state.SetComplexityN(spawnAllEntitiesCallCount);
  39. }
  40. BENCHMARK_REGISTER_F(BM_SpawnAllEntities, SingleEntitySpawnable_SpawnCallVariable)
  41. ->RangeMultiplier(10)
  42. ->Range(100, 10000)
  43. ->Unit(benchmark::kMillisecond)
  44. ->Complexity();
  45. BENCHMARK_DEFINE_F(BM_SpawnAllEntities, SingleSpawnCall_EntityCountVariable)(::benchmark::State& state)
  46. {
  47. const uint64_t entityCountInSpawnable = aznumeric_cast<uint64_t>(state.range());
  48. SetUpSpawnableAsset(entityCountInSpawnable);
  49. for ([[maybe_unused]] auto _ : state)
  50. {
  51. state.PauseTiming();
  52. m_spawnTicket = aznew AzFramework::EntitySpawnTicket(m_spawnableAsset);
  53. state.ResumeTiming();
  54. AzFramework::SpawnableEntitiesInterface::Get()->SpawnAllEntities(*m_spawnTicket);
  55. m_rootSpawnableInterface->ProcessSpawnableQueue();
  56. // Destroy the ticket so that this queues a request to delete all the entities spawned with this ticket.
  57. state.PauseTiming();
  58. delete m_spawnTicket;
  59. m_spawnTicket = nullptr;
  60. // This will process the request to delete all entities spawned with the ticket
  61. m_rootSpawnableInterface->ProcessSpawnableQueue();
  62. state.ResumeTiming();
  63. }
  64. state.SetComplexityN(entityCountInSpawnable);
  65. }
  66. BENCHMARK_REGISTER_F(BM_SpawnAllEntities, SingleSpawnCall_EntityCountVariable)
  67. ->RangeMultiplier(10)
  68. ->Range(100, 10000)
  69. ->Unit(benchmark::kMillisecond)
  70. ->Complexity();
  71. BENCHMARK_DEFINE_F(BM_SpawnAllEntities, EntityCountVariable_SpawnCallCountVariable)(::benchmark::State& state)
  72. {
  73. const uint64_t entityCountInSpawnable = aznumeric_cast<uint64_t>(state.range(0));
  74. const uint64_t spawnCallCount = aznumeric_cast<uint64_t>(state.range(1));
  75. SetUpSpawnableAsset(entityCountInSpawnable);
  76. auto spawner = AzFramework::SpawnableEntitiesInterface::Get();
  77. for ([[maybe_unused]] auto _ : state)
  78. {
  79. state.PauseTiming();
  80. m_spawnTicket = aznew AzFramework::EntitySpawnTicket(m_spawnableAsset);
  81. state.ResumeTiming();
  82. for (uint64_t spawnCallCounter = 0; spawnCallCounter < spawnCallCount; spawnCallCounter++)
  83. {
  84. spawner->SpawnAllEntities(*m_spawnTicket);
  85. }
  86. m_rootSpawnableInterface->ProcessSpawnableQueue();
  87. state.PauseTiming();
  88. delete m_spawnTicket;
  89. m_spawnTicket = nullptr;
  90. m_rootSpawnableInterface->ProcessSpawnableQueue();
  91. state.ResumeTiming();
  92. }
  93. state.SetComplexityN(entityCountInSpawnable * spawnCallCount);
  94. }
  95. // Provide ranges here to compare times for spawning the same number of entities by altering entityCountInSpawnable and spawnCallCount.
  96. BENCHMARK_REGISTER_F(BM_SpawnAllEntities, EntityCountVariable_SpawnCallCountVariable)
  97. ->Args({ 10, 100 })
  98. ->Args({ 100, 10 })
  99. ->Args({ 10, 1000 })
  100. ->Args({ 1000, 10 })
  101. ->Args({ 100, 1000 })
  102. ->Args({ 1000, 100 })
  103. ->Unit(benchmark::kMillisecond)
  104. ->Complexity();
  105. } // namespace Benchmark
  106. #endif