DomPatchBenchmarks.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. #include <AzCore/DOM/DomPatch.h>
  9. #include <AzCore/DOM/DomUtils.h>
  10. #include <AzCore/DOM/DomValue.h>
  11. #include <AzCore/DOM/DomComparison.h>
  12. #include <AzCore/Name/NameDictionary.h>
  13. #include <AzCore/UnitTest/TestTypes.h>
  14. #include <Tests/DOM/DomFixtures.h>
  15. namespace AZ::Dom::Benchmark
  16. {
  17. class DomPatchBenchmark : public Tests::DomBenchmarkFixture
  18. {
  19. public:
  20. void TearDownHarness() override
  21. {
  22. m_before = {};
  23. m_after = {};
  24. Tests::DomBenchmarkFixture::TearDownHarness();
  25. }
  26. void SimpleReplace(benchmark::State& state, bool deepCopy, bool apply)
  27. {
  28. m_before = GenerateDomBenchmarkPayload(state.range(0), state.range(1));
  29. m_after = deepCopy ? Utils::DeepCopy(m_before) : m_before;
  30. m_after["entries"]["Key0"] = Value("replacement string", true);
  31. RunBenchmarkInternal(state, apply);
  32. }
  33. void TopLevelReplace(benchmark::State& state, bool apply)
  34. {
  35. m_before = GenerateDomBenchmarkPayload(state.range(0), state.range(1));
  36. m_after = Value(Type::Object);
  37. m_after["UnrelatedKey"] = Value(42);
  38. RunBenchmarkInternal(state, apply);
  39. }
  40. void KeyRemove(benchmark::State& state, bool deepCopy, bool apply)
  41. {
  42. m_before = GenerateDomBenchmarkPayload(state.range(0), state.range(1));
  43. m_after = deepCopy ? Utils::DeepCopy(m_before) : m_before;
  44. m_after["entries"].RemoveMember("Key1");
  45. RunBenchmarkInternal(state, apply);
  46. }
  47. void ArrayAppend(benchmark::State& state, bool deepCopy, bool apply)
  48. {
  49. m_before = GenerateDomBenchmarkPayload(state.range(0), state.range(1));
  50. m_after = deepCopy ? Utils::DeepCopy(m_before) : m_before;
  51. m_after["entries"]["Key2"].ArrayPushBack(Value(0));
  52. RunBenchmarkInternal(state, apply);
  53. }
  54. void ArrayPrepend(benchmark::State& state, bool deepCopy, bool apply)
  55. {
  56. m_before = GenerateDomBenchmarkPayload(state.range(0), state.range(1));
  57. m_after = deepCopy ? Utils::DeepCopy(m_before) : m_before;
  58. auto& arr = m_after["entries"]["Key2"].GetMutableArray();
  59. arr.insert(arr.begin(), Value(42));
  60. RunBenchmarkInternal(state, apply);
  61. }
  62. private:
  63. void RunBenchmarkInternal(benchmark::State& state, bool apply)
  64. {
  65. if (apply)
  66. {
  67. auto patchInfo = GenerateHierarchicalDeltaPatch(m_before, m_after);
  68. for ([[maybe_unused]] auto _ : state)
  69. {
  70. auto patchResult = patchInfo.m_forwardPatches.Apply(m_before);
  71. benchmark::DoNotOptimize(patchResult);
  72. }
  73. }
  74. else
  75. {
  76. for ([[maybe_unused]] auto _ : state)
  77. {
  78. auto patchInfo = GenerateHierarchicalDeltaPatch(m_before, m_after);
  79. benchmark::DoNotOptimize(patchInfo);
  80. }
  81. }
  82. state.SetItemsProcessed(state.iterations());
  83. }
  84. Value m_before;
  85. Value m_after;
  86. };
  87. BENCHMARK_DEFINE_F(DomPatchBenchmark, AzDomPatch_Generate_SimpleReplace_ShallowCopy)(benchmark::State& state)
  88. {
  89. SimpleReplace(state, false, false);
  90. }
  91. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomPatchBenchmark, AzDomPatch_Generate_SimpleReplace_ShallowCopy)
  92. BENCHMARK_DEFINE_F(DomPatchBenchmark, AzDomPatch_Generate_SimpleReplace_DeepCopy)(benchmark::State& state)
  93. {
  94. SimpleReplace(state, true, false);
  95. }
  96. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomPatchBenchmark, AzDomPatch_Generate_SimpleReplace_DeepCopy)
  97. BENCHMARK_DEFINE_F(DomPatchBenchmark, AzDomPatch_Generate_TopLevelReplace)(benchmark::State& state)
  98. {
  99. TopLevelReplace(state, false);
  100. }
  101. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomPatchBenchmark, AzDomPatch_Generate_TopLevelReplace)
  102. BENCHMARK_DEFINE_F(DomPatchBenchmark, AzDomPatch_Generate_KeyRemove_ShallowCopy)(benchmark::State& state)
  103. {
  104. KeyRemove(state, false, false);
  105. }
  106. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomPatchBenchmark, AzDomPatch_Generate_KeyRemove_ShallowCopy)
  107. BENCHMARK_DEFINE_F(DomPatchBenchmark, AzDomPatch_Generate_KeyRemove_DeepCopy)(benchmark::State& state)
  108. {
  109. KeyRemove(state, true, false);
  110. }
  111. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomPatchBenchmark, AzDomPatch_Generate_KeyRemove_DeepCopy)
  112. BENCHMARK_DEFINE_F(DomPatchBenchmark, AzDomPatch_Generate_ArrayAppend_ShallowCopy)(benchmark::State& state)
  113. {
  114. ArrayAppend(state, false, false);
  115. }
  116. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomPatchBenchmark, AzDomPatch_Generate_ArrayAppend_ShallowCopy)
  117. BENCHMARK_DEFINE_F(DomPatchBenchmark, AzDomPatch_Generate_ArrayAppend_DeepCopy)(benchmark::State& state)
  118. {
  119. ArrayAppend(state, true, false);
  120. }
  121. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomPatchBenchmark, AzDomPatch_Generate_ArrayAppend_DeepCopy)
  122. BENCHMARK_DEFINE_F(DomPatchBenchmark, AzDomPatch_Generate_ArrayPrepend)(benchmark::State& state)
  123. {
  124. ArrayPrepend(state, true, false);
  125. }
  126. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomPatchBenchmark, AzDomPatch_Generate_ArrayPrepend)
  127. BENCHMARK_DEFINE_F(DomPatchBenchmark, AzDomPatch_Apply_SimpleReplace)(benchmark::State& state)
  128. {
  129. SimpleReplace(state, true, true);
  130. }
  131. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomPatchBenchmark, AzDomPatch_Apply_SimpleReplace)
  132. BENCHMARK_DEFINE_F(DomPatchBenchmark, AzDomPatch_Apply_TopLevelReplace)(benchmark::State& state)
  133. {
  134. TopLevelReplace(state, true);
  135. }
  136. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomPatchBenchmark, AzDomPatch_Apply_TopLevelReplace)
  137. BENCHMARK_DEFINE_F(DomPatchBenchmark, AzDomPatch_Apply_KeyRemove)(benchmark::State& state)
  138. {
  139. KeyRemove(state, true, true);
  140. }
  141. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomPatchBenchmark, AzDomPatch_Apply_KeyRemove)
  142. BENCHMARK_DEFINE_F(DomPatchBenchmark, AzDomPatch_Apply_ArrayAppend)(benchmark::State& state)
  143. {
  144. ArrayAppend(state, true, true);
  145. }
  146. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomPatchBenchmark, AzDomPatch_Apply_ArrayAppend)
  147. BENCHMARK_DEFINE_F(DomPatchBenchmark, AzDomPatch_Apply_ArrayPrepend)(benchmark::State& state)
  148. {
  149. ArrayPrepend(state, true, true);
  150. }
  151. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomPatchBenchmark, AzDomPatch_Apply_ArrayPrepend)
  152. } // namespace AZ::Dom::Benchmark