|
| 1 | +// |
| 2 | +// NextTrackerDataSetPerformanceTests.swift |
| 3 | +// |
| 4 | +// Copyright © 2021 DuckDuckGo. All rights reserved. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | + |
| 19 | + |
| 20 | +import Foundation |
| 21 | +import XCTest |
| 22 | +import TrackerRadarKit |
| 23 | +import WebKit |
| 24 | + |
| 25 | +class NextTrackerDataSetPerformanceTests: XCTestCase { |
| 26 | + |
| 27 | + static func nextURL(filename: String) -> URL { |
| 28 | + return URL(string: "https://staticcdn.duckduckgo.com/trackerblocking/v5/next/\(filename)")! |
| 29 | + } |
| 30 | + |
| 31 | + var nextiOSTDS: TrackerData! |
| 32 | + |
| 33 | + |
| 34 | + override func setUp() async throws { |
| 35 | + try await super.setUp() |
| 36 | + |
| 37 | + let (data, _) = try await URLSession.shared.data(from: Self.nextURL(filename: "ios-tds.json")) |
| 38 | + |
| 39 | + nextiOSTDS = try JSONDecoder().decode(TrackerData.self, from: data) |
| 40 | + |
| 41 | + print("Next TDS prepared") |
| 42 | + } |
| 43 | + |
| 44 | + func testPerformanceOfNext_iOSTDS() throws { |
| 45 | + |
| 46 | + let rules = ContentBlockerRulesBuilder(trackerData: nextiOSTDS).buildRules() |
| 47 | + |
| 48 | + let data = try JSONEncoder().encode(rules) |
| 49 | + let ruleList = String(data: data, encoding: .utf8)! |
| 50 | + |
| 51 | + var store: WKContentRuleListStore! |
| 52 | + |
| 53 | + let warmUpExpectation = expectation(description: "Warmed up") |
| 54 | + |
| 55 | + DispatchQueue.main.async { |
| 56 | + store = WKContentRuleListStore(url: FileManager.default.temporaryDirectory) |
| 57 | + store.compileContentRuleList(forIdentifier: UUID().uuidString, encodedContentRuleList: ruleList) { _, error in |
| 58 | + XCTAssertNil(error) |
| 59 | + warmUpExpectation.fulfill() |
| 60 | + } |
| 61 | + } |
| 62 | + wait(for: [warmUpExpectation], timeout: 40) |
| 63 | + |
| 64 | + measure { |
| 65 | + let time = CACurrentMediaTime() |
| 66 | + let expectation = expectation(description: "Compiled") |
| 67 | + |
| 68 | + store.compileContentRuleList(forIdentifier: UUID().uuidString, encodedContentRuleList: ruleList) { _, error in |
| 69 | + XCTAssertNil(error) |
| 70 | + |
| 71 | + Thread.sleep(forTimeInterval: 1) |
| 72 | + |
| 73 | + expectation.fulfill() |
| 74 | + } |
| 75 | + |
| 76 | + wait(for: [expectation], timeout: 40) |
| 77 | + print("Compiled in \(CACurrentMediaTime() - time)") |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments