DomFixtures.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. // note that the `this` pointer is going to be a singleton, but this function gets called once per thread
  25. // and is done overlapping with other threads calling the same function on the same `this` pointer, so
  26. // do things that are thread-safe here, and only initialize things once.
  27. UnitTest::AllocatorsBenchmarkFixture::SetUp(st);
  28. if (st.thread_index() == 0)
  29. {
  30. SetUpHarness();
  31. }
  32. }
  33. void DomBenchmarkFixture::SetUp(::benchmark::State& st)
  34. {
  35. UnitTest::AllocatorsBenchmarkFixture::SetUp(st);
  36. if (st.thread_index() == 0)
  37. {
  38. SetUpHarness();
  39. }
  40. }
  41. void DomBenchmarkFixture::TearDown(::benchmark::State& st)
  42. {
  43. if (st.thread_index() == 0)
  44. {
  45. TearDownHarness();
  46. }
  47. UnitTest::AllocatorsBenchmarkFixture::TearDown(st);
  48. }
  49. void DomBenchmarkFixture::TearDown(const ::benchmark::State& st)
  50. {
  51. if (st.thread_index() == 0)
  52. {
  53. TearDownHarness();
  54. }
  55. UnitTest::AllocatorsBenchmarkFixture::TearDown(st);
  56. }
  57. rapidjson::Document DomBenchmarkFixture::GenerateDomJsonBenchmarkDocument(int64_t entryCount, int64_t stringTemplateLength)
  58. {
  59. rapidjson::Document document;
  60. document.SetObject();
  61. AZStd::string entryTemplate;
  62. while (entryTemplate.size() < aznumeric_cast<size_t>(stringTemplateLength))
  63. {
  64. entryTemplate += "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor ";
  65. }
  66. entryTemplate.resize(stringTemplateLength);
  67. AZStd::string buffer;
  68. auto createString = [&](int n) -> rapidjson::Value
  69. {
  70. buffer = AZStd::string::format("#%i %s", n, entryTemplate.c_str());
  71. return rapidjson::Value(buffer.data(), aznumeric_cast<rapidjson::SizeType>(buffer.size()), document.GetAllocator());
  72. };
  73. auto createEntry = [&](int n) -> rapidjson::Value
  74. {
  75. rapidjson::Value entry(rapidjson::kObjectType);
  76. entry.AddMember("string", createString(n), document.GetAllocator());
  77. entry.AddMember("int", rapidjson::Value(n), document.GetAllocator());
  78. entry.AddMember("double", rapidjson::Value(aznumeric_cast<double>(n) * 0.5), document.GetAllocator());
  79. entry.AddMember("bool", rapidjson::Value(n % 2 == 0), document.GetAllocator());
  80. entry.AddMember("null", rapidjson::Value(rapidjson::kNullType), document.GetAllocator());
  81. return entry;
  82. };
  83. auto createArray = [&]() -> rapidjson::Value
  84. {
  85. rapidjson::Value array;
  86. array.SetArray();
  87. for (int i = 0; i < entryCount; ++i)
  88. {
  89. array.PushBack(createEntry(i), document.GetAllocator());
  90. }
  91. return array;
  92. };
  93. auto createObject = [&]() -> rapidjson::Value
  94. {
  95. rapidjson::Value object;
  96. object.SetObject();
  97. for (int i = 0; i < entryCount; ++i)
  98. {
  99. buffer = AZStd::string::format("Key%i", i);
  100. rapidjson::Value key;
  101. key.SetString(buffer.data(), aznumeric_cast<rapidjson::SizeType>(buffer.length()), document.GetAllocator());
  102. object.AddMember(key.Move(), createArray(), document.GetAllocator());
  103. }
  104. return object;
  105. };
  106. document.SetObject();
  107. document.AddMember("entries", createObject(), document.GetAllocator());
  108. return document;
  109. }
  110. AZStd::string DomBenchmarkFixture::GenerateDomJsonBenchmarkPayload(int64_t entryCount, int64_t stringTemplateLength)
  111. {
  112. rapidjson::Document document = GenerateDomJsonBenchmarkDocument(entryCount, stringTemplateLength);
  113. AZStd::string serializedJson;
  114. auto result = AZ::JsonSerializationUtils::WriteJsonString(document, serializedJson);
  115. AZ_Assert(result.IsSuccess(), "Failed to serialize generated JSON");
  116. return serializedJson;
  117. }
  118. Value DomBenchmarkFixture::GenerateDomBenchmarkPayload(int64_t entryCount, int64_t stringTemplateLength)
  119. {
  120. Value root(Type::Object);
  121. AZStd::string entryTemplate;
  122. while (entryTemplate.size() < aznumeric_cast<size_t>(stringTemplateLength))
  123. {
  124. entryTemplate += "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor ";
  125. }
  126. entryTemplate.resize(stringTemplateLength);
  127. AZStd::string buffer;
  128. auto createString = [&](int n) -> Value
  129. {
  130. return Value(AZStd::string::format("#%i %s", n, entryTemplate.c_str()), true);
  131. };
  132. auto createEntry = [&](int n) -> Value
  133. {
  134. Value entry(Type::Object);
  135. entry.AddMember("string", createString(n));
  136. entry.AddMember("int", Value(n));
  137. entry.AddMember("double", Value(aznumeric_cast<double>(n) * 0.5));
  138. entry.AddMember("bool", Value(n % 2 == 0));
  139. entry.AddMember("null", Value(Type::Null));
  140. return entry;
  141. };
  142. auto createArray = [&]() -> Value
  143. {
  144. Value array(Type::Array);
  145. for (int i = 0; i < entryCount; ++i)
  146. {
  147. array.ArrayPushBack(createEntry(i));
  148. }
  149. return array;
  150. };
  151. auto createObject = [&]() -> Value
  152. {
  153. Value object;
  154. object.SetObject();
  155. for (int i = 0; i < entryCount; ++i)
  156. {
  157. buffer = AZStd::string::format("Key%i", i);
  158. object.AddMember(AZ::Name(buffer), createArray());
  159. }
  160. return object;
  161. };
  162. root["entries"] = createObject();
  163. return root;
  164. }
  165. void DomTestFixture::SetUp()
  166. {
  167. UnitTest::LeakDetectionFixture::SetUp();
  168. SetUpHarness();
  169. }
  170. void DomTestFixture::TearDown()
  171. {
  172. TearDownHarness();
  173. UnitTest::LeakDetectionFixture::TearDown();
  174. }
  175. } // namespace AZ::Dom::Tests