Home | History | Annotate | Download | only in performance
      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 
     13 using typed_urls_helper::AddUrlToHistory;
     14 using typed_urls_helper::DeleteUrlsFromHistory;
     15 using typed_urls_helper::GetTypedUrlsFromClient;
     16 
     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.
     24 COMPILE_ASSERT(
     25     ((kNumUrls % syncer::kDefaultMaxCommitBatchSize) >=
     26      (syncer::kDefaultMaxCommitBatchSize / 2)) &&
     27     ((kNumUrls % syncer::kDefaultMaxCommitBatchSize) <=
     28      ((syncer::kDefaultMaxCommitBatchSize + 1) / 2)),
     29     kNumUrlsShouldBeBetweenTwoMultiplesOfkDefaultMaxCommitBatchSize);
     30 
     31 class TypedUrlsSyncPerfTest : public SyncTest {
     32  public:
     33   TypedUrlsSyncPerfTest()
     34       : SyncTest(TWO_CLIENT),
     35         url_number_(0) {}
     36 
     37   // Adds |num_urls| new unique typed urls to |profile|.
     38   void AddURLs(int profile, int num_urls);
     39 
     40   // Update all typed urls in |profile| by visiting them once again.
     41   void UpdateURLs(int profile);
     42 
     43   // Removes all typed urls for |profile|.
     44   void RemoveURLs(int profile);
     45 
     46   // Returns the number of typed urls stored in |profile|.
     47   int GetURLCount(int profile);
     48 
     49  private:
     50   // Returns a new unique typed URL.
     51   GURL NextURL();
     52 
     53   // Returns a unique URL according to the integer |n|.
     54   GURL IntToURL(int n);
     55 
     56   int url_number_;
     57   DISALLOW_COPY_AND_ASSIGN(TypedUrlsSyncPerfTest);
     58 };
     59 
     60 void TypedUrlsSyncPerfTest::AddURLs(int profile, int num_urls) {
     61   for (int i = 0; i < num_urls; ++i) {
     62     AddUrlToHistory(profile, NextURL());
     63   }
     64 }
     65 
     66 void TypedUrlsSyncPerfTest::UpdateURLs(int profile) {
     67   history::URLRows urls = GetTypedUrlsFromClient(profile);
     68   for (history::URLRows::const_iterator it = urls.begin(); it != urls.end();
     69        ++it) {
     70     AddUrlToHistory(profile, it->url());
     71   }
     72 }
     73 
     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();
     78        ++it) {
     79     gurls.push_back(it->url());
     80   }
     81   DeleteUrlsFromHistory(profile, gurls);
     82 }
     83 
     84 int TypedUrlsSyncPerfTest::GetURLCount(int profile) {
     85   return GetTypedUrlsFromClient(profile).size();
     86 }
     87 
     88 GURL TypedUrlsSyncPerfTest::NextURL() {
     89   return IntToURL(url_number_++);
     90 }
     91 
     92 GURL TypedUrlsSyncPerfTest::IntToURL(int n) {
     93   return GURL(base::StringPrintf("http://history%d.google.com/", n));
     94 }
     95 
     96 IN_PROC_BROWSER_TEST_F(TypedUrlsSyncPerfTest, P0) {
     97   ASSERT_TRUE(SetupSync()) << "SetupSync() failed.";
     98 
     99   // TCM ID - 7985716.
    100   AddURLs(0, kNumUrls);
    101   base::TimeDelta dt =
    102       SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
    103   ASSERT_EQ(kNumUrls, GetURLCount(1));
    104   SyncTimingHelper::PrintResult("typed_urls", "add_typed_urls", dt);
    105 
    106   // TCM ID - 7981755.
    107   UpdateURLs(0);
    108   dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
    109   ASSERT_EQ(kNumUrls, GetURLCount(1));
    110   SyncTimingHelper::PrintResult("typed_urls", "update_typed_urls", dt);
    111 
    112   // TCM ID - 7651271.
    113   RemoveURLs(0);
    114   dt = SyncTimingHelper::TimeMutualSyncCycle(GetClient(0), GetClient(1));
    115   ASSERT_EQ(0, GetURLCount(1));
    116   SyncTimingHelper::PrintResult("typed_urls", "delete_typed_urls", dt);
    117 }
    118