autofill_sync_perf_test.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 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/stringprintf.h"
6#include "base/utf_string_conversions.h"
7#include "chrome/browser/sync/profile_sync_service_harness.h"
8#include "chrome/browser/sync/test/integration/autofill_helper.h"
9#include "chrome/browser/sync/test/integration/bookmarks_helper.h"
10#include "chrome/browser/sync/test/integration/performance/sync_timing_helper.h"
11#include "chrome/browser/sync/test/integration/sync_test.h"
12#include "chrome/browser/webdata/autofill_entry.h"
13#include "components/autofill/browser/autofill_common_test.h"
14#include "components/autofill/browser/autofill_profile.h"
15
16using autofill_helper::AllProfilesMatch;
17using autofill_helper::GetAllKeys;
18using autofill_helper::GetAllProfiles;
19using autofill_helper::GetKeyCount;
20using autofill_helper::GetProfileCount;
21using autofill_helper::RemoveKeys;
22using autofill_helper::SetProfiles;
23
24// See comments in typed_urls_sync_perf_test.cc for reasons for these
25// magic numbers.
26//
27// TODO(akalin): If this works, decomp the magic number calculation
28// into a macro and have all the perf tests use it.
29static const int kNumKeys = 163;
30static const int kNumProfiles = 163;
31
32class AutofillSyncPerfTest : public SyncTest {
33 public:
34  AutofillSyncPerfTest()
35      : SyncTest(TWO_CLIENT),
36        guid_number_(0),
37        name_number_(0),
38        value_number_(0) {}
39
40  // Adds |num_profiles| new autofill profiles to the sync profile |profile|.
41  void AddProfiles(int profile, int num_profiles);
42
43  // Updates all autofill profiles for the sync profile |profile|.
44  void UpdateProfiles(int profile);
45
46  // Removes all autofill profiles from |profile|.
47  void RemoveProfiles(int profile);
48
49  // Adds |num_keys| new autofill keys to the sync profile |profile|.
50  void AddKeys(int profile, int num_keys);
51
52 private:
53  // Returns a new unique autofill profile.
54  const AutofillProfile NextAutofillProfile();
55
56  // Returns a new unique autofill key.
57  const AutofillKey NextAutofillKey();
58
59  // Returns an unused unique guid.
60  const std::string NextGUID();
61
62  // Returns a unique guid based on the input integer |n|.
63  const std::string IntToGUID(int n);
64
65  // Returns a new unused unique name.
66  const std::string NextName();
67
68  // Returns a unique name based on the input integer |n|.
69  const std::string IntToName(int n);
70
71  // Returns a new unused unique value for autofill entries.
72  const std::string NextValue();
73
74  // Returnes a unique value based on the input integer |n|.
75  const std::string IntToValue(int n);
76
77  int guid_number_;
78  int name_number_;
79  int value_number_;
80  DISALLOW_COPY_AND_ASSIGN(AutofillSyncPerfTest);
81};
82
83void AutofillSyncPerfTest::AddProfiles(int profile, int num_profiles) {
84  const std::vector<AutofillProfile*>& all_profiles =
85      GetAllProfiles(profile);
86  std::vector<AutofillProfile> autofill_profiles;
87  for (size_t i = 0; i < all_profiles.size(); ++i) {
88    autofill_profiles.push_back(*all_profiles[i]);
89  }
90  for (int i = 0; i < num_profiles; ++i) {
91    autofill_profiles.push_back(NextAutofillProfile());
92  }
93  SetProfiles(profile, &autofill_profiles);
94}
95
96void AutofillSyncPerfTest::UpdateProfiles(int profile) {
97  const std::vector<AutofillProfile*>& all_profiles =
98      GetAllProfiles(profile);
99  std::vector<AutofillProfile> autofill_profiles;
100  for (size_t i = 0; i < all_profiles.size(); ++i) {
101    autofill_profiles.push_back(*all_profiles[i]);
102    autofill_profiles.back().SetRawInfo(AutofillFieldType(NAME_FIRST),
103                                        UTF8ToUTF16(NextName()));
104  }
105  SetProfiles(profile, &autofill_profiles);
106}
107
108void AutofillSyncPerfTest::RemoveProfiles(int profile) {
109  std::vector<AutofillProfile> empty;
110  SetProfiles(profile, &empty);
111}
112
113void AutofillSyncPerfTest::AddKeys(int profile, int num_keys) {
114  std::set<AutofillKey> keys;
115  for (int i = 0; i < num_keys; ++i) {
116    keys.insert(NextAutofillKey());
117  }
118  autofill_helper::AddKeys(profile, keys);
119}
120
121const AutofillProfile AutofillSyncPerfTest::NextAutofillProfile() {
122  AutofillProfile profile;
123  autofill_test::SetProfileInfoWithGuid(&profile, NextGUID().c_str(),
124                                        NextName().c_str(), "", "", "", "", "",
125                                        "", "", "", "", "", "");
126  return profile;
127}
128
129const AutofillKey AutofillSyncPerfTest::NextAutofillKey() {
130  return AutofillKey(NextName().c_str(), NextName().c_str());
131}
132
133const std::string AutofillSyncPerfTest::NextGUID() {
134  return IntToGUID(guid_number_++);
135}
136
137const std::string AutofillSyncPerfTest::IntToGUID(int n) {
138  return base::StringPrintf("00000000-0000-0000-0000-%012X", n);
139}
140
141const std::string AutofillSyncPerfTest::NextName() {
142  return IntToName(name_number_++);
143}
144
145const std::string AutofillSyncPerfTest::IntToName(int n) {
146  return base::StringPrintf("Name%d", n);
147}
148
149const std::string AutofillSyncPerfTest::NextValue() {
150  return IntToValue(value_number_++);
151}
152
153const std::string AutofillSyncPerfTest::IntToValue(int n) {
154  return base::StringPrintf("Value%d", n);
155}
156
157void ForceSync(int profile) {
158  static int id = 0;
159  ++id;
160  EXPECT_TRUE(
161      bookmarks_helper::AddURL(profile, 0,
162                               bookmarks_helper::IndexedURLTitle(id),
163                               GURL(bookmarks_helper::IndexedURL(id))) != NULL);
164}
165
166IN_PROC_BROWSER_TEST_F(AutofillSyncPerfTest, AutofillProfiles_P0) {
167  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
168
169  // TCM ID - 7557873.
170  AddProfiles(0, kNumProfiles);
171  base::TimeDelta dt =
172      SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
173  ASSERT_EQ(kNumProfiles, GetProfileCount(1));
174  SyncTimingHelper::PrintResult("autofill", "add_autofill_profiles", dt);
175
176  // TCM ID - 7549835.
177  UpdateProfiles(0);
178  dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
179  ASSERT_EQ(kNumProfiles, GetProfileCount(1));
180  SyncTimingHelper::PrintResult("autofill", "update_autofill_profiles", dt);
181
182  // TCM ID - 7553678.
183  RemoveProfiles(0);
184  dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
185  ASSERT_EQ(0, GetProfileCount(1));
186  SyncTimingHelper::PrintResult("autofill", "delete_autofill_profiles", dt);
187}
188
189IN_PROC_BROWSER_TEST_F(AutofillSyncPerfTest, Autofill_P0) {
190  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
191
192  AddKeys(0, kNumKeys);
193  // TODO(lipalani): fix this. The following line is added to force sync.
194  ForceSync(0);
195  base::TimeDelta dt =
196      SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
197  ASSERT_EQ(kNumKeys, GetKeyCount(1));
198  SyncTimingHelper::PrintResult("autofill", "add_autofill_keys", dt);
199
200  RemoveKeys(0);
201  // TODO(lipalani): fix this. The following line is added to force sync.
202  ForceSync(0);
203  dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
204  ASSERT_EQ(0, GetKeyCount(1));
205  SyncTimingHelper::PrintResult("autofill", "delete_autofill_keys", dt);
206}
207