forked from duckduckgo/iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppTrackingProtectionStoringModelPerformanceTests.swift
138 lines (114 loc) · 4.96 KB
/
AppTrackingProtectionStoringModelPerformanceTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// AppTrackingProtectionStoringModelPerformanceTests.swift
// DuckDuckGo
//
// Copyright © 2023 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import XCTest
import CoreData
import Persistence
@testable import Core
class AppTrackingProtectionStoringModelPerformanceTests: XCTestCase {
private let dateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
return formatter
}()
private var database: CoreDataDatabase!
override func setUp() {
super.setUp()
let bundle = Bundle(for: AppTrackingProtectionStoringModel.self)
let model = CoreDataDatabase.loadModel(from: bundle, named: "AppTrackingProtectionModel")!
let options = [
NSPersistentHistoryTrackingKey: true as NSNumber,
NSPersistentStoreRemoteChangeNotificationPostOptionKey: true as NSNumber
]
database = CoreDataDatabase(name: "AppTrackingProtectionStoringModelPerformanceTests",
containerLocation: tempDBDir(),
model: model,
options: options)
database.loadStore()
}
override func tearDownWithError() throws {
try super.tearDownWithError()
try database.tearDown(deleteStores: true)
}
func testNewTrackerPerformance() throws {
let dates = [
createDate(year: 2023, month: 01, day: 01),
createDate(year: 2023, month: 01, day: 02),
createDate(year: 2023, month: 01, day: 03),
createDate(year: 2023, month: 01, day: 04),
createDate(year: 2023, month: 01, day: 05),
createDate(year: 2023, month: 01, day: 06),
createDate(year: 2023, month: 01, day: 07)
]
for date in dates {
try populateData(in: database, date: date, trackerCount: 10_000)
}
let storingModel = AppTrackingProtectionStoringModel(appTrackingProtectionDatabase: database)
var newTrackerValue = 1
measure {
storingModel.storeTracker(domain: "newtracker\(newTrackerValue).com",
trackerOwner: "New",
blocked: true,
date: createDate(year: 2023, month: 01, day: 07))
newTrackerValue += 1
}
}
func testExistingTrackerPerformance() throws {
let dates = [
createDate(year: 2023, month: 01, day: 01),
createDate(year: 2023, month: 01, day: 02),
createDate(year: 2023, month: 01, day: 03),
createDate(year: 2023, month: 01, day: 04),
createDate(year: 2023, month: 01, day: 05),
createDate(year: 2023, month: 01, day: 06),
createDate(year: 2023, month: 01, day: 07)
]
for date in dates {
try populateData(in: database, date: date, trackerCount: 10_000)
}
let storingModel = AppTrackingProtectionStoringModel(appTrackingProtectionDatabase: database)
measure {
storingModel.storeTracker(domain: "tracker1.com", trackerOwner: "Existing",
blocked: true, date: createDate(year: 2023, month: 01, day: 07))
}
}
// MARK: - Utilities
func populateData(in database: CoreDataDatabase, date: Date, trackerCount: Int) throws {
let context = database.makeContext(concurrencyType: .mainQueueConcurrencyType)
let bucket = dateFormatter.string(from: date)
for value in 0...trackerCount {
let name = "tracker\(value).com"
_ = AppTrackerEntity.makeTracker(domain: name, trackerOwner: name, blocked: true, date: date, bucket: bucket, context: context)
}
try context.save()
}
private func createDate(year: Int, month: Int, day: Int) -> Date {
var dateComponents = DateComponents()
dateComponents.year = year
dateComponents.month = month
dateComponents.day = day
dateComponents.hour = 9
dateComponents.minute = 42
return Calendar(identifier: .gregorian).date(from: dateComponents)!
}
private func fetchTrackers(_ context: NSManagedObjectContext) -> [AppTrackerEntity] {
let request = AppTrackerEntity.fetchRequest()
request.returnsObjectsAsFaults = false
return (try? context.fetch(request)) ?? []
}
}