sessions_sync_perf_test.cc revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright (c) 2011 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 "base/strings/stringprintf.h"
6#include "chrome/browser/sync/profile_sync_service_harness.h"
7#include "chrome/browser/sync/test/integration/performance/sync_timing_helper.h"
8#include "chrome/browser/sync/test/integration/sessions_helper.h"
9#include "chrome/browser/sync/test/integration/sync_test.h"
10#include "chrome/browser/ui/browser.h"
11#include "chrome/browser/ui/browser_commands.h"
12#include "chrome/browser/ui/tabs/tab_strip_model.h"
13
14using content::OpenURLParams;
15using sessions_helper::GetLocalSession;
16using sessions_helper::GetSessionData;
17using sessions_helper::OpenMultipleTabs;
18using sessions_helper::SyncedSessionVector;
19using sessions_helper::SessionWindowMap;
20using sessions_helper::WaitForTabsToLoad;
21
22static const int kNumTabs = 150;
23
24class SessionsSyncPerfTest: public SyncTest {
25 public:
26  SessionsSyncPerfTest() : SyncTest(TWO_CLIENT), url_number_(0) {}
27
28  // Opens |num_tabs| new tabs on |profile|.
29  void AddTabs(int profile, int num_tabs);
30
31  // Update all tabs in |profile| by visiting a new URL.
32  void UpdateTabs(int profile);
33
34  // Close all tabs in |profile|.
35  void RemoveTabs(int profile);
36
37  // Returns the number of open tabs in all sessions (local + foreign) for
38  // |profile|.  Returns -1 on failure.
39  int GetTabCount(int profile);
40
41 private:
42  // Returns a new unique URL.
43  GURL NextURL();
44
45  // Returns a unique URL according to the integer |n|.
46  GURL IntToURL(int n);
47
48  int url_number_;
49  DISALLOW_COPY_AND_ASSIGN(SessionsSyncPerfTest);
50};
51
52void SessionsSyncPerfTest::AddTabs(int profile, int num_tabs) {
53  std::vector<GURL> urls;
54  for (int i = 0; i < num_tabs; ++i) {
55    urls.push_back(NextURL());
56  }
57  OpenMultipleTabs(profile, urls);
58}
59
60void SessionsSyncPerfTest::UpdateTabs(int profile) {
61  Browser* browser = GetBrowser(profile);
62  GURL url;
63  std::vector<GURL> urls;
64  for (int i = 0; i < browser->tab_strip_model()->count(); ++i) {
65    chrome::SelectNumberedTab(browser, i);
66    url = NextURL();
67    browser->OpenURL(
68        OpenURLParams(url,
69        content::Referrer(GURL("http://localhost"),
70                          WebKit::WebReferrerPolicyDefault),
71        CURRENT_TAB,
72        content::PageTransitionFromInt(0), false));
73    urls.push_back(url);
74  }
75  WaitForTabsToLoad(profile, urls);
76}
77
78void SessionsSyncPerfTest::RemoveTabs(int profile) {
79  GetBrowser(profile)->tab_strip_model()->CloseAllTabs();
80}
81
82int SessionsSyncPerfTest::GetTabCount(int profile) {
83  int tab_count = 0;
84  const browser_sync::SyncedSession* local_session;
85  SyncedSessionVector sessions;
86
87  if (!GetLocalSession(profile, &local_session)) {
88    DVLOG(1) << "GetLocalSession returned false";
89    return -1;
90  }
91
92  if (!GetSessionData(profile, &sessions)) {
93    // Foreign session data may be empty.  In this case we only count tabs in
94    // the local session.
95    DVLOG(1) << "GetSessionData returned false";
96  }
97
98  sessions.push_back(local_session);
99  for (SyncedSessionVector::const_iterator it = sessions.begin();
100       it != sessions.end(); ++it) {
101    for (SessionWindowMap::const_iterator win_it = (*it)->windows.begin();
102         win_it != (*it)->windows.end();
103         ++win_it) {
104      tab_count += win_it->second->tabs.size();
105    }
106  }
107  return tab_count;
108}
109
110GURL SessionsSyncPerfTest::NextURL() {
111  return IntToURL(url_number_++);
112}
113
114GURL SessionsSyncPerfTest::IntToURL(int n) {
115  return GURL(base::StringPrintf("http://localhost/%d", n));
116}
117
118// TODO(lipalani): Re-enable after crbug.com/96921 is fixed.
119IN_PROC_BROWSER_TEST_F(SessionsSyncPerfTest, DISABLED_P0) {
120  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
121
122  AddTabs(0, kNumTabs);
123  base::TimeDelta dt =
124      SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
125  ASSERT_EQ(kNumTabs, GetTabCount(0));
126  ASSERT_EQ(kNumTabs, GetTabCount(1));
127  SyncTimingHelper::PrintResult("tabs", "add_tabs", dt);
128
129  UpdateTabs(0);
130  dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
131  ASSERT_EQ(kNumTabs, GetTabCount(0));
132  ASSERT_EQ(kNumTabs, GetTabCount(1));
133  SyncTimingHelper::PrintResult("tabs", "update_tabs", dt);
134
135  RemoveTabs(0);
136  dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
137  // New tab page remains open on profile 0 after closing all tabs.
138  ASSERT_EQ(1, GetTabCount(0));
139  ASSERT_EQ(0, GetTabCount(1));
140  SyncTimingHelper::PrintResult("tabs", "delete_tabs", dt);
141}
142