Element.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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,
  101. route,
  102. max,
  103. value
  104. );
  105. rml += rml_row;
  106. }
  107. return rml;
  108. }
  109. TEST_CASE("element.creation_and_destruction")
  110. {
  111. Context* context = TestsShell::GetContext();
  112. REQUIRE(context);
  113. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  114. REQUIRE(document);
  115. document->Show();
  116. Element* el = document->GetElementById("performance");
  117. REQUIRE(el);
  118. constexpr int num_rows = 50;
  119. const String rml = GenerateRml(num_rows);
  120. el->SetInnerRML(rml);
  121. context->Update();
  122. context->Render();
  123. TestsShell::RenderLoop();
  124. String msg = Rml::CreateString(128, "\nElement construction and destruction of %d total elements.\n", GetNumDescendentElements(el));
  125. msg += TestsShell::GetRenderStats();
  126. MESSAGE(msg);
  127. nanobench::Bench bench;
  128. bench.title("Element");
  129. bench.timeUnit(std::chrono::microseconds(1), "us");
  130. bench.relative(true);
  131. bench.run("Update (unmodified)", [&] {
  132. context->Update();
  133. });
  134. bench.run("Render", [&] {
  135. context->Render();
  136. });
  137. bench.run("SetInnerRML", [&] {
  138. el->SetInnerRML(rml);
  139. });
  140. bench.run("SetInnerRML + Update", [&] {
  141. el->SetInnerRML(rml);
  142. context->Update();
  143. });
  144. bench.run("SetInnerRML + Update + Render", [&] {
  145. el->SetInnerRML(rml);
  146. context->Update();
  147. context->Render();
  148. });
  149. document->Close();
  150. }
  151. TEST_CASE("element.asymptotic_complexity")
  152. {
  153. Context* context = TestsShell::GetContext();
  154. REQUIRE(context);
  155. ElementDocument* document = context->LoadDocument("basic/benchmark/data/benchmark.rml");
  156. REQUIRE(document);
  157. document->Show();
  158. Element* el = document->GetElementById("performance");
  159. REQUIRE(el);
  160. struct BenchDef {
  161. const char* title;
  162. Function<void(const String& rml)> run;
  163. };
  164. Vector<BenchDef> bench_list = {
  165. {
  166. "SetInnerRML",
  167. [&](const String& rml) {
  168. el->SetInnerRML(rml);
  169. }
  170. },
  171. {
  172. "Update (unmodified)",
  173. [&](const String& /*rml*/) {
  174. context->Update();
  175. }
  176. },
  177. {
  178. "Render",
  179. [&](const String& /*rml*/) {
  180. context->Render();
  181. }
  182. },
  183. {
  184. "SetInnerRML + Update",
  185. [&](const String& rml) {
  186. el->SetInnerRML(rml);
  187. context->Update();
  188. }
  189. },
  190. {
  191. "SetInnerRML + Update + Render",
  192. [&](const String& rml) {
  193. el->SetInnerRML(rml);
  194. context->Update();
  195. context->Render();
  196. }
  197. },
  198. };
  199. for (auto& bench_def : bench_list)
  200. {
  201. nanobench::Bench bench;
  202. bench.title(bench_def.title);
  203. bench.timeUnit(std::chrono::microseconds(1), "us");
  204. bench.relative(true);
  205. // Running the benchmark multiple times, with different number of rows.
  206. for (const int num_rows : { 1, 2, 5, 10, 20, 50, 100, 200, 500 })
  207. {
  208. const String rml = GenerateRml(num_rows);
  209. el->SetInnerRML(rml);
  210. context->Update();
  211. context->Render();
  212. bench.complexityN(num_rows).run(bench_def.title, [&]() {
  213. bench_def.run(rml);
  214. });
  215. }
  216. #if defined(RMLUI_BENCHMARKS_SHOW_COMPLEXITY) || 0
  217. MESSAGE(bench.complexityBigO());
  218. #endif
  219. }
  220. document->Close();
  221. }