Element.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../Common/TestsShell.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/Element.h>
  31. #include <RmlUi/Core/ElementDocument.h>
  32. #include <RmlUi/Core/Types.h>
  33. #include <doctest.h>
  34. #include <nanobench.h>
  35. using namespace ankerl;
  36. using namespace Rml;
  37. static const String document_rml = R"(
  38. <rml>
  39. <head>
  40. <link type="text/template" href="/assets/window.rml"/>
  41. <title>Benchmark Sample</title>
  42. <style>
  43. body.window
  44. {
  45. max-width: 2000px;
  46. max-height: 2000px;
  47. left: 100px;
  48. top: 50px;
  49. width: 1300px;
  50. height: 600px;
  51. }
  52. #performance
  53. {
  54. width: 800px;
  55. height: 300px;
  56. }
  57. </style>
  58. </head>
  59. <body template="window">
  60. <div id="performance"/>
  61. </body>
  62. </rml>
  63. )";
  64. static int GetNumDescendentElements(Element* element)
  65. {
  66. const int num_children = element->GetNumChildren(true);
  67. int result = num_children;
  68. for (int i = 0; i < num_children; i++)
  69. {
  70. result += GetNumDescendentElements(element->GetChild(i));
  71. }
  72. return result;
  73. }
  74. static String GenerateRml(const int num_rows)
  75. {
  76. static nanobench::Rng rng;
  77. Rml::String rml;
  78. rml.reserve(1000 * num_rows);
  79. for (int i = 0; i < num_rows; i++)
  80. {
  81. int index = rng() % 1000;
  82. int route = rng() % 50;
  83. int max = (rng() % 40) + 10;
  84. int value = rng() % max;
  85. Rml::String rml_row = Rml::CreateString(1000, R"(
  86. <div class="row">
  87. <div class="col col1"><button class="expand" index="%d">+</button>&nbsp;<a>Route %d</a></div>
  88. <div class="col col23"><input type="range" class="assign_range" min="0" max="%d" value="%d"/></div>
  89. <div class="col col4">Assigned</div>
  90. <select>
  91. <option>Red</option><option>Blue</option><option selected>Green</option><option style="background-color: yellow;">Yellow</option>
  92. </select>
  93. <div class="inrow unmark_collapse">
  94. <div class="col col123 assign_text">Assign to route</div>
  95. <div class="col col4">
  96. <input type="submit" class="vehicle_depot_assign_confirm" quantity="0">Confirm</input>
  97. </div>
  98. </div>
  99. </div>)",
  100. index, route, max, value);
  101. rml += rml_row;
  102. }
  103. return rml;
  104. }
  105. TEST_CASE("element.creation_and_destruction")
  106. {
  107. Context* context = TestsShell::GetContext();
  108. REQUIRE(context);
  109. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  110. REQUIRE(document);
  111. document->Show();
  112. Element* el = document->GetElementById("performance");
  113. REQUIRE(el);
  114. constexpr int num_rows = 50;
  115. const String rml = GenerateRml(num_rows);
  116. el->SetInnerRML(rml);
  117. context->Update();
  118. context->Render();
  119. TestsShell::RenderLoop();
  120. String msg = Rml::CreateString(128, "\nElement construction and destruction of %d total elements.\n", GetNumDescendentElements(el));
  121. msg += TestsShell::GetRenderStats();
  122. MESSAGE(msg);
  123. nanobench::Bench bench;
  124. bench.title("Element");
  125. bench.timeUnit(std::chrono::microseconds(1), "us");
  126. bench.relative(true);
  127. bench.run("Update (unmodified)", [&] { context->Update(); });
  128. bool hover_toggle = true;
  129. auto child = el->GetChild(num_rows / 2);
  130. bench.run("Update (hover child)", [&] {
  131. static nanobench::Rng rng;
  132. child->SetPseudoClass(":hover", hover_toggle);
  133. hover_toggle = !hover_toggle;
  134. context->Update();
  135. });
  136. bench.run("Update (hover)", [&] {
  137. el->SetPseudoClass(":hover", hover_toggle);
  138. hover_toggle = !hover_toggle;
  139. context->Update();
  140. });
  141. bench.run("Render", [&] { context->Render(); });
  142. bench.run("SetInnerRML", [&] { el->SetInnerRML(rml); });
  143. bench.run("SetInnerRML + Update", [&] {
  144. el->SetInnerRML(rml);
  145. context->Update();
  146. });
  147. bench.run("SetInnerRML + Update + Render", [&] {
  148. el->SetInnerRML(rml);
  149. context->Update();
  150. context->Render();
  151. });
  152. document->Close();
  153. }
  154. TEST_CASE("element.asymptotic_complexity")
  155. {
  156. Context* context = TestsShell::GetContext();
  157. REQUIRE(context);
  158. ElementDocument* document = context->LoadDocument("basic/benchmark/data/benchmark.rml");
  159. REQUIRE(document);
  160. document->Show();
  161. Element* el = document->GetElementById("performance");
  162. REQUIRE(el);
  163. struct BenchDef {
  164. const char* title;
  165. Function<void(const String& rml)> run;
  166. };
  167. Vector<BenchDef> bench_list = {
  168. {"SetInnerRML", [&](const String& rml) { el->SetInnerRML(rml); }},
  169. {"Update (unmodified)", [&](const String& /*rml*/) { context->Update(); }},
  170. {"Render", [&](const String& /*rml*/) { context->Render(); }},
  171. {"SetInnerRML + Update",
  172. [&](const String& rml) {
  173. el->SetInnerRML(rml);
  174. context->Update();
  175. }},
  176. {"SetInnerRML + Update + Render",
  177. [&](const String& rml) {
  178. el->SetInnerRML(rml);
  179. context->Update();
  180. context->Render();
  181. }},
  182. };
  183. for (auto& bench_def : bench_list)
  184. {
  185. nanobench::Bench bench;
  186. bench.title(bench_def.title);
  187. bench.timeUnit(std::chrono::microseconds(1), "us");
  188. bench.relative(true);
  189. // Running the benchmark multiple times, with different number of rows.
  190. for (const int num_rows : {1, 2, 5, 10, 20, 50, 100, 200, 500})
  191. {
  192. const String rml = GenerateRml(num_rows);
  193. el->SetInnerRML(rml);
  194. context->Update();
  195. context->Render();
  196. bench.complexityN(num_rows).run(bench_def.title, [&]() { bench_def.run(rml); });
  197. }
  198. #if defined(RMLUI_BENCHMARKS_SHOW_COMPLEXITY) || 0
  199. MESSAGE(bench.complexityBigO());
  200. #endif
  201. }
  202. document->Close();
  203. }