Repository Public License
1// ORIX Convert — Packages/ConvertKit/Sources/ConvertCLI/DevCommand.swift2// Description: Development commands of `orix-convert` (hidden from the overview): `dev seed` generates benchmark and3// test material in code, `dev stress` runs the mixed-batch robustness scenario through `convert`.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import CoreGraphics7import CoreText8import ConvertCore9import ConvertKit1011enum DevCommand: Command {12 static let hidden = true13 static let spec = CommandSpec(14 "dev", summary: "Development helpers: generated test material and the mixed-batch robustness run.",15 usage: "dev seed images <dir> [--count <n>] [--width <w>] [--height <h>]\n orix-convert dev seed wav <file> [--seconds <s>]\n orix-convert dev seed csv <file> [--bytes <n>]\n orix-convert dev seed markdown <file> [--pages <n>]\n orix-convert dev seed pdf <file> [--pages <n>] [--scanned]\n orix-convert dev seed video <file> [--seconds <s>] [--width <w>] [--height <h>] [--codec <ffmpeg encoder>]\n orix-convert dev seed folder <dir> [--bytes <n>]\n orix-convert dev stress [--work-dir <dir>] [--report-dir <dir>] [--jobs <n>]\n orix-convert dev fixtures <dir>",16 flags: ["scanned", "keep"], options: ["count", "width", "height", "seconds", "bytes", "pages", "codec", "work-dir", "report-dir", "jobs"],17 minPositionals: 1, unbounded: true)1819 static func run(_ arguments: ParsedArguments, io: CommandIO) async throws -> ExitCode {20 switch arguments.positionals[0] {21 case "seed":22 guard arguments.positionals.count == 3 else { throw UsageError("Usage: orix-convert \(spec.usage)") }23 try await seed(kind: arguments.positionals[1], target: Shared.fileURL(arguments.positionals[2]), arguments: arguments, io: io)24 return .success25 case "stress":26 return try await Stress.run(arguments, io: io)27 case "fixtures":28 // One real file per format (seeds generated in code, the rest converted through the engines), for scripts.29 guard arguments.positionals.count == 2 else { throw UsageError("Usage: orix-convert dev fixtures <dir>") }30 let directory = Shared.fileURL(arguments.positionals[1])31 let service = ConversionService(configuration: .init(maxConcurrentJobs: try arguments.int("jobs", in: 1...32) ?? 6, recordHistory: false))32 let store = FixtureStore(directory: directory, service: service, log: { io.err(" " + $0) })33 await store.loadManifest()34 await store.build(for: Set(service.registry.paths.map(\.source)))35 for fixture in await store.all { io.out("\(fixture.format)\t\(fixture.path)\t\(fixture.origin)") }36 return .success37 default:38 throw UsageError("dev: unknown action '\(arguments.positionals[0])'.")39 }40 }4142 static func seed(kind: String, target: URL, arguments: ParsedArguments, io: CommandIO) async throws {43 let fm = FileManager.default44 switch kind {45 case "images":46 let count = try arguments.int("count", in: 1...100_000) ?? 1047 let width = try arguments.int("width", in: 1...20_000) ?? 4000, height = try arguments.int("height", in: 1...20_000) ?? 300048 try fm.createDirectory(at: target, withIntermediateDirectories: true)49 // Drawing is CPU-bound and independent per file: spread it over the cores.50 let failures = LockedCounter()51 DispatchQueue.concurrentPerform(iterations: count) { index in52 do {53 let image = try Seeds.image(width: width, height: height, seed: index + 1)54 try Seeds.writeImage(image, to: target.appendingPathComponent(String(format: "image-%04d.png", index + 1)))55 } catch { failures.increment() }56 }57 guard failures.value == 0 else { throw CommandFailure(.conversionFailed, "\(failures.value) image(s) could not be written.") }58 io.out("Wrote \(count) PNG image(s) of \(width)×\(height) in \(target.path)")59 case "wav":60 let seconds = try arguments.double("seconds", in: 0.1...36_000) ?? 1061 try fm.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)62 try Seeds.writeWAV(to: target, seconds: seconds)63 io.out("Wrote \(target.path) (\(seconds) s, 44.1 kHz, stereo, 16-bit)")64 case "csv":65 let bytes = try arguments.int("bytes", in: 100...50_000_000_000) ?? 1_000_00066 try fm.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)67 let rows = try writeCSV(to: target, bytes: bytes)68 io.out("Wrote \(target.path) (\(rows) rows, \(Shared.bytes(Int64(bytes))) requested)")69 case "markdown":70 let pages = try arguments.int("pages", in: 1...5000) ?? 1071 try fm.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)72 try Data(longMarkdown(pages: pages).utf8).write(to: target)73 io.out("Wrote \(target.path) (text for about \(pages) pages)")74 case "pdf":75 let pages = try arguments.int("pages", in: 1...5000) ?? 1076 try fm.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)77 try writePDF(to: target, pages: pages, scanned: arguments.flag("scanned"))78 io.out("Wrote \(target.path) (\(pages) pages, \(arguments.flag("scanned") ? "page images without a text layer" : "text pages"))")79 case "video":80 let seconds = try arguments.double("seconds", in: 0.5...3600) ?? 1081 let width = try arguments.int("width", in: 16...8192) ?? 3840, height = try arguments.int("height", in: 16...8192) ?? 216082 try fm.createDirectory(at: target.deletingLastPathComponent(), withIntermediateDirectories: true)83 let codec = arguments.value("codec") ?? "h264_videotoolbox"84 let distinct = min(30, Int(seconds * 30))85 try await Seeds.writeVideo(to: target, width: width, height: height, rate: 30, distinctFrames: distinct, loops: max(1, Int((seconds * 30).rounded()) / distinct),86 codec: codec, extra: codec.contains("videotoolbox") ? ["-b:v", "30M"] : [])87 io.out("Wrote \(target.path) (\(width)×\(height), \(seconds) s, \(codec), frames drawn in code, encoded by the bundled ffmpeg)")88 case "folder":89 let bytes = try arguments.int("bytes", in: 1000...50_000_000_000) ?? 10_000_00090 try writeFolder(at: target, bytes: bytes)91 io.out("Wrote \(target.path) (about \(Shared.bytes(Int64(bytes))))")92 default:93 throw UsageError("dev seed: unknown kind '\(kind)'. Kinds: images, wav, csv, markdown, pdf, video, folder.")94 }95 }9697 final class LockedCounter: @unchecked Sendable {98 private let lock = NSLock()99 private var count = 0100 func increment() { lock.lock(); count += 1; lock.unlock() }101 var value: Int { lock.lock(); defer { lock.unlock() }; return count }102 }103104 /// A table of nine typed columns, written in blocks until `bytes` is reached. Returns the number of rows.105 static func writeCSV(to url: URL, bytes: Int) throws -> Int {106 FileManager.default.createFile(atPath: url.path, contents: Data("id,token,comment,amount,active,day,stamp,category,zip\n".utf8))107 guard let handle = try? FileHandle(forWritingTo: url) else { throw CommandFailure(.conversionFailed, "\(url.path) could not be written.") }108 defer { try? handle.close() }109 try handle.seekToEnd()110 let categories = ["alpha", "beta", "gamma", "delta", "epsilon"]111 var written = 50, row = 0112 var state: UInt64 = 0x9E3779B97F4A7C15113 let hex = Array("0123456789abcdef".utf8)114 while written < bytes {115 var block = [UInt8]()116 block.reserveCapacity(4 << 20)117 while block.count < (4 << 20) - 200, written + block.count < bytes {118 row += 1119 state = state &* 6364136223846793005 &+ 1442695040888963407120 block.append(contentsOf: String(row).utf8); block.append(44)121 var s = state122 for _ in 0..<32 { block.append(hex[Int(s & 15)]); s = (s >> 4) | (s << 60) }123 block.append(44)124 block.append(contentsOf: "comment number \(row % 9973) for the row".utf8); block.append(44)125 block.append(contentsOf: "\(Int(state >> 40) % 100_000).\(String(format: "%02d", Int(state >> 20) % 100))".utf8); block.append(44)126 block.append(contentsOf: (row % 3 == 0 ? "true" : "false").utf8); block.append(44)127 block.append(contentsOf: "20\(String(format: "%02d", 10 + row % 17))-\(String(format: "%02d", 1 + row % 12))-\(String(format: "%02d", 1 + row % 28))".utf8); block.append(44)128 block.append(contentsOf: "2026-01-\(String(format: "%02d", 1 + row % 28)) \(String(format: "%02d", row % 24)):\(String(format: "%02d", row % 60)):00".utf8); block.append(44)129 block.append(contentsOf: categories[row % categories.count].utf8); block.append(44)130 block.append(contentsOf: String(format: "%05d", row % 99_999).utf8); block.append(10)131 }132 try handle.write(contentsOf: Data(block))133 written += block.count134 }135 return row136 }137138 static func longMarkdown(pages: Int) -> String {139 var out = "# ORIX Convert benchmark document\n\n"140 let sentence = "The conversion studio reads a file, plans the shortest honest route, converts it on this Mac and verifies the result before writing it. "141 for page in 1...pages {142 out += "## Section \(page)\n\n"143 for paragraph in 1...5 {144 out += String(repeating: sentence, count: 4) + "Paragraph \(paragraph) of section \(page).\n\n"145 }146 if page % 5 == 0 { out += "| Item | Value |\n|---|---:|\n| Pages so far | \(page) |\n| Paragraphs | \(page * 5) |\n\n" }147 }148 return out149 }150151 /// A US-Letter PDF drawn with Core Graphics and Core Text. `scanned` draws each page into a 150 dpi bitmap first,152 /// so the page holds a picture of the text and no text layer (what a scanner produces).153 static func writePDF(to url: URL, pages: Int, scanned: Bool) throws {154 var box = CGRect(x: 0, y: 0, width: 612, height: 792)155 guard let context = CGContext(url as CFURL, mediaBox: &box, nil) else { throw CommandFailure(.conversionFailed, "\(url.path) could not be written.") }156 let font = CTFontCreateWithName("Helvetica" as CFString, 12, nil)157 func draw(page: Int, in target: CGContext) {158 target.setFillColor(CGColor(gray: 1, alpha: 1)); target.fill(box)159 for line in 0..<44 {160 let text = line == 0 ? "ORIX Convert benchmark — page \(page) of \(pages)" : "Line \(line) of page \(page): the quick brown fox jumps over the lazy dog 0123456789."161 let attributed = NSAttributedString(string: text, attributes: [NSAttributedString.Key(kCTFontAttributeName as String): font,162 NSAttributedString.Key(kCTForegroundColorAttributeName as String): CGColor(gray: 0, alpha: 1)])163 target.textPosition = CGPoint(x: 54, y: 792 - 60 - CGFloat(line) * 16)164 CTLineDraw(CTLineCreateWithAttributedString(attributed), target)165 }166 }167 for page in 1...pages {168 context.beginPDFPage(nil)169 if scanned {170 let scale: CGFloat = 150.0 / 72.0171 let width = Int(612 * scale), height = Int(792 * scale)172 guard let bitmap = CGContext(data: nil, width: width, height: height, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpaceCreateDeviceGray(),173 bitmapInfo: CGImageAlphaInfo.none.rawValue) else { throw CommandFailure(.conversionFailed, "A page bitmap could not be created.") }174 bitmap.scaleBy(x: scale, y: scale)175 draw(page: page, in: bitmap)176 if let image = bitmap.makeImage() { context.draw(image, in: box) }177 } else {178 draw(page: page, in: context)179 }180 context.endPDFPage()181 }182 context.closePDF()183 }184185 /// A folder tree of text (compressible) and pseudo-random (incompressible) files totalling about `bytes`.186 static func writeFolder(at url: URL, bytes: Int) throws {187 let fm = FileManager.default188 try fm.createDirectory(at: url, withIntermediateDirectories: true)189 var remaining = bytes, index = 0190 var state: UInt64 = 0xD1B54A32D192ED03191 let line = Data("ORIX Convert archive benchmark line: the quick brown fox jumps over the lazy dog 0123456789.\n".utf8)192 while remaining > 0 {193 index += 1194 let size = min(remaining, index % 10 == 0 ? 20 << 20 : 2 << 20)195 let folder = url.appendingPathComponent("group-\(index % 8)", isDirectory: true)196 try fm.createDirectory(at: folder, withIntermediateDirectories: true)197 var data = Data(capacity: size)198 if index % 2 == 0 {199 while data.count < size { data.append(line) }200 } else {201 var words = [UInt64](repeating: 0, count: size / 8 + 1)202 for i in words.indices { state ^= state << 13; state ^= state >> 7; state ^= state << 17; words[i] = state }203 words.withUnsafeBytes { data.append(contentsOf: $0.prefix(size)) }204 }205 try data.prefix(size).write(to: folder.appendingPathComponent(String(format: "file-%04d.%@", index, index % 2 == 0 ? "txt" : "bin")))206 remaining -= size207 }208 }209}210