Repository Public License
1// ORIX Clean — Packages/ORIXCleanCore/Sources/orix-bench/main.swift2// Description: Command-line benchmark and accuracy probe for the core engines.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import Darwin5import Foundation6import ORIXCleanCore78setvbuf(stdout, nil, _IOLBF, 0)9let args = CommandLine.arguments10let command = args.count > 1 ? args[1] : "help"1112func rss() -> Int64 {13 var info = mach_task_basic_info()14 var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size) / 415 let kr = withUnsafeMutablePointer(to: &info) { $0.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { task_info(mach_task_self_, task_flavor_t(MACH_TASK_BASIC_INFO), $0, &count) } }16 return kr == KERN_SUCCESS ? Int64(info.resident_size) : 017}1819func cpuSeconds() -> Double {20 var u = rusage()21 getrusage(RUSAGE_SELF, &u)22 return Double(u.ru_utime.tv_sec) + Double(u.ru_utime.tv_usec) / 1e6 + Double(u.ru_stime.tv_sec) + Double(u.ru_stime.tv_usec) / 1e623}2425switch command {26case "scan":27 // orix-bench scan <path> [workers]28 let path = args.count > 2 ? args[2] : NSHomeDirectory()29 var o = path == "/" ? ScanOptions.startupVolume() : ScanOptions()30 if args.count > 3 { o.workers = Int(args[3]) ?? 0 }31 let cpu0 = cpuSeconds()32 let r = try FileScanner(root: path, options: o).run()33 let cpu = cpuSeconds() - cpu034 print("path=\(path) files=\(r.files) dirs=\(r.directories) issues=\(r.issues.count) seconds=\(String(format: "%.3f", r.duration)) objects_per_s=\(Int(r.filesPerSecond)) cpu_s=\(String(format: "%.2f", cpu)) rss_mb=\(rss() / 1_048_576) bytes_per_object=\(rss() / Int64(max(1, r.tree.count)))")35 print("logical=\(r.tree[0].logicalSize) allocated=\(r.tree[0].allocatedSize) private=\(r.tree[0].privateSize) sharing=\(r.sharingMeasured)")36case "dupes":37 let path = args.count > 2 ? args[2] : NSHomeDirectory()38 let r = try FileScanner(root: path).run()39 var o = DuplicateOptions()40 o.minimumSize = args.count > 3 ? Int64(args[3]) ?? 1 : 141 let d = DuplicateFinder().run(tree: r.tree, options: o)42 print("candidates=\(d.candidates) groups=\(d.groups.count) hashed_bytes=\(d.bytesHashed) seconds=\(String(format: "%.3f", d.duration)) mb_per_s=\(Int(Double(d.bytesHashed) / 1e6 / max(d.duration, 0.001))) redundant=\(d.redundantLogical) rss_mb=\(rss() / 1_048_576)")43case "similar":44 let path = args.count > 2 ? args[2] : NSHomeDirectory()45 let r = try FileScanner(root: path).run()46 let s = SimilarImageFinder().run(tree: r.tree)47 print("images=\(s.imagesExamined) groups=\(s.groups.count) unreadable=\(s.unreadable) seconds=\(String(format: "%.3f", s.duration)) images_per_s=\(Int(Double(s.imagesExamined) / max(s.duration, 0.001)))")48case "treemap":49 let path = args.count > 2 ? args[2] : NSHomeDirectory()50 let r = try FileScanner(root: path).run()51 let t0 = Date()52 var cells = 053 for _ in 0 ..< 100 { cells = Treemap.layout(tree: r.tree, root: 0, in: CGRect(x: 0, y: 0, width: 1600, height: 1000)).count }54 print("nodes=\(r.tree.count) cells=\(cells) layout_ms=\(String(format: "%.3f", Date().timeIntervalSince(t0) * 10))")55case "sample":56 // orix-bench sample <db> [interval seconds]: records real volume samples until killed.57 let store = try HistoryStore(path: args[2])58 let interval = args.count > 3 ? Double(args[3]) ?? 300 : 30059 while true {60 for v in Volumes.mounted() where v.isStartup { store.record(volume: v, minimumInterval: 0) }61 Thread.sleep(forTimeInterval: interval)62 }63case "fixture":64 // orix-bench fixture <dir> [scale]: writes a sample home folder with real files.65 Fixture.build(home: args[2], scale: args.count > 3 ? Double(args[3]) ?? 1 : 1)66 print("fixture written to \(args[2])")67default:68 print("usage: orix-bench scan|dupes|similar|treemap|sample|fixture <path> …")69}70