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.
5 #include "base/basictypes.h"
6 #include "base/strings/stringprintf.h"
7 #include "chrome/browser/sync/test/integration/performance/sync_timing_helper.h"
8 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
9 #include "chrome/browser/sync/test/integration/sync_test.h"
10 #include "chrome/browser/sync/test/integration/typed_urls_helper.h"
11 #include "sync/sessions/sync_session_context.h"
13 using typed_urls_helper::AddUrlToHistory;
14 using typed_urls_helper::DeleteUrlsFromHistory;
15 using typed_urls_helper::GetTypedUrlsFromClient;
17 // This number should be as far away from a multiple of
18 // kDefaultMaxCommitBatchSize as possible, so that sync cycle counts
19 // for batch operations stay the same even if some batches end up not
20 // being completely full.
21 static const int kNumUrls = 163;
22 // This compile assert basically asserts that kNumUrls is right in the
23 // middle between two multiples of kDefaultMaxCommitBatchSize.
25 ((kNumUrls % syncer::kDefaultMaxCommitBatchSize) >=
26 (syncer::kDefaultMaxCommitBatchSize / 2)) &&
27 ((kNumUrls % syncer::kDefaultMaxCommitBatchSize) <=
28 ((syncer::kDefaultMaxCommitBatchSize + 1) / 2)),
29 kNumUrlsShouldBeBetweenTwoMultiplesOfkDefaultMaxCommitBatchSize);
31 class TypedUrlsSyncPerfTest : public SyncTest {
33 TypedUrlsSyncPerfTest()
34 : SyncTest(TWO_CLIENT),
37 // Adds |num_urls| new unique typed urls to |profile|.
38 void AddURLs(int profile, int num_urls);
40 // Update all typed urls in |profile| by visiting them once again.
41 void UpdateURLs(int profile);
43 // Removes all typed urls for |profile|.
44 void RemoveURLs(int profile);
46 // Returns the number of typed urls stored in |profile|.
47 int GetURLCount(int profile);
50 // Returns a new unique typed URL.
53 // Returns a unique URL according to the integer |n|.
57 DISALLOW_COPY_AND_ASSIGN(TypedUrlsSyncPerfTest);
60 void TypedUrlsSyncPerfTest::AddURLs(int profile, int num_urls) {
61 for (int i = 0; i < num_urls; ++i) {
62 AddUrlToHistory(profile, NextURL());
66 void TypedUrlsSyncPerfTest::UpdateURLs(int profile) {
67 history::URLRows urls = GetTypedUrlsFromClient(profile);
68 for (history::URLRows::const_iterator it = urls.begin(); it != urls.end();
70 AddUrlToHistory(profile, it->url());
74 void TypedUrlsSyncPerfTest::RemoveURLs(int profile) {
75 const history::URLRows& urls = GetTypedUrlsFromClient(profile);
76 std::vector<GURL> gurls;
77 for (history::URLRows::const_iterator it = urls.begin(); it != urls.end();
79 gurls.push_back(it->url());
81 DeleteUrlsFromHistory(profile, gurls);
84 int TypedUrlsSyncPerfTest::GetURLCount(int profile) {
85 return GetTypedUrlsFromClient(profile).size();
88 GURL TypedUrlsSyncPerfTest::NextURL() {
89 return IntToURL(url_number_++);
92 GURL TypedUrlsSyncPerfTest::IntToURL(int n) {
93 return GURL(base::StringPrintf("http://history%d.google.com/", n));
96 IN_PROC_BROWSER_TEST_F(TypedUrlsSyncPerfTest, P0) {
97 ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
100 AddURLs(0, kNumUrls);
102 SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
103 ASSERT_EQ(kNumUrls, GetURLCount(1));
104 SyncTimingHelper::PrintResult("typed_urls", "add_typed_urls", dt);
108 dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
109 ASSERT_EQ(kNumUrls, GetURLCount(1));
110 SyncTimingHelper::PrintResult("typed_urls", "update_typed_urls", dt);
114 dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
115 ASSERT_EQ(0, GetURLCount(1));
116 SyncTimingHelper::PrintResult("typed_urls", "delete_typed_urls", dt);