Skip to content

Commit

Permalink
Add DataCache write/flush performance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Jul 15, 2023
1 parent 8e2ee31 commit 3f95239
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
4 changes: 1 addition & 3 deletions Sources/Nuke/Caching/DataCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ public final class DataCache: DataCaching, @unchecked Sendable {

/// Returns `url` for the given cache key.
public func url(for key: String) -> URL? {
guard let filename = self.filename(for: key) else {
return nil
}
guard let filename = self.filename(for: key) else { return nil }
return self.path.appendingPathComponent(filename, isDirectory: false)
}

Expand Down
39 changes: 39 additions & 0 deletions Tests/NukePerformanceTests/DataCachePeformanceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,43 @@ class DataCachePeformanceTests: XCTestCase {
try? FileManager.default.removeItem(at: cache.path)
}

// MARK: - Write

func testWriteWithFlush() {
let data = Array(0..<count).map { _ in generateRandomData() }

measure {
for index in data.indices {
cache["\(index)"] = data[index]
}
cache.flush()
}
}

func testWriteWithFlushIndividual() {
let data = Array(0..<count).map { _ in generateRandomData() }

measure {
for index in data.indices {
let key = "\(index)"
cache[key] = data[index]
cache.flush(for: key)
}
}
}

func testWriteWithoutFlush() {
let data = Array(0..<count).map { _ in generateRandomData() }

measure {
for index in data.indices {
cache["\(index)"] = data[index]
}
}
}

// MARK: - Read

func testReadFlushedPerformance() {
populate()

Expand All @@ -47,6 +84,8 @@ class DataCachePeformanceTests: XCTestCase {
}
}

// MARK: - Helpers

func populate() {
for idx in 0..<count {
cache["\(idx)"] = generateRandomData()
Expand Down

0 comments on commit 3f95239

Please sign in to comment.