Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2006-2008 The Chromium Authors |
Yuri Wiitala | dd88438 | 2019-01-16 19:38:33 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/strings/string_piece.h" |
| 6 | #include "base/test/perf_time_logger.h" |
| 7 | #include "testing/gtest/include/gtest/gtest.h" |
| 8 | #include "url/gurl.h" |
| 9 | #include "url/third_party/mozilla/url_parse.h" |
| 10 | #include "url/url_canon.h" |
| 11 | #include "url/url_canon_stdstring.h" |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | TEST(URLParse, FullURL) { |
| 16 | constexpr base::StringPiece kUrl = |
| 17 | "http://me:pass@host/foo/bar.html;param?query=yes#ref"; |
| 18 | |
| 19 | url::Parsed parsed; |
| 20 | base::PerfTimeLogger timer("Full_URL_Parse_AMillion"); |
| 21 | |
| 22 | for (int i = 0; i < 1000000; i++) |
| 23 | url::ParseStandardURL(kUrl.data(), kUrl.size(), &parsed); |
| 24 | timer.Done(); |
| 25 | } |
| 26 | |
| 27 | constexpr base::StringPiece kTypicalUrl1 = |
| 28 | "http://www.google.com/" |
| 29 | "search?q=url+parsing&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:" |
| 30 | "official&client=firefox-a"; |
| 31 | |
| 32 | constexpr base::StringPiece kTypicalUrl2 = |
| 33 | "http://www.amazon.com/Stephen-King-Thrillers-Horror-People/dp/0766012336/" |
| 34 | "ref=sr_1_2/133-4144931-4505264?ie=UTF8&s=books&qid=2144880915&sr=8-2"; |
| 35 | |
| 36 | constexpr base::StringPiece kTypicalUrl3 = |
| 37 | "http://store.apple.com/1-800-MY-APPLE/WebObjects/AppleStore.woa/wa/" |
| 38 | "RSLID?nnmm=browse&mco=578E9744&node=home/desktop/mac_pro"; |
| 39 | |
| 40 | TEST(URLParse, TypicalURLParse) { |
| 41 | url::Parsed parsed1; |
| 42 | url::Parsed parsed2; |
| 43 | url::Parsed parsed3; |
| 44 | |
| 45 | // Do this 1/3 of a million times since we do 3 different URLs. |
| 46 | base::PerfTimeLogger parse_timer("Typical_URL_Parse_AMillion"); |
| 47 | for (int i = 0; i < 333333; i++) { |
| 48 | url::ParseStandardURL(kTypicalUrl1.data(), kTypicalUrl1.size(), &parsed1); |
| 49 | url::ParseStandardURL(kTypicalUrl2.data(), kTypicalUrl2.size(), &parsed2); |
| 50 | url::ParseStandardURL(kTypicalUrl3.data(), kTypicalUrl3.size(), &parsed3); |
| 51 | } |
| 52 | parse_timer.Done(); |
| 53 | } |
| 54 | |
| 55 | // Includes both parsing and canonicalization with no mallocs. |
| 56 | TEST(URLParse, TypicalURLParseCanon) { |
| 57 | url::Parsed parsed1; |
| 58 | url::Parsed parsed2; |
| 59 | url::Parsed parsed3; |
| 60 | |
| 61 | base::PerfTimeLogger canon_timer("Typical_Parse_Canon_AMillion"); |
| 62 | url::Parsed out_parsed; |
| 63 | url::RawCanonOutput<1024> output; |
| 64 | for (int i = 0; i < 333333; i++) { // divide by 3 so we get 1M |
| 65 | url::ParseStandardURL(kTypicalUrl1.data(), kTypicalUrl1.size(), &parsed1); |
| 66 | output.set_length(0); |
| 67 | url::CanonicalizeStandardURL( |
| 68 | kTypicalUrl1.data(), kTypicalUrl1.size(), parsed1, |
| 69 | url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, nullptr, &output, |
| 70 | &out_parsed); |
| 71 | |
| 72 | url::ParseStandardURL(kTypicalUrl2.data(), kTypicalUrl2.size(), &parsed2); |
| 73 | output.set_length(0); |
| 74 | url::CanonicalizeStandardURL( |
| 75 | kTypicalUrl2.data(), kTypicalUrl2.size(), parsed2, |
| 76 | url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, nullptr, &output, |
| 77 | &out_parsed); |
| 78 | |
| 79 | url::ParseStandardURL(kTypicalUrl3.data(), kTypicalUrl3.size(), &parsed3); |
| 80 | output.set_length(0); |
| 81 | url::CanonicalizeStandardURL( |
| 82 | kTypicalUrl3.data(), kTypicalUrl3.size(), parsed3, |
| 83 | url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, nullptr, &output, |
| 84 | &out_parsed); |
| 85 | } |
| 86 | canon_timer.Done(); |
| 87 | } |
| 88 | |
| 89 | // Includes both parsing and canonicalization, and mallocs for the output. |
| 90 | TEST(URLParse, TypicalURLParseCanonStdString) { |
| 91 | url::Parsed parsed1; |
| 92 | url::Parsed parsed2; |
| 93 | url::Parsed parsed3; |
| 94 | |
| 95 | base::PerfTimeLogger canon_timer("Typical_Parse_Canon_AMillion"); |
| 96 | url::Parsed out_parsed; |
| 97 | for (int i = 0; i < 333333; i++) { // divide by 3 so we get 1M |
| 98 | url::ParseStandardURL(kTypicalUrl1.data(), kTypicalUrl1.size(), &parsed1); |
| 99 | std::string out1; |
| 100 | url::StdStringCanonOutput output1(&out1); |
| 101 | url::CanonicalizeStandardURL( |
| 102 | kTypicalUrl1.data(), kTypicalUrl1.size(), parsed1, |
| 103 | url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, nullptr, &output1, |
| 104 | &out_parsed); |
| 105 | |
| 106 | url::ParseStandardURL(kTypicalUrl2.data(), kTypicalUrl2.size(), &parsed2); |
| 107 | std::string out2; |
| 108 | url::StdStringCanonOutput output2(&out2); |
| 109 | url::CanonicalizeStandardURL( |
| 110 | kTypicalUrl2.data(), kTypicalUrl2.size(), parsed2, |
| 111 | url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, nullptr, &output2, |
| 112 | &out_parsed); |
| 113 | |
| 114 | url::ParseStandardURL(kTypicalUrl3.data(), kTypicalUrl3.size(), &parsed3); |
| 115 | std::string out3; |
| 116 | url::StdStringCanonOutput output3(&out3); |
| 117 | url::CanonicalizeStandardURL( |
| 118 | kTypicalUrl3.data(), kTypicalUrl3.size(), parsed3, |
| 119 | url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, nullptr, &output3, |
| 120 | &out_parsed); |
| 121 | } |
| 122 | canon_timer.Done(); |
| 123 | } |
| 124 | |
| 125 | TEST(URLParse, GURL) { |
| 126 | base::PerfTimeLogger gurl_timer("Typical_GURL_AMillion"); |
| 127 | for (int i = 0; i < 333333; i++) { // divide by 3 so we get 1M |
| 128 | GURL gurl1(kTypicalUrl1); |
| 129 | GURL gurl2(kTypicalUrl2); |
| 130 | GURL gurl3(kTypicalUrl3); |
| 131 | } |
| 132 | gurl_timer.Done(); |
| 133 | } |
| 134 | |
| 135 | } // namespace |