Created
June 16, 2021 23:51
-
-
Save josephlord/ea5ecb9cc4b330de5c205276d95b7ee8 to your computer and use it in GitHub Desktop.
Rough and ready performance tests of AsyncSequence file reading (using
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import XCTest | |
class TestAsyncFileReadPerformanceTests: XCTestCase { | |
let bundle = Bundle(for: TestAsyncFileReadPerformanceTests.self) | |
lazy var bigFileUrl: URL = { | |
bundle.url(forResource: "randomData", withExtension: "txt")! | |
}() | |
lazy var smallFileUrl: URL = { | |
bundle.url(forResource: "dataJun-14-2021", withExtension: "json")! | |
}() | |
let measureOptions: XCTMeasureOptions = { | |
var mO = XCTMeasureOptions() | |
mO.iterationCount = 1 // First run is more representative | |
return mO | |
}() | |
let measureMetrics: [XCTMetric] = { | |
[XCTCPUMetric(), | |
XCTClockMetric(), | |
XCTMemoryMetric(), | |
] | |
}() | |
func testPerformanceSyncExample() throws { | |
var data: Data? | |
self.measure(metrics: measureMetrics, options: measureOptions) { | |
data = try! Data(contentsOf: bigFileUrl) | |
} | |
XCTAssert(data!.count > 1) | |
} | |
func testPerformanceSyncExampleXOR() throws { | |
var data: Data? | |
self.measure(metrics: measureMetrics, options: measureOptions) { | |
var xor: UInt8 = 0 | |
data = try! Data(contentsOf: bigFileUrl) | |
data?.withUnsafeBytes({ pointer in | |
for byte in pointer { | |
xor ^= byte | |
} | |
}) | |
XCTAssertEqual(xor, 107) | |
} | |
//XCTAssert(data!.count > 1) | |
} | |
func testPerformanceAsyncXOR() throws { | |
measure(metrics: measureMetrics, options: measureOptions) { | |
let e = expectation(description: "Completion") | |
async(priority: .userInteractive) { | |
var xor: UInt8 = 0 | |
for try await byte in try await URLSession.shared.bytes(from: bigFileUrl, delegate: nil).0 { | |
xor ^= byte | |
} | |
XCTAssertEqual(107, xor) | |
e.fulfill() | |
} | |
waitForExpectations(timeout: 1, handler: nil) | |
} | |
} | |
func testPerformanceAsyncReduceXOR() throws { | |
measure(metrics: measureMetrics, options: measureOptions) { | |
let e = expectation(description: "Completion") | |
async(priority: .userInteractive) { | |
let xor = try await URLSession.shared.bytes(from: bigFileUrl, delegate: nil) | |
.0 | |
.reduce(0, ^) | |
XCTAssertEqual(107, xor) | |
e.fulfill() | |
} | |
waitForExpectations(timeout: 1, handler: nil) | |
} | |
} | |
var dataTRAB: Data? = nil | |
/// As I feared the reduce approach is very inefficent and slow | |
func testReduceAsyncBytes() throws { | |
let urlSession = URLSession.shared | |
self.measure(metrics: measureMetrics, options: measureOptions) { | |
let e = expectation(description: "completed") | |
async(priority: .userInteractive) { | |
let bytes = try await urlSession.bytes(from: bigFileUrl, delegate: nil) | |
.0 | |
.reduce([], { $0 + [$1] }) | |
dataTRAB = Data(bytes) | |
e.fulfill() | |
} | |
waitForExpectations(timeout: 600, handler: nil) | |
} | |
XCTAssertGreaterThan(dataTRAB!.count, 2000) | |
} | |
var dataTAAB: Data? = nil | |
func testAppendAsyncBytes() throws { | |
let testUrl = bigFileUrl | |
let urlSession = URLSession.shared | |
self.measure(metrics: measureMetrics, options: measureOptions) { | |
let e = expectation(description: "completed") | |
async(priority: .userInteractive) { | |
var bytes = [UInt8]() | |
bytes.reserveCapacity(4096) | |
for try await byte in try await urlSession.bytes(from: testUrl, delegate: nil).0 { | |
bytes.append(byte) | |
} | |
dataTAAB = Data(bytes) | |
e.fulfill() | |
} | |
waitForExpectations(timeout: 15, handler: nil) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment