Skip to content
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

Include A Zig Library In Speed Comparison #514

Merged
merged 5 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
13 changes: 13 additions & 0 deletions benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,19 @@ if(ADA_BOOST_URL)
endif(ADA_BOOST_URL)
endif(Boost_FOUND)

# Zuri
find_package(ZURI)
if(ZURI_FOUND)
message(STATUS "Zuri found")
target_link_libraries(bench PRIVATE zuri)
target_link_libraries(benchdata PRIVATE zuri)
target_link_libraries(bbc_bench PRIVATE zuri)
target_compile_definitions(bench PRIVATE ADA_ZURI_ENABLED=1)
target_compile_definitions(benchdata PRIVATE ADA_ZURI_ENABLED=1)
target_compile_definitions(bbc_bench PRIVATE ADA_ZURI_ENABLED=1)
else(ZURI_FOUND)
message(STATUS "Zuri not found! Please install to include in benchmark.")
endif(ZURI_FOUND)


# We want the check whether Rust is available before trying to build a crate.
Expand Down
108 changes: 107 additions & 1 deletion benchmarks/benchmark_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,101 @@ BENCHMARK(BasicBench_BoostURL);
// BENCHMARK(BasicBench_BoostURL_just_parse);
#endif // ADA_BOOST_ENABLED

#if ADA_ZURI_ENABLED
#include <zuri.h>

size_t count_zuri_invalid() {
size_t how_many = 0;
for (std::string& url_string : url_examples) {
struct zuri2k uri;
zuri_error err = zuri_parse2k(&uri, url_string.c_str(), url_string.size());
if (err) how_many++;
}
return how_many;
}

// ZURI follows RFC3986
template <bool just_parse = false>
static void BasicBench_ZURI(benchmark::State& state) {
// volatile to prevent optimizations.
volatile size_t success = 0;
volatile size_t href_size = 0;

for (auto _ : state) {
for (std::string& url_string : url_examples) {
struct zuri2k uri;
benchmark::DoNotOptimize(uri);
zuri_error err =
zuri_parse2k(&uri, url_string.c_str(), url_string.size());
if (!err) {
success++;
if constexpr (!just_parse) {
char buf[2048];
benchmark::DoNotOptimize(href_size +=
zuri_read2k(&uri, &buf[0], sizeof(buf)));
}
}
}
}
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 (std::string& url_string : url_examples) {
struct zuri2k uri;
benchmark::DoNotOptimize(uri);
zuri_error err =
zuri_parse2k(&uri, url_string.c_str(), url_string.size());
if (!err) {
success++;
if constexpr (!just_parse) {
char buf[2048];
benchmark::DoNotOptimize(href_size +=
zuri_read2k(&uri, &buf[0], sizeof(buf)));
}
}
}
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_examples);
state.counters["instructions/url"] =
aggregate.best.instructions() / std::size(url_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_examples);
state.counters["cycle/byte"] = aggregate.best.cycles() / url_examples_bytes;
}
(void)success;
(void)href_size;

state.counters["time/byte"] = benchmark::Counter(
url_examples_bytes, benchmark::Counter::kIsIterationInvariantRate |
benchmark::Counter::kInvert);
state.counters["time/url"] = benchmark::Counter(
std::size(url_examples), benchmark::Counter::kIsIterationInvariantRate |
benchmark::Counter::kInvert);
state.counters["speed"] = benchmark::Counter(
url_examples_bytes, benchmark::Counter::kIsIterationInvariantRate);
state.counters["url/s"] = benchmark::Counter(
std::size(url_examples), benchmark::Counter::kIsIterationInvariantRate);
}
BENCHMARK(BasicBench_ZURI);
// There is no need for 'just_parse' because BoostURL materializes the href.
// auto BasicBench_BoostURL_just_parse = BasicBench_BoostURL<JUST_PARSE>;
// BENCHMARK(BasicBench_BoostURL_just_parse);
#endif // ADA_ZURI_ENABLED

#if ADA_VARIOUS_COMPETITION_ENABLED
static void BasicBench_uriparser_just_parse(benchmark::State& state) {
// volatile to prevent optimizations.
Expand Down Expand Up @@ -658,6 +753,13 @@ int main(int argc, char** argv) {
"Boost URL follows RFC3986, not whatwg/url");
size_t boost_bad_url = count_boosturl_invalid();
#endif
#if ADA_ZURI_ENABLED
benchmark::AddCustomContext("zuri spec",
"Zuri follows RFC3986, not whatwg/url");
size_t zuri_bad_url = count_zuri_invalid();
#else
benchmark::AddCustomContext("zuri ", "OMITTED");
#endif
#if (__APPLE__ && __aarch64__) || defined(__linux__)
if (!collector.has_events()) {
benchmark::AddCustomContext("performance counters",
Expand Down Expand Up @@ -702,6 +804,10 @@ int main(int argc, char** argv) {
#if ADA_BOOST_ENABLED
badcounts << "boost-url---count of bad URLs " << std::to_string(boost_bad_url)
<< "\n";
#endif
#if ADA_ZURI_ENABLED
badcounts << "zuri---count of bad URLs " << std::to_string(zuri_bad_url)
<< "\n";
#endif
badcounts << "-------------------------------\n";
benchmark::AddCustomContext("bad urls", badcounts.str());
Expand All @@ -713,4 +819,4 @@ int main(int argc, char** argv) {
benchmark::Initialize(&argc, argv);
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
}
}
Loading