From a1ce9fc3606f01545a10268c08d53ff766eb7aba Mon Sep 17 00:00:00 2001 From: Robin Linden Date: Sat, 4 Jan 2025 05:02:54 +0100 Subject: [PATCH] style: Add a class-selector-matching benchmark --- style/BUILD | 18 ++++++++++++++++- style/style_bench.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 style/style_bench.cpp diff --git a/style/BUILD b/style/BUILD index aab959344..957930bf4 100644 --- a/style/BUILD +++ b/style/BUILD @@ -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, @@ -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"])] diff --git a/style/style_bench.cpp b/style/style_bench.cpp new file mode 100644 index 000000000..cbdeae9cf --- /dev/null +++ b/style/style_bench.cpp @@ -0,0 +1,46 @@ +// SPDX-FileCopyrightText: 2025 Robin Lindén +// +// SPDX-License-Identifier: BSD-2-Clause + +#include "style/style.h" + +#include "style/styled_node.h" + +#include "dom/dom.h" +#include "etest/etest2.h" + +#include + +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(); +}