OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| 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 <string> |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/history/history_service_factory.h" |
| 9 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_window.h" |
| 12 #include "chrome/browser/ui/location_bar/location_bar.h" |
| 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 14 #include "chrome/test/base/in_process_browser_test.h" |
| 15 #include "chrome/test/base/interactive_test_utils.h" |
| 16 #include "chrome/test/base/search_test_utils.h" |
| 17 #include "components/history/core/browser/history_service.h" |
| 18 #include "components/omnibox/browser/omnibox_popup_model.h" |
| 19 #include "components/omnibox/browser/omnibox_view.h" |
| 20 #include "components/search_engines/template_url_service.h" |
| 21 #include "testing/perf/scoped_histogram_result_printer.h" |
| 22 |
| 23 using base::Time; |
| 24 using base::TimeDelta; |
| 25 using history::HistoryService; |
| 26 |
| 27 namespace { |
| 28 |
| 29 constexpr char kPopularUrl[] = "http://long.popular_url_with.many_variations/"; |
| 30 |
| 31 struct TestHistoryEntry { |
| 32 std::string url; |
| 33 std::string title; |
| 34 int visit_count; |
| 35 int typed_count; |
| 36 }; |
| 37 |
| 38 void AddControllerHistograms(perf_test::ScopedHistogramResultPrinter* printer) { |
| 39 DCHECK(printer); |
| 40 std::string histogram_prefix("Omnibox.ProviderTime2."); |
| 41 std::string measurement_prefix("ProviderTime2."); |
| 42 |
| 43 printer->AddHistogramStrict(histogram_prefix + "HistoryURL", |
| 44 measurement_prefix + "HistoryURL"); |
| 45 printer->AddHistogramStrict(histogram_prefix + "HistoryQuick", |
| 46 measurement_prefix + "HistoryQuick"); |
| 47 printer->AddHistogramStrict("Omnibox.StartSearchToFirstResultsTime", |
| 48 "StartSearchToFirstResultsTime"); |
| 49 printer->AddHistogramStrict("Omnibox.HistoryIDSetFromWords", |
| 50 "HistoryIDSetFromWords"); |
| 51 } |
| 52 |
| 53 std::string GenerateSymbolsToChooseFrom() { |
| 54 std::string res; |
| 55 auto append_chars = [&res](char from, char to) { |
| 56 for (char ch = from; ch <= to; ++ch) |
| 57 res += ch; |
| 58 }; |
| 59 append_chars('a', 'z'); |
| 60 append_chars('A', 'Z'); |
| 61 append_chars('0', '9'); |
| 62 res += ",/=+?#"; |
| 63 return res; |
| 64 } |
| 65 |
| 66 std::string GenerateWeiredShortString(int seed) { |
| 67 CR_DEFINE_STATIC_LOCAL(std::string, syms, (GenerateSymbolsToChooseFrom())); |
| 68 std::string res; |
| 69 // Much like translating to another calculation system. |
| 70 for (auto i = std::hash<int>()(seed); i; i /= syms.size()) { |
| 71 res += syms[i % syms.size()]; |
| 72 } |
| 73 return res; |
| 74 } |
| 75 |
| 76 std::string GeneratePopularUrlVariation() { |
| 77 static long iteration = 0; |
| 78 std::string res = kPopularUrl; |
| 79 |
| 80 constexpr int kShortStringsCount = 5; |
| 81 for (int i = iteration; i < iteration + kShortStringsCount; ++i) |
| 82 res += GenerateWeiredShortString(i); |
| 83 |
| 84 ++iteration; |
| 85 return res; |
| 86 } |
| 87 |
| 88 } // namespace |
| 89 |
| 90 class OmniboxPerformanceTest : public InProcessBrowserTest { |
| 91 protected: |
| 92 void SetUpOnMainThread() override; |
| 93 void AddHistoryEntry(const TestHistoryEntry& entry, const Time& time); |
| 94 |
| 95 OmniboxView* omnibox_view_ = nullptr; |
| 96 AutocompleteController* ac_controller_ = nullptr; |
| 97 HistoryService* history_service_ = nullptr; |
| 98 }; |
| 99 |
| 100 void OmniboxPerformanceTest::SetUpOnMainThread() { |
| 101 omnibox_view_ = browser()->window()->GetLocationBar()->GetOmniboxView(); |
| 102 ASSERT_TRUE(omnibox_view_); |
| 103 |
| 104 ac_controller_ = |
| 105 omnibox_view_->model()->popup_model()->autocomplete_controller(); |
| 106 ASSERT_TRUE(ac_controller_); |
| 107 |
| 108 // History |
| 109 { |
| 110 Profile* profile = browser()->profile(); |
| 111 |
| 112 search_test_utils::WaitForTemplateURLServiceToLoad( |
| 113 TemplateURLServiceFactory::GetForProfile(profile)); |
| 114 |
| 115 history_service_ = HistoryServiceFactory::GetForProfile( |
| 116 profile, ServiceAccessType::EXPLICIT_ACCESS); |
| 117 ASSERT_TRUE(history_service_); |
| 118 |
| 119 ui_test_utils::WaitForHistoryToLoad(history_service_); |
| 120 ASSERT_TRUE(history_service_->BackendLoaded()); |
| 121 } |
| 122 } |
| 123 |
| 124 void OmniboxPerformanceTest::AddHistoryEntry(const TestHistoryEntry& entry, |
| 125 const Time& time) { |
| 126 history_service_->AddPageWithDetails( |
| 127 GURL(entry.url), base::UTF8ToUTF16(entry.title), entry.visit_count, |
| 128 entry.typed_count, time, false, history::SOURCE_BROWSED); |
| 129 } |
| 130 |
| 131 IN_PROC_BROWSER_TEST_F(OmniboxPerformanceTest, DeletingPopularUrl) { |
| 132 // Preparing histogram printer. |
| 133 perf_test::ScopedHistogramResultPrinter scoped_printer( |
| 134 perf_test::TestBasedPerfTestPrinter(::testing::UnitTest::GetInstance(), |
| 135 "ms")); |
| 136 AddControllerHistograms(&scoped_printer); |
| 137 |
| 138 // Generating urls. |
| 139 { |
| 140 constexpr std::size_t kNumberOfUrls = 10000; |
| 141 for (std::size_t i = 0; i < kNumberOfUrls; ++i) { |
| 142 constexpr int kVisitCount = 1; |
| 143 constexpr int kTypedCount = 1; |
| 144 auto new_entry = GeneratePopularUrlVariation(); |
| 145 AddHistoryEntry( |
| 146 { |
| 147 new_entry, "Page: " + std::to_string(i), kVisitCount, kTypedCount, |
| 148 }, |
| 149 base::Time::Now() - base::TimeDelta::FromHours(i)); |
| 150 } |
| 151 } |
| 152 |
| 153 const std::string test_url = GeneratePopularUrlVariation(); |
| 154 |
| 155 omnibox_view_->SetFocus(); |
| 156 omnibox_view_->SelectAll(true); |
| 157 omnibox_view_->SetUserText(base::UTF8ToUTF16(test_url)); |
| 158 ui_test_utils::WaitForAutocompleteController(*ac_controller_); |
| 159 |
| 160 for (size_t i = test_url.length(); i != 0; --i) |
| 161 EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_BACK, false, |
| 162 false, false, false)); |
| 163 } |
OLD | NEW |