ElementDocument.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/Factory.h>
  34. #include <RmlUi/Core/Types.h>
  35. #include <doctest.h>
  36. #include <nanobench.h>
  37. using namespace ankerl;
  38. using namespace Rml;
  39. static const String document_rml = R"(
  40. <rml>
  41. <head>
  42. <link type="text/template" href="/assets/window.rml"/>
  43. <title>Benchmark Sample</title>
  44. <style>
  45. body.window
  46. {
  47. left: 50px;
  48. top: 50px;
  49. width: 800px;
  50. height: 200px;
  51. }
  52. #performance
  53. {
  54. width: 500px;
  55. height: 300px;
  56. }
  57. </style>
  58. </head>
  59. <body template="window">
  60. <div id="performance">
  61. <div class="row">
  62. <div class="col col1"><button class="expand" index="3">+</button>&nbsp;<a>Route 15</a></div>
  63. <div class="col col23"><input type="range" class="assign_range" min="0" max="20" value="3"/></div>
  64. <div class="col col4">Assigned</div>
  65. <select>
  66. <option>Red</option><option>Blue</option><option selected>Green</option><option style="background-color: yellow;">Yellow</option>
  67. </select>
  68. <div class="inrow unmark_collapse">
  69. <div class="col col123 assign_text">Assign to route</div>
  70. <div class="col col4">
  71. <input type="submit" class="vehicle_depot_assign_confirm" quantity="0">Confirm</input>
  72. </div>
  73. </div>
  74. </div>
  75. </div>
  76. </body>
  77. </rml>
  78. )";
  79. TEST_CASE("elementdocument")
  80. {
  81. Context* context = TestsShell::GetContext();
  82. REQUIRE(context);
  83. {
  84. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  85. document->Show();
  86. const String stats = TestsShell::GetRenderStats();
  87. MESSAGE(stats);
  88. TestsShell::RenderLoop();
  89. document->Close();
  90. context->Update();
  91. }
  92. {
  93. nanobench::Bench bench;
  94. bench.title("ElementDocument");
  95. bench.timeUnit(std::chrono::microseconds(1), "us");
  96. bench.relative(true);
  97. bench.run("LoadDocument", [&] {
  98. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  99. document->Close();
  100. context->Update();
  101. });
  102. bench.run("LoadDocument + Show", [&] {
  103. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  104. document->Show();
  105. document->Close();
  106. context->Update();
  107. });
  108. bench.run("LoadDocument + Show + Update", [&] {
  109. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  110. document->Show();
  111. context->Update();
  112. document->Close();
  113. context->Update();
  114. });
  115. bench.run("LoadDocument + Show + Update + Render", [&] {
  116. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  117. document->Show();
  118. context->Update();
  119. TestsShell::PrepareRenderBuffer();
  120. context->Render();
  121. TestsShell::PresentRenderBuffer();
  122. document->Close();
  123. context->Update();
  124. });
  125. }
  126. {
  127. nanobench::Bench bench;
  128. bench.title("ElementDocument w/ClearStyleSheetCache");
  129. bench.timeUnit(std::chrono::microseconds(1), "us");
  130. bench.relative(true);
  131. bench.run("Clear + LoadDocument", [&] {
  132. Factory::ClearStyleSheetCache();
  133. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  134. document->Close();
  135. context->Update();
  136. });
  137. bench.run("Clear + LoadDocument + Show", [&] {
  138. Factory::ClearStyleSheetCache();
  139. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  140. document->Show();
  141. document->Close();
  142. context->Update();
  143. });
  144. bench.run("Clear + LoadDocument + Show + Update", [&] {
  145. Factory::ClearStyleSheetCache();
  146. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  147. document->Show();
  148. context->Update();
  149. document->Close();
  150. context->Update();
  151. });
  152. bench.run("Clear + LoadDocument + Show + Update + Render", [&] {
  153. Factory::ClearStyleSheetCache();
  154. ElementDocument* document = context->LoadDocumentFromMemory(document_rml);
  155. document->Show();
  156. context->Update();
  157. TestsShell::PrepareRenderBuffer();
  158. context->Render();
  159. TestsShell::PresentRenderBuffer();
  160. document->Close();
  161. context->Update();
  162. });
  163. }
  164. }