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/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"
12
13using typed_urls_helper::AddUrlToHistory;
14using typed_urls_helper::AssertAllProfilesHaveSameURLsAsVerifier;
15using typed_urls_helper::DeleteUrlsFromHistory;
16using typed_urls_helper::GetTypedUrlsFromClient;
17
18// This number should be as far away from a multiple of
19// kDefaultMaxCommitBatchSize as possible, so that sync cycle counts
20// for batch operations stay the same even if some batches end up not
21// being completely full.
22static const int kNumUrls = 163;
23// This compile assert basically asserts that kNumUrls is right in the
24// middle between two multiples of kDefaultMaxCommitBatchSize.
25COMPILE_ASSERT(
26    ((kNumUrls % syncer::kDefaultMaxCommitBatchSize) >=
27     (syncer::kDefaultMaxCommitBatchSize / 2)) &&
28    ((kNumUrls % syncer::kDefaultMaxCommitBatchSize) <=
29     ((syncer::kDefaultMaxCommitBatchSize + 1) / 2)),
30    kNumUrlsShouldBeBetweenTwoMultiplesOfkDefaultMaxCommitBatchSize);
31
32class TypedUrlsSyncPerfTest : public SyncTest {
33 public:
34  TypedUrlsSyncPerfTest()
35      : SyncTest(TWO_CLIENT),
36        url_number_(0) {}
37
38  // Adds |num_urls| new unique typed urls to |profile|.
39  void AddURLs(int profile, int num_urls);
40
41  // Update all typed urls in |profile| by visiting them once again.
42  void UpdateURLs(int profile);
43
44  // Removes all typed urls for |profile|.
45  void RemoveURLs(int profile);
46
47  // Returns the number of typed urls stored in |profile|.
48  int GetURLCount(int profile);
49
50 private:
51  // Returns a new unique typed URL.
52  GURL NextURL();
53
54  // Returns a unique URL according to the integer |n|.
55  GURL IntToURL(int n);
56
57  int url_number_;
58  DISALLOW_COPY_AND_ASSIGN(TypedUrlsSyncPerfTest);
59};
60
61void TypedUrlsSyncPerfTest::AddURLs(int profile, int num_urls) {
62  for (int i = 0; i < num_urls; ++i) {
63    AddUrlToHistory(profile, NextURL());
64  }
65}
66
67void TypedUrlsSyncPerfTest::UpdateURLs(int profile) {
68  history::URLRows urls = GetTypedUrlsFromClient(profile);
69  for (history::URLRows::const_iterator it = urls.begin(); it != urls.end();
70       ++it) {
71    AddUrlToHistory(profile, it->url());
72  }
73}
74
75void TypedUrlsSyncPerfTest::RemoveURLs(int profile) {
76  const history::URLRows& urls = GetTypedUrlsFromClient(profile);
77  std::vector<GURL> gurls;
78  for (history::URLRows::const_iterator it = urls.begin(); it != urls.end();
79       ++it) {
80    gurls.push_back(it->url());
81  }
82  DeleteUrlsFromHistory(profile, gurls);
83}
84
85int TypedUrlsSyncPerfTest::GetURLCount(int profile) {
86  return GetTypedUrlsFromClient(profile).size();
87}
88
89GURL TypedUrlsSyncPerfTest::NextURL() {
90  return IntToURL(url_number_++);
91}
92
93GURL TypedUrlsSyncPerfTest::IntToURL(int n) {
94  return GURL(base::StringPrintf("http://history%d.google.com/", n));
95}
96
97IN_PROC_BROWSER_TEST_F(TypedUrlsSyncPerfTest, P0) {
98  ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
99
100  // TCM ID - 7985716.
101  AddURLs(0, kNumUrls);
102  base::TimeDelta dt =
103      SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
104  ASSERT_EQ(kNumUrls, GetURLCount(1));
105  SyncTimingHelper::PrintResult("typed_urls", "add_typed_urls", dt);
106
107  // TCM ID - 7981755.
108  UpdateURLs(0);
109  dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
110  ASSERT_EQ(kNumUrls, GetURLCount(1));
111  SyncTimingHelper::PrintResult("typed_urls", "update_typed_urls", dt);
112
113  // TCM ID - 7651271.
114  RemoveURLs(0);
115  dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
116  ASSERT_EQ(0, GetURLCount(1));
117  SyncTimingHelper::PrintResult("typed_urls", "delete_typed_urls", dt);
118}
119