DomJsonBenchmarks.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <AzCore/DOM/Backends/JSON/JsonBackend.h>
  10. #include <AzCore/DOM/Backends/JSON/JsonSerializationUtils.h>
  11. #include <AzCore/DOM/DomUtils.h>
  12. #include <AzCore/DOM/DomValue.h>
  13. #include <AzCore/JSON/document.h>
  14. #include <AzCore/Name/NameDictionary.h>
  15. #include <AzCore/Serialization/Json/JsonUtils.h>
  16. #include <AzCore/UnitTest/TestTypes.h>
  17. #include <Tests/DOM/DomFixtures.h>
  18. namespace AZ::Dom::Benchmark
  19. {
  20. class DomJsonBenchmark : public Tests::DomBenchmarkFixture
  21. {
  22. };
  23. BENCHMARK_DEFINE_F(DomJsonBenchmark, AzDomDeserializeToRapidjsonInPlace)(benchmark::State& state)
  24. {
  25. AZ::Dom::JsonBackend backend;
  26. AZStd::string serializedPayload = GenerateDomJsonBenchmarkPayload(state.range(0), state.range(1));
  27. for ([[maybe_unused]] auto _ : state)
  28. {
  29. state.PauseTiming();
  30. AZStd::string payloadCopy = serializedPayload;
  31. state.ResumeTiming();
  32. auto result = AZ::Dom::Json::WriteToRapidJsonDocument(
  33. [&](AZ::Dom::Visitor& visitor)
  34. {
  35. return AZ::Dom::Utils::ReadFromStringInPlace(backend, payloadCopy, visitor);
  36. });
  37. TakeAndDiscardWithoutTimingDtor(result.TakeValue(), state);
  38. }
  39. state.SetBytesProcessed(serializedPayload.size() * state.iterations());
  40. }
  41. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomJsonBenchmark, AzDomDeserializeToRapidjsonInPlace)
  42. BENCHMARK_DEFINE_F(DomJsonBenchmark, AzDomDeserializeToAzDomValueInPlace)(benchmark::State& state)
  43. {
  44. AZ::Dom::JsonBackend backend;
  45. AZStd::string serializedPayload = GenerateDomJsonBenchmarkPayload(state.range(0), state.range(1));
  46. for ([[maybe_unused]] auto _ : state)
  47. {
  48. state.PauseTiming();
  49. AZStd::string payloadCopy = serializedPayload;
  50. state.ResumeTiming();
  51. auto result = AZ::Dom::Utils::WriteToValue(
  52. [&](AZ::Dom::Visitor& visitor)
  53. {
  54. return AZ::Dom::Utils::ReadFromStringInPlace(backend, payloadCopy, visitor);
  55. });
  56. TakeAndDiscardWithoutTimingDtor(result.TakeValue(), state);
  57. }
  58. state.SetBytesProcessed(serializedPayload.size() * state.iterations());
  59. }
  60. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomJsonBenchmark, AzDomDeserializeToAzDomValueInPlace)
  61. BENCHMARK_DEFINE_F(DomJsonBenchmark, AzDomDeserializeToRapidjson)(benchmark::State& state)
  62. {
  63. AZ::Dom::JsonBackend backend;
  64. AZStd::string serializedPayload = GenerateDomJsonBenchmarkPayload(state.range(0), state.range(1));
  65. for ([[maybe_unused]] auto _ : state)
  66. {
  67. auto result = AZ::Dom::Json::WriteToRapidJsonDocument(
  68. [&](AZ::Dom::Visitor& visitor)
  69. {
  70. return AZ::Dom::Utils::ReadFromString(backend, serializedPayload, AZ::Dom::Lifetime::Temporary, visitor);
  71. });
  72. TakeAndDiscardWithoutTimingDtor(result.TakeValue(), state);
  73. }
  74. state.SetBytesProcessed(serializedPayload.size() * state.iterations());
  75. }
  76. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomJsonBenchmark, AzDomDeserializeToRapidjson)
  77. BENCHMARK_DEFINE_F(DomJsonBenchmark, AzDomDeserializeToAzDomValue)(benchmark::State& state)
  78. {
  79. AZ::Dom::JsonBackend backend;
  80. AZStd::string serializedPayload = GenerateDomJsonBenchmarkPayload(state.range(0), state.range(1));
  81. for ([[maybe_unused]] auto _ : state)
  82. {
  83. auto result = AZ::Dom::Utils::WriteToValue(
  84. [&](AZ::Dom::Visitor& visitor)
  85. {
  86. return AZ::Dom::Utils::ReadFromString(backend, serializedPayload, AZ::Dom::Lifetime::Temporary, visitor);
  87. });
  88. TakeAndDiscardWithoutTimingDtor(result.TakeValue(), state);
  89. }
  90. state.SetBytesProcessed(serializedPayload.size() * state.iterations());
  91. }
  92. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomJsonBenchmark, AzDomDeserializeToAzDomValue)
  93. BENCHMARK_DEFINE_F(DomJsonBenchmark, RapidjsonDeserializeToRapidjson)(benchmark::State& state)
  94. {
  95. AZ::Dom::JsonBackend backend;
  96. AZStd::string serializedPayload = GenerateDomJsonBenchmarkPayload(state.range(0), state.range(1));
  97. for ([[maybe_unused]] auto _ : state)
  98. {
  99. auto result = AZ::JsonSerializationUtils::ReadJsonString(serializedPayload);
  100. TakeAndDiscardWithoutTimingDtor(result.TakeValue(), state);
  101. }
  102. state.SetBytesProcessed(serializedPayload.size() * state.iterations());
  103. }
  104. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomJsonBenchmark, RapidjsonDeserializeToRapidjson)
  105. BENCHMARK_DEFINE_F(DomJsonBenchmark, RapidjsonMakeComplexObject)(benchmark::State& state)
  106. {
  107. for ([[maybe_unused]] auto _ : state)
  108. {
  109. TakeAndDiscardWithoutTimingDtor(GenerateDomJsonBenchmarkPayload(state.range(0), state.range(1)), state);
  110. }
  111. state.SetItemsProcessed(state.range(0) * state.range(0) * state.iterations());
  112. }
  113. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomJsonBenchmark, RapidjsonMakeComplexObject)
  114. BENCHMARK_DEFINE_F(DomJsonBenchmark, RapidjsonLookupMemberByString)(benchmark::State& state)
  115. {
  116. rapidjson::Document document(rapidjson::kObjectType);
  117. AZStd::vector<AZStd::string> keys;
  118. for (int64_t i = 0; i < state.range(0); ++i)
  119. {
  120. AZStd::string key(AZStd::string::format("key%" PRId64, i));
  121. keys.push_back(key);
  122. document.AddMember(
  123. rapidjson::Value(key.data(), static_cast<rapidjson::SizeType>(key.size()), document.GetAllocator()), rapidjson::Value(i),
  124. document.GetAllocator());
  125. }
  126. for ([[maybe_unused]] auto _ : state)
  127. {
  128. for (const AZStd::string& key : keys)
  129. {
  130. benchmark::DoNotOptimize(document.FindMember(key.data()));
  131. }
  132. }
  133. state.SetItemsProcessed(state.iterations() * state.range(0));
  134. }
  135. BENCHMARK_REGISTER_F(DomJsonBenchmark, RapidjsonLookupMemberByString)->Arg(100)->Arg(1000)->Arg(10000)->Unit(benchmark::kMillisecond);
  136. BENCHMARK_DEFINE_F(DomJsonBenchmark, RapidjsonDeepCopy)(benchmark::State& state)
  137. {
  138. rapidjson::Document original = GenerateDomJsonBenchmarkDocument(state.range(0), state.range(1));
  139. for ([[maybe_unused]] auto _ : state)
  140. {
  141. rapidjson::Document copy;
  142. copy.CopyFrom(original, copy.GetAllocator(), true);
  143. TakeAndDiscardWithoutTimingDtor(AZStd::move(copy), state);
  144. }
  145. state.SetItemsProcessed(state.iterations());
  146. }
  147. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomJsonBenchmark, RapidjsonDeepCopy)
  148. BENCHMARK_DEFINE_F(DomJsonBenchmark, RapidjsonCopyAndMutate)(benchmark::State& state)
  149. {
  150. rapidjson::Document original = GenerateDomJsonBenchmarkDocument(state.range(0), state.range(1));
  151. for ([[maybe_unused]] auto _ : state)
  152. {
  153. rapidjson::Document copy;
  154. copy.CopyFrom(original, copy.GetAllocator(), true);
  155. copy["entries"]["Key0"].PushBack(42, copy.GetAllocator());
  156. TakeAndDiscardWithoutTimingDtor(AZStd::move(copy), state);
  157. }
  158. state.SetItemsProcessed(state.iterations());
  159. }
  160. DOM_REGISTER_SERIALIZATION_BENCHMARK_MS(DomJsonBenchmark, RapidjsonCopyAndMutate)
  161. } // namespace AZ::Dom::Benchmark
  162. #endif // defined(HAVE_BENCHMARK)