Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 "chrome/browser/site_details.h" | |
| 6 | |
| 7 #include "base/bind_helpers.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "base/path_service.h" | |
| 10 #include "base/test/histogram_tester.h" | |
| 11 #include "chrome/browser/metrics/metrics_memory_details.h" | |
| 12 #include "chrome/browser/ui/browser.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/ui_test_utils.h" | |
| 16 #include "content/public/browser/notification_service.h" | |
| 17 #include "content/public/test/browser_test_utils.h" | |
| 18 #include "content/public/test/test_utils.h" | |
| 19 #include "net/dns/mock_host_resolver.h" | |
| 20 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 21 #include "testing/gmock/include/gmock/gmock.h" | |
| 22 | |
| 23 using base::Bucket; | |
| 24 using testing::ContainerEq; | |
| 25 using testing::ElementsAre; | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 class TestMemoryDetails : public MetricsMemoryDetails { | |
| 30 public: | |
| 31 TestMemoryDetails() | |
| 32 : MetricsMemoryDetails(base::Bind(&base::DoNothing), nullptr) {} | |
| 33 | |
| 34 void StartFetchAndWait() { | |
| 35 uma_.reset(new base::HistogramTester()); | |
| 36 StartFetch(FROM_CHROME_ONLY); | |
| 37 content::RunMessageLoop(); | |
| 38 } | |
| 39 | |
| 40 // Returns a HistogramTester which observed the most recent call to | |
| 41 // StartFetchAndWait(). | |
| 42 base::HistogramTester* uma() { return uma_.get(); } | |
| 43 | |
| 44 private: | |
| 45 ~TestMemoryDetails() override {} | |
| 46 | |
| 47 void OnDetailsAvailable() override { | |
| 48 MetricsMemoryDetails::OnDetailsAvailable(); | |
| 49 // Exit the loop initiated by StartFetchAndWait(). | |
| 50 base::MessageLoop::current()->Quit(); | |
| 51 } | |
| 52 | |
| 53 scoped_ptr<base::HistogramTester> uma_; | |
| 54 | |
| 55 DISALLOW_COPY_AND_ASSIGN(TestMemoryDetails); | |
| 56 }; | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 class SiteDetailsBrowserTest : public InProcessBrowserTest { | |
| 61 public: | |
| 62 SiteDetailsBrowserTest() {} | |
| 63 ~SiteDetailsBrowserTest() override {} | |
| 64 | |
| 65 void SetUpOnMainThread() override { | |
| 66 host_resolver()->AddRule("*", "127.0.0.1"); | |
| 67 | |
| 68 // Add content/test/data so we can use cross_site_iframe_factory.html | |
| 69 base::FilePath test_data_dir; | |
| 70 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &test_data_dir)); | |
| 71 embedded_test_server()->ServeFilesFromDirectory( | |
| 72 test_data_dir.AppendASCII("content/test/data/")); | |
| 73 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | |
| 74 } | |
| 75 | |
| 76 // The expected number of non-renderer processes, which is platform dependent. | |
| 77 int other_process_count() { | |
|
dcheng
2015/08/07 20:03:50
nit: const
ncarter (slow)
2015/08/07 20:19:26
Done.
| |
| 78 #if defined(OS_LINUX) | |
| 79 return 3; // Browser, GPU, Zygote. | |
| 80 #else | |
| 81 return 2; // Browser, GPU. | |
| 82 #endif | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 DISALLOW_COPY_AND_ASSIGN(SiteDetailsBrowserTest); | |
| 87 }; | |
| 88 | |
| 89 // Test the accuracy of SiteDetails process estimation, in the presence of | |
| 90 // multiple iframes, navigation, multiple BrowsingInstances, and multiple tabs | |
| 91 // in the same BrowsingInstance. | |
| 92 IN_PROC_BROWSER_TEST_F(SiteDetailsBrowserTest, ManyCrossSiteIframes) { | |
| 93 // Page with 14 nested oopifs across 9 sites (a.com through i.com). | |
| 94 // None of these are https. | |
| 95 GURL abcdefghi_url = embedded_test_server()->GetURL( | |
| 96 "a.com", | |
| 97 "/cross_site_iframe_factory.html?a(b(a(b,c,d,e,f,g,h)),c,d,e,i(f))"); | |
| 98 ui_test_utils::NavigateToURL(browser(), abcdefghi_url); | |
| 99 | |
| 100 // Get the metrics. | |
| 101 scoped_refptr<TestMemoryDetails> details = new TestMemoryDetails(); | |
| 102 details->StartFetchAndWait(); | |
| 103 | |
| 104 EXPECT_THAT( | |
| 105 details->uma()->GetAllSamples("SiteIsolation.BrowsingInstanceCount"), | |
| 106 ElementsAre(Bucket(1, 1))); | |
| 107 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 108 "SiteIsolation.CurrentRendererProcessCount"), | |
| 109 ElementsAre(Bucket(1, 1))); | |
| 110 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 111 "SiteIsolation.IsolateAllSitesProcessCountEstimate"), | |
| 112 ElementsAre(Bucket(9, 1))); | |
| 113 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 114 "SiteIsolation.IsolateAllSitesProcessCountLowerBound"), | |
| 115 ElementsAre(Bucket(9, 1))); | |
| 116 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 117 "SiteIsolation.IsolateAllSitesProcessCountNoLimit"), | |
| 118 ElementsAre(Bucket(9, 1))); | |
| 119 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 120 "SiteIsolation.IsolateAllSitesTotalProcessCountEstimate"), | |
| 121 ElementsAre(Bucket(9 + other_process_count(), 1))); | |
| 122 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 123 "SiteIsolation.IsolateHttpsSitesProcessCountEstimate"), | |
| 124 ElementsAre(Bucket(1, 1))); | |
| 125 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 126 "SiteIsolation.IsolateHttpsSitesProcessCountLowerBound"), | |
| 127 ElementsAre(Bucket(1, 1))); | |
| 128 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 129 "SiteIsolation.IsolateHttpsSitesProcessCountNoLimit"), | |
| 130 ElementsAre(Bucket(1, 1))); | |
| 131 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 132 "SiteIsolation.IsolateHttpsSitesTotalProcessCountEstimate"), | |
| 133 ElementsAre(Bucket(1 + other_process_count(), 1))); | |
| 134 | |
| 135 // Navigate to a different, disjoint set of 7 sites. | |
| 136 GURL pqrstuv_url = embedded_test_server()->GetURL( | |
| 137 "p.com", | |
| 138 "/cross_site_iframe_factory.html?p(q(r),r(s),s(t),t(q),u(u),v(p))"); | |
| 139 ui_test_utils::NavigateToURL(browser(), pqrstuv_url); | |
| 140 | |
| 141 details = new TestMemoryDetails(); | |
| 142 details->StartFetchAndWait(); | |
| 143 | |
| 144 EXPECT_THAT( | |
| 145 details->uma()->GetAllSamples("SiteIsolation.BrowsingInstanceCount"), | |
| 146 ElementsAre(Bucket(1, 1))); | |
| 147 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 148 "SiteIsolation.CurrentRendererProcessCount"), | |
| 149 ElementsAre(Bucket(1, 1))); | |
| 150 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 151 "SiteIsolation.IsolateAllSitesProcessCountEstimate"), | |
| 152 ElementsAre(Bucket(7, 1))); | |
| 153 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 154 "SiteIsolation.IsolateAllSitesProcessCountLowerBound"), | |
| 155 ElementsAre(Bucket(7, 1))); | |
| 156 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 157 "SiteIsolation.IsolateAllSitesProcessCountNoLimit"), | |
| 158 ElementsAre(Bucket(7, 1))); | |
| 159 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 160 "SiteIsolation.IsolateAllSitesTotalProcessCountEstimate"), | |
| 161 ElementsAre(Bucket(7 + other_process_count(), 1))); | |
| 162 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 163 "SiteIsolation.IsolateHttpsSitesProcessCountEstimate"), | |
| 164 ElementsAre(Bucket(1, 1))); | |
| 165 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 166 "SiteIsolation.IsolateHttpsSitesProcessCountLowerBound"), | |
| 167 ElementsAre(Bucket(1, 1))); | |
| 168 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 169 "SiteIsolation.IsolateHttpsSitesProcessCountNoLimit"), | |
| 170 ElementsAre(Bucket(1, 1))); | |
| 171 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 172 "SiteIsolation.IsolateHttpsSitesTotalProcessCountEstimate"), | |
| 173 ElementsAre(Bucket(1 + other_process_count(), 1))); | |
| 174 | |
| 175 // Open a second tab (different BrowsingInstance) with 4 sites (a through d). | |
| 176 GURL abcd_url = embedded_test_server()->GetURL( | |
| 177 "a.com", "/cross_site_iframe_factory.html?a(b(c(d())))"); | |
| 178 AddTabAtIndex(1, abcd_url, ui::PAGE_TRANSITION_TYPED); | |
| 179 | |
| 180 details = new TestMemoryDetails(); | |
| 181 details->StartFetchAndWait(); | |
| 182 | |
| 183 EXPECT_THAT( | |
| 184 details->uma()->GetAllSamples("SiteIsolation.BrowsingInstanceCount"), | |
| 185 ElementsAre(Bucket(2, 1))); | |
| 186 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 187 "SiteIsolation.CurrentRendererProcessCount"), | |
| 188 ElementsAre(Bucket(2, 1))); | |
| 189 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 190 "SiteIsolation.IsolateAllSitesProcessCountEstimate"), | |
| 191 ElementsAre(Bucket(11, 1))); | |
| 192 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 193 "SiteIsolation.IsolateAllSitesProcessCountLowerBound"), | |
| 194 ElementsAre(Bucket(11, 1))); | |
| 195 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 196 "SiteIsolation.IsolateAllSitesProcessCountNoLimit"), | |
| 197 ElementsAre(Bucket(11, 1))); | |
| 198 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 199 "SiteIsolation.IsolateAllSitesTotalProcessCountEstimate"), | |
| 200 ElementsAre(Bucket(11 + other_process_count(), 1))); | |
| 201 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 202 "SiteIsolation.IsolateHttpsSitesProcessCountEstimate"), | |
| 203 ElementsAre(Bucket(2, 1))); | |
| 204 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 205 "SiteIsolation.IsolateHttpsSitesProcessCountLowerBound"), | |
| 206 ElementsAre(Bucket(1, 1))); // TODO(nick): This should be 2. | |
| 207 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 208 "SiteIsolation.IsolateHttpsSitesProcessCountNoLimit"), | |
| 209 ElementsAre(Bucket(2, 1))); | |
| 210 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 211 "SiteIsolation.IsolateHttpsSitesTotalProcessCountEstimate"), | |
| 212 ElementsAre(Bucket(2 + other_process_count(), 1))); | |
| 213 | |
| 214 // Open a third tab (different BrowsingInstance) with the same 4 sites. | |
| 215 AddTabAtIndex(2, abcd_url, ui::PAGE_TRANSITION_TYPED); | |
| 216 | |
| 217 details = new TestMemoryDetails(); | |
| 218 details->StartFetchAndWait(); | |
| 219 | |
| 220 EXPECT_THAT( | |
| 221 details->uma()->GetAllSamples("SiteIsolation.BrowsingInstanceCount"), | |
| 222 ElementsAre(Bucket(3, 1))); | |
| 223 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 224 "SiteIsolation.CurrentRendererProcessCount"), | |
| 225 ElementsAre(Bucket(3, 1))); | |
| 226 // Could be 11 if subframe processes were reused across BrowsingInstances. | |
| 227 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 228 "SiteIsolation.IsolateAllSitesProcessCountEstimate"), | |
| 229 ElementsAre(Bucket(15, 1))); | |
| 230 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 231 "SiteIsolation.IsolateAllSitesProcessCountLowerBound"), | |
| 232 ElementsAre(Bucket(11, 1))); | |
| 233 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 234 "SiteIsolation.IsolateAllSitesProcessCountNoLimit"), | |
| 235 ElementsAre(Bucket(15, 1))); | |
| 236 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 237 "SiteIsolation.IsolateAllSitesTotalProcessCountEstimate"), | |
| 238 ElementsAre(Bucket(15 + other_process_count(), 1))); | |
| 239 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 240 "SiteIsolation.IsolateHttpsSitesProcessCountEstimate"), | |
| 241 ElementsAre(Bucket(3, 1))); | |
| 242 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 243 "SiteIsolation.IsolateHttpsSitesProcessCountLowerBound"), | |
| 244 ElementsAre(Bucket(1, 1))); // TODO(nick): This should be 3. | |
| 245 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 246 "SiteIsolation.IsolateHttpsSitesProcessCountNoLimit"), | |
| 247 ElementsAre(Bucket(3, 1))); | |
| 248 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 249 "SiteIsolation.IsolateHttpsSitesTotalProcessCountEstimate"), | |
| 250 ElementsAre(Bucket(3 + other_process_count(), 1))); | |
| 251 | |
| 252 // Do a window.open() to obtain a fourth tab in the same BrowsingInstance as | |
| 253 // the third tab. The new page uses the same four sites "a-d" as third tab, | |
| 254 // plus an additional site "e". The estimated process counts should increase | |
| 255 // by one (not five) from the previous result, since the new tab can reuse the | |
| 256 // four processes already in the BrowsingInstance. | |
| 257 GURL dcbae_url = embedded_test_server()->GetURL( | |
| 258 "a.com", "/cross_site_iframe_factory.html?d(c(b(a(e))))"); | |
| 259 ui_test_utils::UrlLoadObserver load_complete( | |
| 260 dcbae_url, content::NotificationService::AllSources()); | |
| 261 ASSERT_EQ(3, browser()->tab_strip_model()->count()); | |
| 262 ASSERT_TRUE(content::ExecuteScript( | |
| 263 browser()->tab_strip_model()->GetActiveWebContents(), | |
| 264 "window.open('" + dcbae_url.spec() + "');")); | |
| 265 ASSERT_EQ(4, browser()->tab_strip_model()->count()); | |
| 266 load_complete.Wait(); | |
| 267 | |
| 268 details = new TestMemoryDetails(); | |
| 269 details->StartFetchAndWait(); | |
| 270 | |
| 271 EXPECT_THAT( | |
| 272 details->uma()->GetAllSamples("SiteIsolation.BrowsingInstanceCount"), | |
| 273 ElementsAre(Bucket(3, 1))); | |
| 274 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 275 "SiteIsolation.CurrentRendererProcessCount"), | |
| 276 ElementsAre(Bucket(3, 1))); | |
| 277 // Could be 11 if subframe processes were reused across BrowsingInstances. | |
| 278 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 279 "SiteIsolation.IsolateAllSitesProcessCountEstimate"), | |
| 280 ElementsAre(Bucket(16, 1))); | |
| 281 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 282 "SiteIsolation.IsolateAllSitesProcessCountLowerBound"), | |
| 283 ElementsAre(Bucket(12, 1))); | |
| 284 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 285 "SiteIsolation.IsolateAllSitesProcessCountNoLimit"), | |
| 286 ElementsAre(Bucket(16, 1))); | |
| 287 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 288 "SiteIsolation.IsolateAllSitesTotalProcessCountEstimate"), | |
| 289 ElementsAre(Bucket(16 + other_process_count(), 1))); | |
| 290 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 291 "SiteIsolation.IsolateHttpsSitesProcessCountEstimate"), | |
| 292 ElementsAre(Bucket(3, 1))); | |
| 293 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 294 "SiteIsolation.IsolateHttpsSitesProcessCountLowerBound"), | |
| 295 ElementsAre(Bucket(1, 1))); // TODO(nick): This should be 3. | |
| 296 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 297 "SiteIsolation.IsolateHttpsSitesProcessCountNoLimit"), | |
| 298 ElementsAre(Bucket(3, 1))); | |
| 299 EXPECT_THAT(details->uma()->GetAllSamples( | |
| 300 "SiteIsolation.IsolateHttpsSitesTotalProcessCountEstimate"), | |
| 301 ElementsAre(Bucket(3 + other_process_count(), 1))); | |
| 302 } | |
| OLD | NEW |