Element.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 "../Common/TestsInterface.h"
  30. #include <RmlUi/Core/Context.h>
  31. #include <RmlUi/Core/Element.h>
  32. #include <RmlUi/Core/ElementDocument.h>
  33. #include <RmlUi/Core/Types.h>
  34. #include <doctest.h>
  35. #include <nanobench.h>
  36. using namespace ankerl;
  37. using namespace Rml;
  38. static String GenerateRml(const int num_rows)
  39. {
  40. static nanobench::Rng rng;
  41. Rml::String rml;
  42. rml.reserve(1000 * num_rows);
  43. for (int i = 0; i < num_rows; i++)
  44. {
  45. int index = rng() % 1000;
  46. int route = rng() % 50;
  47. int max = (rng() % 40) + 10;
  48. int value = rng() % max;
  49. Rml::String rml_row = Rml::CreateString(1000, R"(
  50. <div class="row">
  51. <div class="col col1"><button class="expand" index="%d">+</button>&nbsp;<a>Route %d</a></div>
  52. <div class="col col23"><input type="range" class="assign_range" min="0" max="%d" value="%d"/></div>
  53. <div class="col col4">Assigned</div>
  54. <select>
  55. <option>Red</option><option>Blue</option><option selected>Green</option><option style="background-color: yellow;">Yellow</option>
  56. </select>
  57. <div class="inrow unmark_collapse">
  58. <div class="col col123 assign_text">Assign to route</div>
  59. <div class="col col4">
  60. <input type="submit" class="vehicle_depot_assign_confirm" quantity="0">Confirm</input>
  61. </div>
  62. </div>
  63. </div>)",
  64. index,
  65. route,
  66. max,
  67. value
  68. );
  69. rml += rml_row;
  70. }
  71. return rml;
  72. }
  73. TEST_CASE("Elements (shell)")
  74. {
  75. Context* context = TestsShell::GetMainContext();
  76. REQUIRE(context);
  77. ElementDocument* document = context->LoadDocument("basic/benchmark/data/benchmark.rml");
  78. REQUIRE(document);
  79. document->Show();
  80. Element* el = document->GetElementById("performance");
  81. REQUIRE(el);
  82. nanobench::Bench bench;
  83. bench.title("Elements (shell)");
  84. bench.relative(true);
  85. constexpr int num_rows = 50;
  86. const String rml = GenerateRml(num_rows);
  87. el->SetInnerRML(rml);
  88. context->Update();
  89. context->Render();
  90. bench.run("Update (unmodified)", [&] {
  91. context->Update();
  92. });
  93. bench.run("Render", [&] {
  94. TestsShell::PrepareRenderBuffer();
  95. context->Render();
  96. TestsShell::PresentRenderBuffer();
  97. });
  98. bench.run("SetInnerRML", [&] {
  99. el->SetInnerRML(rml);
  100. });
  101. bench.run("SetInnerRML + Update", [&] {
  102. el->SetInnerRML(rml);
  103. context->Update();
  104. });
  105. bench.run("SetInnerRML + Update + Render", [&] {
  106. el->SetInnerRML(rml);
  107. context->Update();
  108. TestsShell::PrepareRenderBuffer();
  109. context->Render();
  110. TestsShell::PresentRenderBuffer();
  111. });
  112. document->Close();
  113. }
  114. TEST_CASE("Elements (dummy interface)")
  115. {
  116. TestsRenderInterface render_interface;
  117. Context* context = TestsShell::CreateContext("element_dummy", &render_interface);
  118. REQUIRE(context);
  119. ElementDocument* document = context->LoadDocument("basic/benchmark/data/benchmark.rml");
  120. REQUIRE(document);
  121. document->Show();
  122. Element* el = document->GetElementById("performance");
  123. REQUIRE(el);
  124. nanobench::Bench bench;
  125. bench.title("Elements (dummy interface)");
  126. bench.relative(true);
  127. constexpr int num_rows = 50;
  128. const String rml = GenerateRml(num_rows);
  129. el->SetInnerRML(rml);
  130. context->Update();
  131. context->Render();
  132. bench.run("Update (unmodified)", [&] {
  133. context->Update();
  134. });
  135. bench.run("Render", [&] {
  136. context->Render();
  137. });
  138. bench.run("SetInnerRML", [&] {
  139. el->SetInnerRML(rml);
  140. });
  141. bench.run("SetInnerRML + Update", [&] {
  142. el->SetInnerRML(rml);
  143. context->Update();
  144. });
  145. bench.run("SetInnerRML + Update + Render", [&] {
  146. el->SetInnerRML(rml);
  147. context->Update();
  148. context->Render();
  149. });
  150. render_interface.ResetCounters();
  151. context->Render();
  152. auto& counters = render_interface.GetCounters();
  153. const String msg = CreateString(256,
  154. "Stats for single Context::Render() with n=%d rows: \n"
  155. "Render calls: %d\n"
  156. "Scissor enable: %d\n"
  157. "Scissor set: %d\n"
  158. "Texture load: %d\n"
  159. "Texture generate: %d\n"
  160. "Texture release: %d\n"
  161. "Transform set: %d\n",
  162. num_rows,
  163. counters.render_calls,
  164. counters.enable_scissor,
  165. counters.set_scissor,
  166. counters.load_texture,
  167. counters.generate_texture,
  168. counters.release_texture,
  169. counters.set_transform
  170. );
  171. MESSAGE(msg);
  172. document->Close();
  173. TestsShell::RemoveContext(context);
  174. }
  175. TEST_CASE("Elements asymptotic complexity (dummy interface)")
  176. {
  177. TestsRenderInterface render_interface;
  178. Context* context = TestsShell::CreateContext("element_complexity", &render_interface);
  179. REQUIRE(context);
  180. ElementDocument* document = context->LoadDocument("basic/benchmark/data/benchmark.rml");
  181. REQUIRE(document);
  182. document->Show();
  183. Element* el = document->GetElementById("performance");
  184. REQUIRE(el);
  185. struct BenchDef {
  186. const char* title;
  187. Function<void(const String& rml)> run;
  188. };
  189. Vector<BenchDef> bench_list = {
  190. {
  191. "SetInnerRML",
  192. [&](const String& rml) {
  193. el->SetInnerRML(rml);
  194. }
  195. },
  196. {
  197. "Update (unmodified)",
  198. [&](const String& /*rml*/) {
  199. context->Update();
  200. }
  201. },
  202. {
  203. "Render",
  204. [&](const String& /*rml*/) {
  205. context->Render();
  206. }
  207. },
  208. {
  209. "SetInnerRML + Update",
  210. [&](const String& rml) {
  211. el->SetInnerRML(rml);
  212. context->Update();
  213. }
  214. },
  215. {
  216. "SetInnerRML + Update + Render",
  217. [&](const String& rml) {
  218. el->SetInnerRML(rml);
  219. context->Update();
  220. context->Render();
  221. }
  222. },
  223. };
  224. for (auto& bench_def : bench_list)
  225. {
  226. nanobench::Bench bench;
  227. bench.title(bench_def.title);
  228. bench.relative(true);
  229. // Running the benchmark multiple times, with different number of rows.
  230. for (const int num_rows : { 1, 2, 5, 10, 20, 50, 100, 200, 500 })
  231. {
  232. const String rml = GenerateRml(num_rows);
  233. el->SetInnerRML(rml);
  234. context->Update();
  235. context->Render();
  236. bench.complexityN(num_rows).run(bench_def.title, [&]() {
  237. bench_def.run(rml);
  238. });
  239. }
  240. #ifdef RMLUI_BENCHMARKS_SHOW_COMPLEXITY
  241. MESSAGE(bench.complexityBigO());
  242. #endif
  243. }
  244. TestsShell::RemoveContext(context);
  245. }