-
Notifications
You must be signed in to change notification settings - Fork 326
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add benchmark for element definition (applicable style rules lookup),…
… see #293
- Loading branch information
Showing
1 changed file
with
241 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
/* | ||
* This source file is part of RmlUi, the HTML/CSS Interface Middleware | ||
* | ||
* For the latest information, see http://github.com/mikke89/RmlUi | ||
* | ||
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd | ||
* Copyright (c) 2019 The RmlUi Team, and contributors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
*/ | ||
|
||
#include "../Common/TestsShell.h" | ||
#include <RmlUi/Core/Context.h> | ||
#include <RmlUi/Core/Element.h> | ||
#include <RmlUi/Core/ElementDocument.h> | ||
#include <RmlUi/Core/Types.h> | ||
#include <doctest.h> | ||
#include <nanobench.h> | ||
|
||
using namespace ankerl; | ||
using namespace Rml; | ||
|
||
static constexpr const char* document_rml_template = R"( | ||
<rml> | ||
<head> | ||
<title>Benchmark Sample</title> | ||
<link type="text/template" href="/assets/window.rml"/> | ||
<style> | ||
body | ||
{ | ||
font-family: LatoLatin; | ||
font-weight: normal; | ||
font-style: normal; | ||
font-size: 15dp; | ||
color: white; | ||
} | ||
body.window | ||
{ | ||
max-width: 2000px; | ||
max-height: 2000px; | ||
left: 100px; | ||
top: 50px; | ||
width: 1300px; | ||
height: 600px; | ||
} | ||
#performance | ||
{ | ||
width: 800px; | ||
height: 300px; | ||
} | ||
/* Insert generated style rules below */ | ||
%s | ||
</style> | ||
</head> | ||
<body template="window"> | ||
<div id="performance"/> | ||
</body> | ||
</rml> | ||
)"; | ||
|
||
static int GetNumDescendentElements(Element* element) | ||
{ | ||
const int num_children = element->GetNumChildren(true); | ||
int result = num_children; | ||
for (int i = 0; i < num_children; i++) | ||
{ | ||
result += GetNumDescendentElements(element->GetChild(i)); | ||
} | ||
return result; | ||
} | ||
|
||
static constexpr int num_rule_iterations = 10; | ||
|
||
static String GenerateRCSS(bool with_tag, bool with_id, bool with_class, bool with_child_div, String& out_rule_name) | ||
{ | ||
static_assert('a' < 'z' && 'a' + 25 == 'z', "Assumes ASCII characters"); | ||
|
||
auto GenerateRule = [=](const String& name) { | ||
String rule; | ||
if (!with_tag && !with_id && !with_class) | ||
rule += '*'; | ||
else | ||
{ | ||
if (with_tag) | ||
{ | ||
if (with_id || with_class) | ||
rule += "div"; | ||
else | ||
rule += name; | ||
} | ||
|
||
if (with_id) | ||
rule += '#' + name; | ||
if (with_class) | ||
rule += '.' + name; | ||
} | ||
if (with_child_div) | ||
rule += " div"; | ||
return rule; | ||
}; | ||
|
||
out_rule_name = GenerateRule("a"); | ||
|
||
String result; | ||
|
||
for (int i = 0; i < num_rule_iterations; i++) | ||
{ | ||
for (char c = 'a'; c <= 'z'; c++) | ||
{ | ||
const String name = c + ToString(i); | ||
result += GenerateRule(name); | ||
|
||
// Set a property that does not require a layout change | ||
result += CreateString(64, " { scrollbar-margin: %dpx; }\n", int(c - 'a') + 1); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
static String GenerateRml(const int num_rows) | ||
{ | ||
static nanobench::Rng rng; | ||
|
||
Rml::String rml; | ||
rml.reserve(1000 * num_rows); | ||
|
||
for (int i = 0; i < num_rows; i++) | ||
{ | ||
int index = rng() % 1000; | ||
int route = rng() % 50; | ||
int max = (rng() % 40) + 10; | ||
int value = rng() % max; | ||
String class_name_a = char('a' + char(rng() % 26)) + ToString(rng() % num_rule_iterations); | ||
String class_name_b = char('a' + char(rng() % 26)) + ToString(rng() % num_rule_iterations); | ||
Rml::String rml_row = Rml::CreateString(1000, R"( | ||
<div class="row"> | ||
<div class="col col1"><button class="expand" index="%d">+</button> <a>Route %d</a></div> | ||
<div class="col col23"><input type="range" class="assign_range" min="0" max="%d" value="%d"/></div> | ||
<div class="col col4 %s">Assigned</div> | ||
<select> | ||
<option>Red</option><option>Blue</option><option selected>Green</option><option style="background-color: yellow;">Yellow</option> | ||
</select> | ||
<div class="inrow unmark_collapse %s"> | ||
<div class="col col123 assign_text">Assign to route</div> | ||
<div class="col col4"> | ||
<input type="submit" class="vehicle_depot_assign_confirm" quantity="0">Confirm</input> | ||
</div> | ||
</div> | ||
</div>)", | ||
index, route, max, value, class_name_a.c_str(), class_name_b.c_str()); | ||
rml += rml_row; | ||
} | ||
|
||
return rml; | ||
} | ||
|
||
TEST_CASE("elementstyle") | ||
{ | ||
Context* context = TestsShell::GetContext(); | ||
REQUIRE(context); | ||
|
||
constexpr int num_rows = 50; | ||
const String rml = GenerateRml(num_rows); | ||
|
||
nanobench::Bench bench; | ||
bench.title("ElementStyle (rule name)"); | ||
bench.timeUnit(std::chrono::microseconds(1), "us"); | ||
bench.relative(true); | ||
bench.minEpochIterations(5); | ||
|
||
for (int i = 0; i <= 0b1111 + 1; i++) | ||
{ | ||
const bool reference = (i == 0); | ||
const int flags = i - 1; | ||
|
||
const bool with_tag = flags & (1 << 0); | ||
const bool with_id = flags & (1 << 1); | ||
const bool with_class = flags & (1 << 2); | ||
const bool long_names = flags & (1 << 3); | ||
|
||
String name = "Reference (no style rules)"; | ||
const String styles = reference ? "" : GenerateRCSS(with_tag, with_id, with_class, long_names, name); | ||
|
||
const String compiled_document_rml = Rml::CreateString(1000 + styles.size(), document_rml_template, styles.c_str()); | ||
|
||
ElementDocument* document = context->LoadDocumentFromMemory(compiled_document_rml); | ||
document->Show(); | ||
|
||
Element* el = document->GetElementById("performance"); | ||
el->SetInnerRML(rml); | ||
context->Update(); | ||
context->Render(); | ||
|
||
if (reference) | ||
{ | ||
String msg = Rml::CreateString(128, "\nElement update after pseudo class change with %d descendant elements and %d unique RCSS rules.", | ||
GetNumDescendentElements(el), num_rule_iterations * 26); | ||
MESSAGE(msg); | ||
|
||
bench.run("Reference (load document)", [&] { | ||
ElementDocument* new_document = context->LoadDocumentFromMemory(compiled_document_rml); | ||
new_document->Close(); | ||
context->Update(); | ||
}); | ||
bench.run(CreateString(100, "Reference (update unmodified)", name.c_str()), [&] { context->Update(); }); | ||
} | ||
|
||
bool hover_active = false; | ||
|
||
bench.run(name, [&] { | ||
hover_active = !hover_active; | ||
el->SetPseudoClass("hover", hover_active); | ||
context->Update(); | ||
}); | ||
|
||
document->Close(); | ||
context->Update(); | ||
} | ||
} |