Skip to content

Commit

Permalink
style: Add a class-selector-matching benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Jan 5, 2025
1 parent 4d512e3 commit a1ce9fc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
18 changes: 17 additions & 1 deletion style/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ cc_library(
name = "style",
srcs = glob(
include = ["*.cpp"],
exclude = ["*_test.cpp"],
exclude = [
"*_test.cpp",
"*_bench.cpp",
],
),
hdrs = glob(["*.h"]),
copts = HASTUR_COPTS,
Expand Down Expand Up @@ -33,3 +36,16 @@ cc_library(
"//gfx",
],
) for src in glob(["*_test.cpp"])]

[cc_test(
name = src.removesuffix(".cpp"),
size = "small",
srcs = [src],
copts = HASTUR_COPTS,
deps = [
":style",
"//dom",
"//etest",
"@nanobench",
],
) for src in glob(["*_bench.cpp"])]
46 changes: 46 additions & 0 deletions style/style_bench.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-FileCopyrightText: 2025 Robin Lindén <[email protected]>
//
// SPDX-License-Identifier: BSD-2-Clause

#include "style/style.h"

#include "style/styled_node.h"

#include "dom/dom.h"
#include "etest/etest2.h"

#include <nanobench.h>

int main() {
etest::Suite s;

s.add_test("is_match: class", [](etest::IActions const &) {
ankerl::nanobench::Bench bench;
bench.title("is_match: class");

dom::Node few_classes_dom = dom::Element{"div", {{"class", "first second"}}};
auto few_classes = style::StyledNode{.node = few_classes_dom};
bench.run("match, few classes", [&] {
style::is_match(few_classes, ".first.second"); //
});

bench.run("no match, few classes", [&] {
style::is_match(few_classes, ".first.second.third.fourth"); //
});

dom::Node many_classes_dom = dom::Element{
"div",
{{"class", "one two three four five six seven eight nine ten"}},
};
auto many_classes = style::StyledNode{.node = many_classes_dom};
bench.run("match, many classes", [&] {
style::is_match(many_classes, ".eight.two.seven.ten"); //
});

bench.run("no match, many classes", [&] {
style::is_match(many_classes, ".eight.two.seve.ten"); //
});
});

return s.run();
}

0 comments on commit a1ce9fc

Please sign in to comment.