         site.guid = "abc\(i)def"
 
         history.insertOrUpdatePlace(site.asPlace(), modified: 1234567890).value
-
         for j in 0...20 {
-            let local = SiteVisit(site: site, date: Timestamp(1000 * (1437088398461 + (1000 * i) + j)), type: VisitType.Link)
-            XCTAssertTrue(history.addLocalVisit(local).value.isSuccess)
-        }
-
-        var remotes = [Visit]()
-        for j in 0...20 {
-            remotes.append(SiteVisit(site: site, date: Timestamp(1000 * (1437088399461 + (1000 * i) + j)), type: VisitType.Link))
-        }
-        history.storeRemoteVisits(remotes, forGUID: site.guid!).value
+            let visitTime = Timestamp(1000 * (1437088398461 + (1000 * i) + j))
+            addVisitForSite(site, intoHistory: history, from: .Local, atTime: visitTime)
+            addVisitForSite(site, intoHistory: history, from: .Remote, atTime: visitTime)
+        }
+    }
+}
+
+private func addVisitForSite(site: Site, intoHistory history: SQLiteHistory, from: VisitOrigin, atTime: Timestamp) {
+    let visit = SiteVisit(site: site, date: atTime, type: VisitType.Link)
+    switch from {
+    case .Local:
+            history.addLocalVisit(visit).value
+    case .Remote:
+        history.storeRemoteVisits([visit], forGUID: site.guid!).value
     }
 }
            return succeed()
        }

        func checkTopSitesReturnsResults() -> Success {
            return history.getTopSitesWithLimit(20) >>== { topSites in
                XCTAssertEqual(topSites.count, 20)
                XCTAssertNotNil(prefs.timestampForKey(PrefsKeys.KeyLastFrecencyCacheTime))
                XCTAssertEqual(topSites[0]!.guid, "abc\(500)def")
                return succeed()
            }
        }

        func addAdditionalVisits() -> Success {
            let site = Site(url: "http://s\(19)ite\(19)/foo", title: "A \(19)")
            site.guid = "abc\(19)def"

            for i in 0...20 {
                let local = SiteVisit(site: site, date: Timestamp(1000 * (1437088398461 + (1000 * i))), type: VisitType.Link)
                XCTAssertTrue(history.addLocalVisit(local).value.isSuccess)
            }

            // Set timestamp to something really old to force invalidation
            prefs.setTimestamp(0, forKey: PrefsKeys.KeyLastFrecencyCacheTime)
            return succeed()
        }

        func checkTopOfTopSitesChanged() -> Success {
            return history.getTopSitesWithLimit(20) >>== { topSites in
                XCTAssertEqual(topSites.count, 20)
                XCTAssertEqual(topSites[0]!.guid, "abc\(19)def")
                return succeed()
            }
        }

        checkTopSitesReturnsResults()
            >>> addAdditionalVisits
            >>> checkTopOfTopSitesChanged
            >>> done

        waitForExpectationsWithTimeout(10.0) { error in

        history.clearHistory().value
        populateHistoryForFrecencyCalcuations(history, siteCount: count)

        history.setTopSitesNeedsInvalidation()
        self.measureMetrics([XCTPerformanceMetric_WallClockTime], automaticallyStartMeasuring: true) {
            history.invalidateTopSitesIfNeeded().value
            self.stopMeasuring()
        }
    }
}

// MARK - Private Test Helper Methods

private enum VisitOrigin {
    case Local
    case Remote
}

private func populateHistoryForFrecencyCalcuations(history: SQLiteHistory, siteCount count: Int) {
