DomFixtures.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/DomValue.h>
  9. #include <AzCore/Name/NameDictionary.h>
  10. #include <AzCore/Serialization/Json/JsonUtils.h>
  11. #include <Tests/DOM/DomFixtures.h>
  12. namespace AZ::Dom::Tests
  13. {
  14. void DomTestHarness::SetUpHarness()
  15. {
  16. NameDictionary::Create();
  17. }
  18. void DomTestHarness::TearDownHarness()
  19. {
  20. NameDictionary::Destroy();
  21. }
  22. void DomBenchmarkFixture::SetUp(const ::benchmark::State& st)
  23. {
  24. UnitTest::AllocatorsBenchmarkFixture::SetUp(st);
  25. SetUpHarness();
  26. }
  27. void DomBenchmarkFixture::SetUp(::benchmark::State& st)
  28. {
  29. UnitTest::AllocatorsBenchmarkFixture::SetUp(st);
  30. SetUpHarness();
  31. }
  32. void DomBenchmarkFixture::TearDown(::benchmark::State& st)
  33. {
  34. TearDownHarness();
  35. UnitTest::AllocatorsBenchmarkFixture::TearDown(st);
  36. }
  37. void DomBenchmarkFixture::TearDown(const ::benchmark::State& st)
  38. {
  39. TearDownHarness();
  40. UnitTest::AllocatorsBenchmarkFixture::TearDown(st);
  41. }
  42. rapidjson::Document DomBenchmarkFixture::GenerateDomJsonBenchmarkDocument(int64_t entryCount, int64_t stringTemplateLength)
  43. {
  44. rapidjson::Document document;
  45. document.SetObject();
  46. AZStd::string entryTemplate;
  47. while (entryTemplate.size() < aznumeric_cast<size_t>(stringTemplateLength))
  48. {
  49. entryTemplate += "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor ";
  50. }
  51. entryTemplate.resize(stringTemplateLength);
  52. AZStd::string buffer;
  53. auto createString = [&](int n) -> rapidjson::Value
  54. {
  55. buffer = AZStd::string::format("#%i %s", n, entryTemplate.c_str());
  56. return rapidjson::Value(buffer.data(), aznumeric_cast<rapidjson::SizeType>(buffer.size()), document.GetAllocator());
  57. };
  58. auto createEntry = [&](int n) -> rapidjson::Value
  59. {
  60. rapidjson::Value entry(rapidjson::kObjectType);
  61. entry.AddMember("string", createString(n), document.GetAllocator());
  62. entry.AddMember("int", rapidjson::Value(n), document.GetAllocator());
  63. entry.AddMember("double", rapidjson::Value(aznumeric_cast<double>(n) * 0.5), document.GetAllocator());
  64. entry.AddMember("bool", rapidjson::Value(n % 2 == 0), document.GetAllocator());
  65. entry.AddMember("null", rapidjson::Value(rapidjson::kNullType), document.GetAllocator());
  66. return entry;
  67. };
  68. auto createArray = [&]() -> rapidjson::Value
  69. {
  70. rapidjson::Value array;
  71. array.SetArray();
  72. for (int i = 0; i < entryCount; ++i)
  73. {
  74. array.PushBack(createEntry(i), document.GetAllocator());
  75. }
  76. return array;
  77. };
  78. auto createObject = [&]() -> rapidjson::Value
  79. {
  80. rapidjson::Value object;
  81. object.SetObject();
  82. for (int i = 0; i < entryCount; ++i)
  83. {
  84. buffer = AZStd::string::format("Key%i", i);
  85. rapidjson::Value key;
  86. key.SetString(buffer.data(), aznumeric_cast<rapidjson::SizeType>(buffer.length()), document.GetAllocator());
  87. object.AddMember(key.Move(), createArray(), document.GetAllocator());
  88. }
  89. return object;
  90. };
  91. document.SetObject();
  92. document.AddMember("entries", createObject(), document.GetAllocator());
  93. return document;
  94. }
  95. AZStd::string DomBenchmarkFixture::GenerateDomJsonBenchmarkPayload(int64_t entryCount, int64_t stringTemplateLength)
  96. {
  97. rapidjson::Document document = GenerateDomJsonBenchmarkDocument(entryCount, stringTemplateLength);
  98. AZStd::string serializedJson;
  99. auto result = AZ::JsonSerializationUtils::WriteJsonString(document, serializedJson);
  100. AZ_Assert(result.IsSuccess(), "Failed to serialize generated JSON");
  101. return serializedJson;
  102. }
  103. Value DomBenchmarkFixture::GenerateDomBenchmarkPayload(int64_t entryCount, int64_t stringTemplateLength)
  104. {
  105. Value root(Type::Object);
  106. AZStd::string entryTemplate;
  107. while (entryTemplate.size() < aznumeric_cast<size_t>(stringTemplateLength))
  108. {
  109. entryTemplate += "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor ";
  110. }
  111. entryTemplate.resize(stringTemplateLength);
  112. AZStd::string buffer;
  113. auto createString = [&](int n) -> Value
  114. {
  115. return Value(AZStd::string::format("#%i %s", n, entryTemplate.c_str()), true);
  116. };
  117. auto createEntry = [&](int n) -> Value
  118. {
  119. Value entry(Type::Object);
  120. entry.AddMember("string", createString(n));
  121. entry.AddMember("int", Value(n));
  122. entry.AddMember("double", Value(aznumeric_cast<double>(n) * 0.5));
  123. entry.AddMember("bool", Value(n % 2 == 0));
  124. entry.AddMember("null", Value(Type::Null));
  125. return entry;
  126. };
  127. auto createArray = [&]() -> Value
  128. {
  129. Value array(Type::Array);
  130. for (int i = 0; i < entryCount; ++i)
  131. {
  132. array.ArrayPushBack(createEntry(i));
  133. }
  134. return array;
  135. };
  136. auto createObject = [&]() -> Value
  137. {
  138. Value object;
  139. object.SetObject();
  140. for (int i = 0; i < entryCount; ++i)
  141. {
  142. buffer = AZStd::string::format("Key%i", i);
  143. object.AddMember(AZ::Name(buffer), createArray());
  144. }
  145. return object;
  146. };
  147. root["entries"] = createObject();
  148. return root;
  149. }
  150. void DomTestFixture::SetUp()
  151. {
  152. UnitTest::LeakDetectionFixture::SetUp();
  153. SetUpHarness();
  154. }
  155. void DomTestFixture::TearDown()
  156. {
  157. TearDownHarness();
  158. UnitTest::LeakDetectionFixture::TearDown();
  159. }
  160. } // namespace AZ::Dom::Tests