Skip to content

Add benchmark for URLPattern #917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions benchmarks/bbc_bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ std::string url_examples[] = {
"20220908-1153-091014d07889c842a7bdc06e00fa711c9e04f049/modules/vendor/"
"bower/modernizr/modernizr.js"};

std::vector<std::pair<std::string_view, std::string>> url_pattern_examples = {
{"https://example.com/foo/bar", "/foo/bar"},
{"https://example.com/foo/bar/baz", "/foo/bar"},
{"https://example.com/foo.html", ":name.html"},
{"https://sub.example.com/foo/bar",
"http{s}?://{*.}?example.com/:product/:endpoint"},
{"https://example.com/?foo", "https://example.com?foo"},
{"https://example.com:8080/?foo", "https://example.com:8080?foo"},
{"https://example.com/?foo", "https://example.com/*\\?foo"},
{"https://example.com/bar?foo", "https://example.com/:name?foo"}};

void init_data(const char* v = nullptr) {}

double url_examples_bytes = []() -> double {
Expand Down
11 changes: 11 additions & 0 deletions benchmarks/bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ std::string url_examples_default[] = {

std::vector<std::string> url_examples;

std::vector<std::pair<std::string_view, std::string>> url_pattern_examples = {
{"https://example.com/foo/bar", "/foo/bar"},
{"https://example.com/foo/bar/baz", "/foo/bar"},
{"https://example.com/foo.html", ":name.html"},
{"https://sub.example.com/foo/bar",
"http{s}?://{*.}?example.com/:product/:endpoint"},
{"https://example.com/?foo", "https://example.com?foo"},
{"https://example.com:8080/?foo", "https://example.com:8080?foo"},
{"https://example.com/?foo", "https://example.com/*\\?foo"},
{"https://example.com/bar?foo", "https://example.com/:name?foo"}};

double url_examples_bytes = []() -> double {
size_t bytes{0};
for (std::string& url_string : url_examples) {
Expand Down
64 changes: 64 additions & 0 deletions benchmarks/benchmark_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,70 @@ static void BasicBench_ServoUrl(benchmark::State& state) {
BENCHMARK(BasicBench_ServoUrl);
#endif // ADA_RUST

static void BasicBench_AdaURL_URLPattern(benchmark::State& state) {
// volatile to prevent optimizations.
volatile size_t success = 0;

for (auto _ : state) {
for (const auto& [base, input] : url_pattern_examples) {
auto result =
ada::parse_url_pattern<ada::url_pattern_regex::std_regex_provider>(
input, &base, nullptr);
if (result) {
success++;
}
}
}
if (collector.has_events()) {
event_aggregate aggregate{};
for (size_t i = 0; i < N; i++) {
std::atomic_thread_fence(std::memory_order_acquire);
collector.start();
for (const auto& [base, input] : url_pattern_examples) {
auto result =
ada::parse_url_pattern<ada::url_pattern_regex::std_regex_provider>(
input, &base, nullptr);
if (result) {
success++;
}
}
std::atomic_thread_fence(std::memory_order_release);
event_count allocate_count = collector.end();
aggregate << allocate_count;
}
state.counters["cycles/url"] =
aggregate.best.cycles() / std::size(url_pattern_examples);
state.counters["instructions/url"] =
aggregate.best.instructions() / std::size(url_pattern_examples);
state.counters["instructions/cycle"] =
aggregate.best.instructions() / aggregate.best.cycles();
state.counters["instructions/byte"] =
aggregate.best.instructions() / url_examples_bytes;
state.counters["instructions/ns"] =
aggregate.best.instructions() / aggregate.best.elapsed_ns();
state.counters["GHz"] =
aggregate.best.cycles() / aggregate.best.elapsed_ns();
state.counters["ns/url"] =
aggregate.best.elapsed_ns() / std::size(url_pattern_examples);
state.counters["cycle/byte"] = aggregate.best.cycles() / url_examples_bytes;
}
(void)success;
state.counters["time/byte"] = benchmark::Counter(
url_examples_bytes, benchmark::Counter::kIsIterationInvariantRate |
benchmark::Counter::kInvert);
state.counters["time/url"] =
benchmark::Counter(double(std::size(url_pattern_examples)),
benchmark::Counter::kIsIterationInvariantRate |
benchmark::Counter::kInvert);
state.counters["speed"] = benchmark::Counter(
url_examples_bytes, benchmark::Counter::kIsIterationInvariantRate);
state.counters["url/s"] =
benchmark::Counter(double(std::size(url_pattern_examples)),
benchmark::Counter::kIsIterationInvariantRate);
}

BENCHMARK(BasicBench_AdaURL_URLPattern);

int main(int argc, char** argv) {
if (argc > 1 && file_exists(argv[1])) {
init_data(argv[1]);
Expand Down
Loading