7#include <unicode/uidna.h>
8#include <unicode/utf8.h>
9#include <unicode/utypes.h>
17#include <benchmark/benchmark.h>
22 "xn--mgba3gch31f060k",
25 "me\xc3\x9f\x61\x67\x65\x66\x61\x63\x74\x6f\x72\x79\x2e\x63"
31 for (std::string& url_string :
inputs) {
32 bytes += url_string.size();
37static void Ada(benchmark::State& state) {
38 for (
auto _ : state) {
39 for (std::string& url_string :
inputs) {
46 for (
size_t i = 0; i <
N; i++) {
47 std::atomic_thread_fence(std::memory_order_acquire);
49 for (std::string& url_string :
inputs) {
52 std::atomic_thread_fence(std::memory_order_release);
54 aggregate << allocate_count;
56 state.counters[
"cycles/domain"] =
58 state.counters[
"instructions/domain"] =
60 state.counters[
"instructions/cycle"] =
62 state.counters[
"instructions/byte"] =
64 state.counters[
"instructions/ns"] =
66 state.counters[
"GHz"] =
68 state.counters[
"ns/domain"] =
72 state.counters[
"time/byte"] = benchmark::Counter(
74 benchmark::Counter::kInvert);
75 state.counters[
"time/domain"] = benchmark::Counter(
76 double(std::size(
inputs)), benchmark::Counter::kIsIterationInvariantRate |
77 benchmark::Counter::kInvert);
78 state.counters[
"speed"] = benchmark::Counter(
80 state.counters[
"url/s"] = benchmark::Counter(
81 double(std::size(
inputs)), benchmark::Counter::kIsIterationInvariantRate);
89std::string icu_to_ascii(std::string_view input) {
90 static std::string error =
"";
92 std::string out(255, 0);
93 constexpr bool be_strict =
false;
95 UErrorCode status = U_ZERO_ERROR;
97 UIDNA_CHECK_BIDI | UIDNA_CHECK_CONTEXTJ | UIDNA_NONTRANSITIONAL_TO_ASCII;
100 options |= UIDNA_USE_STD3_RULES;
103 UIDNA* uidna = uidna_openUTS46(options, &status);
104 if (U_FAILURE(status)) {
108 UIDNAInfo info = UIDNA_INFO_INITIALIZER;
115 uidna_nameToASCII_UTF8(uidna, input.data(), int32_t(input.length()),
116 out.data(), 255, &info, &status);
118 if (status == U_BUFFER_OVERFLOW_ERROR) {
119 status = U_ZERO_ERROR;
123 uidna_nameToASCII_UTF8(uidna, input.data(), int32_t(input.length()),
124 out.data(), length, &info, &status);
128 info.errors &= ~UIDNA_ERROR_HYPHEN_3_4;
130 info.errors &= ~UIDNA_ERROR_LEADING_HYPHEN;
132 info.errors &= ~UIDNA_ERROR_TRAILING_HYPHEN;
136 info.errors &= ~UIDNA_ERROR_EMPTY_LABEL;
138 info.errors &= ~UIDNA_ERROR_LABEL_TOO_LONG;
140 info.errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG;
145 if (U_FAILURE(status) || info.errors != 0 || length == 0) {
153static void Icu(benchmark::State& state) {
154 for (
auto _ : state) {
155 for (std::string& url_string :
inputs) {
156 benchmark::DoNotOptimize(icu_to_ascii(url_string));
162 for (
size_t i = 0; i <
N; i++) {
163 std::atomic_thread_fence(std::memory_order_acquire);
165 for (std::string& url_string :
inputs) {
166 benchmark::DoNotOptimize(icu_to_ascii(url_string));
168 std::atomic_thread_fence(std::memory_order_release);
170 aggregate << allocate_count;
173 state.counters[
"instructions/url"] =
175 state.counters[
"instructions/cycle"] =
177 state.counters[
"instructions/byte"] =
179 state.counters[
"instructions/ns"] =
181 state.counters[
"GHz"] =
186 state.counters[
"time/byte"] = benchmark::Counter(
188 benchmark::Counter::kInvert);
189 state.counters[
"time/url"] = benchmark::Counter(
190 double(std::size(
inputs)), benchmark::Counter::kIsIterationInvariantRate |
191 benchmark::Counter::kInvert);
192 state.counters[
"speed"] = benchmark::Counter(
194 state.counters[
"url/s"] = benchmark::Counter(
195 double(std::size(
inputs)), benchmark::Counter::kIsIterationInvariantRate);
202 for (std::string& url_string :
inputs) {
203 std::string icu_answer = icu_to_ascii(url_string);
205 if (icu_answer != ada_answer) {
206 std::cerr <<
" ada/icu mismatch " << ada_answer <<
" vs. " << icu_answer
212 std::cout <<
"\n\n\nWarning: errors found.\n\n\n\n";
214 std::cout <<
"ICU and ada/idna agree on all test inputs.\n";
220int main(
int argc,
char** argv) {
224#if (__APPLE__ && __aarch64__) || defined(__linux__)
226 benchmark::AddCustomContext(
"performance counters",
227 "No privileged access (sudo may help).");
231 benchmark::AddCustomContext(
"performance counters",
"Unsupported system.");
234 benchmark::AddCustomContext(
"input bytes",
236 benchmark::AddCustomContext(
"number of domains",
237 std::to_string(std::size(
inputs)));
238 benchmark::AddCustomContext(
241 benchmark::AddCustomContext(
"performance counters",
"Enabled");
243 benchmark::Initialize(&argc, argv);
244 benchmark::RunSpecifiedBenchmarks();
245 benchmark::Shutdown();
static void Ada(benchmark::State &state)
int main(int argc, char **argv)
event_collector collector
std::string to_ascii(std::string_view ut8_string)
state
States in the URL parsing state machine.
double instructions() const
double elapsed_ns() const