1 #include "gtest/gtest.h"
2 #include "gtest/MozGTestBench.h" // For MOZ_GTEST_BENCH
3 #include "gtest/BlackBox.h"
8 #include "mozilla/Encoding.h"
10 #define TEST_COUNT 10000
12 class TestIDNA
: public ::testing::Test
{
14 void SetUp() override
{
15 // Intentionally Assign and not AssignLiteral
16 // to simulate the heap case.
17 mPlainASCII
.Assign("example.com");
18 mLeadingDigitASCII
.Assign("1test.example");
19 mUnicodeMixed
.Assign("مثال.example");
20 mPunycodeMixed
.Assign("xn--mgbh0fb.example");
21 mUnicodeLTR
.Assign("නම.උදාහරණ");
22 mPunycodeLTR
.Assign("xn--r0co.xn--ozc8dl2c3bxd");
23 mUnicodeRTL
.Assign("الاسم.مثال");
24 mPunycodeRTL
.Assign("xn--mgba0b1dh.xn--mgbh0fb");
25 // Intentionally not assigning to mEmpty
27 // For measuring the case inside nsStandardURL
28 mUrlPlainASCII
.Assign("https://example.com/");
29 mUrlLeadingDigitASCII
.Assign("https://1test.example/");
30 mUrlUnicodeMixed
.Assign("https://مثال.example/");
31 mUrlPunycodeMixed
.Assign("https://xn--mgbh0fb.example/");
32 mUrlUnicodeLTR
.Assign("https://නම.උදාහරණ/");
33 mUrlPunycodeLTR
.Assign("https://xn--r0co.xn--ozc8dl2c3bxd/");
34 mUrlUnicodeRTL
.Assign("https://الاسم.مثال/");
35 mUrlPunycodeRTL
.Assign("https://xn--mgba0b1dh.xn--mgbh0fb/");
39 nsCString mPlainASCII
;
40 nsCString mLeadingDigitASCII
;
41 nsCString mUnicodeMixed
;
42 nsCString mPunycodeMixed
;
43 nsCString mUnicodeLTR
;
44 nsCString mPunycodeLTR
;
45 nsCString mUnicodeRTL
;
46 nsCString mPunycodeRTL
;
47 nsCString mEmpty
; // Extremely suspicious measurement!
48 nsCString mUrlPlainASCII
;
49 nsCString mUrlLeadingDigitASCII
;
50 nsCString mUrlUnicodeMixed
;
51 nsCString mUrlPunycodeMixed
;
52 nsCString mUrlUnicodeLTR
;
53 nsCString mUrlPunycodeLTR
;
54 nsCString mUrlUnicodeRTL
;
55 nsCString mUrlPunycodeRTL
;
58 #define IDNA_ITERATIONS 50000
60 #define IDNA_BENCH(name, func, src) \
61 MOZ_GTEST_BENCH_F(TestIDNA, name, [this] { \
62 for (int i = 0; i < IDNA_ITERATIONS; i++) { \
64 func(*mozilla::BlackBox(&src), *mozilla::BlackBox(&dst)); \
68 #define IDNA_URL_BENCH(name, src) \
69 MOZ_GTEST_BENCH_F(TestIDNA, name, [this] { \
70 for (int i = 0; i < IDNA_ITERATIONS; i++) { \
71 nsCOMPtr<nsIURI> dst; \
72 nsresult rv = NS_NewURI(getter_AddRefs(dst), *mozilla::BlackBox(&src), \
74 ASSERT_EQ(NS_OK, rv); \
75 mozilla::BlackBox(&dst); \
79 IDNA_BENCH(BenchToASCIIPlainASCII
, NS_DomainToASCII
, mPlainASCII
);
80 IDNA_BENCH(BenchToASCIILeadingDigitASCII
, NS_DomainToASCII
, mLeadingDigitASCII
);
81 IDNA_BENCH(BenchToASCIIUnicodeMixed
, NS_DomainToASCII
, mUnicodeMixed
);
82 IDNA_BENCH(BenchToASCIIPunycodeMixed
, NS_DomainToASCII
, mPunycodeMixed
);
83 IDNA_BENCH(BenchToASCIIUnicodeLTR
, NS_DomainToASCII
, mUnicodeLTR
);
84 IDNA_BENCH(BenchToASCIIPunycodeLTR
, NS_DomainToASCII
, mPunycodeLTR
);
85 IDNA_BENCH(BenchToASCIIUnicodeRTL
, NS_DomainToASCII
, mUnicodeRTL
);
86 IDNA_BENCH(BenchToASCIIPunycodeRTL
, NS_DomainToASCII
, mPunycodeRTL
);
87 IDNA_BENCH(BenchToASCIIEmpty
, NS_DomainToASCII
, mEmpty
);
89 IDNA_BENCH(BenchToDisplayPlainASCII
, NS_DomainToDisplay
, mPlainASCII
);
90 IDNA_BENCH(BenchToDisplayLeadingDigitASCII
, NS_DomainToDisplay
,
92 IDNA_BENCH(BenchToDisplayUnicodeMixed
, NS_DomainToDisplay
, mUnicodeMixed
);
93 IDNA_BENCH(BenchToDisplayPunycodeMixed
, NS_DomainToDisplay
, mPunycodeMixed
);
94 IDNA_BENCH(BenchToDisplayUnicodeLTR
, NS_DomainToDisplay
, mUnicodeLTR
);
95 IDNA_BENCH(BenchToDisplayPunycodeLTR
, NS_DomainToDisplay
, mPunycodeLTR
);
96 IDNA_BENCH(BenchToDisplayUnicodeRTL
, NS_DomainToDisplay
, mUnicodeRTL
);
97 IDNA_BENCH(BenchToDisplayPunycodeRTL
, NS_DomainToDisplay
, mPunycodeRTL
);
98 IDNA_BENCH(BenchToDisplayEmpty
, NS_DomainToDisplay
, mEmpty
);
100 IDNA_BENCH(BenchToUnicodePlainASCII
, NS_DomainToUnicode
, mPlainASCII
);
101 IDNA_BENCH(BenchToUnicodeLeadingDigitASCII
, NS_DomainToUnicode
,
103 IDNA_BENCH(BenchToUnicodeUnicodeMixed
, NS_DomainToUnicode
, mUnicodeMixed
);
104 IDNA_BENCH(BenchToUnicodePunycodeMixed
, NS_DomainToUnicode
, mPunycodeMixed
);
105 IDNA_BENCH(BenchToUnicodeUnicodeLTR
, NS_DomainToUnicode
, mUnicodeLTR
);
106 IDNA_BENCH(BenchToUnicodePunycodeLTR
, NS_DomainToUnicode
, mPunycodeLTR
);
107 IDNA_BENCH(BenchToUnicodeUnicodeRTL
, NS_DomainToUnicode
, mUnicodeRTL
);
108 IDNA_BENCH(BenchToUnicodePunycodeRTL
, NS_DomainToUnicode
, mPunycodeRTL
);
109 IDNA_BENCH(BenchToUnicodeEmpty
, NS_DomainToUnicode
, mEmpty
);
111 IDNA_URL_BENCH(BenchUrlPlainASCII
, mUrlPlainASCII
);
112 IDNA_URL_BENCH(BenchUrlLeadingDigitASCII
, mUrlLeadingDigitASCII
);
113 IDNA_URL_BENCH(BenchUrlUnicodeMixed
, mUrlUnicodeMixed
);
114 IDNA_URL_BENCH(BenchUrlPunycodeMixed
, mUrlPunycodeMixed
);
115 IDNA_URL_BENCH(BenchUrlUnicodeLTR
, mUrlUnicodeLTR
);
116 IDNA_URL_BENCH(BenchUrlPunycodeLTR
, mUrlPunycodeLTR
);
117 IDNA_URL_BENCH(BenchUrlUnicodeRTL
, mUrlUnicodeRTL
);
118 IDNA_URL_BENCH(BenchUrlPunycodeRTL
, mUrlPunycodeRTL
);