SPB Git

Repository Public License

PerformanceTests.swift 4.6 KB 98 lines swift

1// ORIX Secure2// File: Packages/ORIXMediaCleanup/Tests/ORIXMediaCleanupTests/PerformanceTests.swift3// Description: Measured throughput of fingerprinting and grouping time / memory for 10 000 and 50 000 synthetic items.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import CoreGraphics8import Foundation9import Testing10@testable import ORIXMediaCleanup1112@Suite("Performance", .serialized)13struct PerformanceTests {14    /// Peak resident memory of the process, in bytes (macOS reports `ru_maxrss` in bytes).15    static func peakMemory() -> Int64 {16        var usage = rusage()17        getrusage(RUSAGE_SELF, &usage)18        return Int64(usage.ru_maxrss)19    }2021    static func seconds(_ duration: Duration) -> Double {22        Double(duration.components.seconds) + Double(duration.components.attoseconds) / 1e1823    }2425    @Test("Fingerprint throughput at 128 px")26    func fingerprintThroughput() {27        let images = (0..<40).map { TestImages.scene(seed: UInt64($0), width: 128, height: 96) }28        let rounds = 2529        let clock = ContinuousClock()30        var produced = 031        let elapsed = clock.measure {32            for _ in 0..<rounds {33                for image in images where ImageFingerprinter.fingerprint(of: image) != nil { produced += 1 }34            }35        }36        let rate = Double(produced) / Self.seconds(elapsed)37        print("PERFORMANCE fingerprint: \(produced) images of 128×96 in \(elapsed) → \(Int(rate)) images/s (one thread)")38        #expect(produced == images.count * rounds)39        #expect(rate > 200)40    }4142    @Test("Grouping time and memory for 10 000 and 50 000 items")43    func grouping() async throws {44        let clock = ContinuousClock()45        var timings: [Int: Double] = [:]46        var growth: [Int: Int64] = [:]47        for count in [10_000, 50_000] {48            let items = SyntheticLibrary.items(count: count, seed: UInt64(count))49            let before = Self.peakMemory()50            let start = clock.now51            let analysis = try await CleanupAnalyzer().analyze(items)52            let elapsed = Self.seconds(clock.now - start)53            let after = Self.peakMemory()54            timings[count] = elapsed55            growth[count] = after - before56            let similar = analysis.groups(in: .similar).count + analysis.groups(in: .burstLike).count57            print("PERFORMANCE grouping \(count) items: \(String(format: "%.3f", elapsed)) s, peak memory growth \((after - before) / 1_048_576) MB, similar groups \(similar), exact \(analysis.groups(in: .exactDuplicates).count)")58            #expect(analysis.statistics.itemsScanned == count)59            #expect(similar > count / 50)60        }61        var config = CleanupConfiguration()62        config.globalPassEnabled = true63        let items = SyntheticLibrary.items(count: 50_000, seed: 50_000)64        let start = clock.now65        _ = try await CleanupAnalyzer(configuration: config).analyze(items)66        let global = Self.seconds(clock.now - start)67        print("PERFORMANCE grouping 50000 items with the global pass: \(String(format: "%.3f", global)) s")6869        let large = try #require(timings[50_000])70        let small = try #require(timings[10_000])71        #expect(large < 30)72        #expect(global < 60)73        // Linear-ish in time (lookback-bounded) and in memory: 5× the items may not cost more than 15× the time, nor more than 400 MB.74        #expect(large < max(small, 0.05) * 15)75        #expect(try #require(growth[50_000]) < 400 * 1_048_576)76    }7778    @Test("Worst case for the session pass: every photo in one session, all alike")79    func denseSession() async throws {80        // 5 000 photos 1 s apart in 50 looks: complete linkage keeps groups tight and the lookback bounds the work.81        var rng = SeededGenerator(seed: 77)82        let looks = (0..<50).map { _ in rng.next() }83        var items: [MediaItem] = []84        for index in 0..<5000 {85            let look = looks[index % looks.count]86            items.append(Fixture.photo(String(format: "d%05d", index), at: Double(index) * 5, fingerprint: Fixture.fingerprint(d: Fixture.flipping(look, bits: 2, seed: rng.next()))))87        }88        let clock = ContinuousClock()89        let start = clock.now90        let analysis = try await CleanupAnalyzer().analyze(items)91        let elapsed = Self.seconds(clock.now - start)92        let groups = analysis.groups(in: .similar)93        print("PERFORMANCE dense session of 5000: \(String(format: "%.3f", elapsed)) s, \(groups.count) groups, largest \(groups.map { $0.items.count }.max() ?? 0)")94        #expect(elapsed < 20)95        #expect(groups.count >= 50)96    }97}98
Files main
Menu

Theme