Repository Public License
1// ORIX Convert — Packages/EnginePDF/Tests/EnginePDFTests/Benchmarks.swift2// Description: Throughput measurements quoted in docs/compatibility/engines/pdf.md. Runs only with3// ORIX_PDF_BENCH=1 (swift test --filter Benchmarks).4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import CoreGraphics7import PDFKit8import Testing9import ConvertCore10@testable import EnginePDF1112extension PDFEngineTests {13 @Suite(.enabled(if: ProcessInfo.processInfo.environment["ORIX_PDF_BENCH"] == "1")) struct Benchmarks {14 static let sentence = "The quick brown fox jumps over the lazy dog while seventeen engineers review page"1516 /// Letter pages with 40 lines of 11 pt text and one 1200 × 800 JPEG photo drawn 4 in wide.17 static func officePDF(pages: Int) -> URL {18 let url = Fx.dir("bench").appendingPathComponent("office.pdf")19 let ctx = CGContext(url as CFURL, mediaBox: nil, nil)!20 let jpeg = Fx.jpegData(Fx.noise(1200, 800), quality: 0.8)21 let photo = CGImage(jpegDataProviderSource: CGDataProvider(data: jpeg as CFData)!, decode: nil, shouldInterpolate: true, intent: .defaultIntent)!22 var box = CGRect(x: 0, y: 0, width: 612, height: 792)23 for page in 1...pages {24 ctx.beginPage(mediaBox: &box)25 for line in 0..<40 { Fx.draw("\(sentence) \(page) line \(line)", at: CGPoint(x: 54, y: 740 - CGFloat(line) * 13), size: 11, in: ctx) }26 ctx.draw(photo, in: CGRect(x: 54, y: 30, width: 288, height: 192))27 ctx.endPage()28 }29 ctx.closePDF()30 return url31 }3233 /// Image-only pages: 40 lines of 11 pt text rasterised at 300 dpi.34 static func scanPDF(pages: Int) -> URL {35 let url = Fx.dir("bench").appendingPathComponent("scan.pdf")36 let ctx = CGContext(url as CFURL, mediaBox: nil, nil)!37 var box = CGRect(x: 0, y: 0, width: 612, height: 792)38 for page in 1...pages {39 let lines = (0..<40).map { Fx.ScanLine(text: "\(sentence) \(page) line \($0)", at: CGPoint(x: 54, y: 740 - CGFloat($0) * 13), size: 11) }40 ctx.beginPage(mediaBox: &box); ctx.draw(Fx.scanBitmap(dpi: 300, lines: lines), in: box); ctx.endPage()41 }42 ctx.closePDF()43 return url44 }4546 static func time(_ body: () async throws -> Void) async rethrows -> Double {47 let start = Date()48 try await body()49 return Date().timeIntervalSince(start)50 }5152 /// One operation per process, for a peak-memory figure that belongs to that operation alone53 /// (ORIX_PDF_BENCH_CASE=png300|ocr|ocrtext-accurate|ocrtext-fast|merge|compress|split).54 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORIX_PDF_BENCH_CASE"] != nil)) func memory() async throws {55 let which = ProcessInfo.processInfo.environment["ORIX_PDF_BENCH_CASE"] ?? ""56 var seconds = 0.057 switch which {58 case "png300":59 let office = Benchmarks.officePDF(pages: 40)60 seconds = try await Benchmarks.time { _ = try await Fx.run(Fx.request(office, to: .png, ["resolution.dpi": .int(300)]), validate: false) }61 case "ocr":62 let scan = Benchmarks.scanPDF(pages: 10)63 seconds = try await Benchmarks.time { _ = try await Fx.run(Fx.request(scan, to: .pdf, op: PDFEngine.Operation.ocr), validate: false) }64 case "ocrtext-accurate", "ocrtext-fast":65 let scan = Benchmarks.scanPDF(pages: 10)66 _ = try await Fx.run(Fx.request(scan, to: .txt, ["pages": .string("1")]), validate: false)67 seconds = try await Benchmarks.time { _ = try await Fx.run(Fx.request(scan, to: .txt, ["ocr.level": .string(which.hasSuffix("fast") ? "fast" : "accurate")]), validate: false) }68 case "merge":69 let big = Fx.simplePDF("bench-big.pdf", pages: 2_000, word: "Bench")70 seconds = try await Benchmarks.time { _ = try await Fx.run(Fx.request(big, to: .pdf, op: PDFEngine.Operation.merge, more: [big, big, big, big]), validate: false) }71 case "split":72 let big = Fx.simplePDF("bench-big.pdf", pages: 2_000, word: "Bench")73 seconds = try await Benchmarks.time { _ = try await Fx.run(Fx.request(big, to: .folder, op: PDFEngine.Operation.split), validate: false) }74 case "compress":75 let scan = Benchmarks.scanPDF(pages: 10)76 seconds = try await Benchmarks.time { _ = try await Fx.run(Fx.request(scan, to: .pdf, op: PDFEngine.Operation.compress), validate: false) }77 default: return78 }79 print("BENCH memory case=\(which): \(String(format: "%.2f", seconds)) s, peak RSS \(String(format: "%.0f", Fx.peakRSSMegabytes())) MB (includes building the fixture)")80 }8182 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORIX_PDF_BENCH_CASE"] == nil)) func throughput() async throws {83 let office = Benchmarks.officePDF(pages: 40)84 print("BENCH fixture office.pdf: 40 pages, \(Fx.size(office)) bytes")85 for (format, dpi) in [(FormatID.png, 150), (.png, 300), (.jpeg, 150), (.jpeg, 300), (.tiff, 150)] {86 var seconds = 0.087 for _ in 0..<3 { seconds = await min(seconds == 0 ? .infinity : seconds, try Benchmarks.time { _ = try await Fx.run(Fx.request(office, to: format, ["resolution.dpi": .int(dpi)]), validate: false) }) }88 print("BENCH pdf→\(format) \(dpi) dpi: \(String(format: "%.1f", 40 / seconds)) pages/s (40 pages in \(String(format: "%.2f", seconds)) s, best of 3)")89 }90 let textSeconds = try await Benchmarks.time { _ = try await Fx.run(Fx.request(office, to: .txt), validate: false) }91 print("BENCH pdf→txt: \(String(format: "%.0f", 40 / textSeconds)) pages/s")92 let docxSeconds = try await Benchmarks.time { _ = try await Fx.run(Fx.request(office, to: .docx), validate: false) }93 print("BENCH pdf→docx: \(String(format: "%.0f", 40 / docxSeconds)) pages/s")9495 let scan = Benchmarks.scanPDF(pages: 10)96 print("BENCH fixture scan.pdf: 10 pages at 300 dpi, \(Fx.size(scan)) bytes")97 do {98 _ = try await Fx.run(Fx.request(scan, to: .pdf, op: PDFEngine.Operation.ocr, ["ocr.level": .string("accurate")]), validate: false) // warm-up: model load99 // Where the time goes: rendering the page at 300 dpi, then Vision alone.100 let document = try #require(PDFDocument(url: scan))101 var render = RenderSettings(); render.dpi = 300; render.annotations = false102 var images: [CGImage] = []103 let renderSeconds = try await Benchmarks.time { for i in 0..<10 { let page = document.page(at: i)!; images.append(try PDFRender.render(page: page, plan: try PDFRender.plan(page: page, settings: render), settings: render)) } }104 print("BENCH ocr breakdown: rendering 10 scanned pages at 300 dpi \(String(format: "%.2f", renderSeconds)) s")105 for accurate in [true, false] {106 var settings = OCRSettings(); settings.accurate = accurate107 var best = Double.infinity108 for _ in 0..<3 { best = min(best, try await Benchmarks.time { for image in images { _ = try PDFOCR.recognize(image, settings: settings) } }) }109 print("BENCH ocr breakdown: Vision \(accurate ? "accurate" : "fast") alone, 10 pages: \(String(format: "%.2f", best)) s (\(String(format: "%.1f", 10 / best)) pages/s, best of 3)")110 }111 images = []112 for level in ["accurate", "fast"] {113 for language in ["auto", "en-US"] {114 var result: RunResult?115 var seconds = Double.infinity116 for _ in 0..<3 { seconds = min(seconds, try await Benchmarks.time { result = try await Fx.run(Fx.request(scan, to: .pdf, op: PDFEngine.Operation.ocr, ["ocr.level": .string(level), "ocr.language": .string(language)]), validate: false) }) }117 let text = Fx.pdfText(result!.file, page: 0)118 let expected = (0..<40).map { "\(Benchmarks.sentence) 1 line \($0)" }119 let exact = expected.filter { text.contains($0) }.count120 print("BENCH pdf.ocr \(level) language=\(language): \(String(format: "%.2f", 10 / seconds)) pages/s (10 pages in \(String(format: "%.2f", seconds)) s, best of 3), words=\(result!.output.facts["ocr.words"] ?? "?"), exact lines on page 1: \(exact)/40, output \(Fx.size(result!.file)) bytes")121 }122 }123 } catch ConversionError.engineUnavailable(let reason) { print("BENCH pdf.ocr unavailable: \(reason)") }124125 let big = Fx.simplePDF("bench-big.pdf", pages: 2_000, word: "Bench")126 let mergeSeconds = try await Benchmarks.time { _ = try await Fx.run(Fx.request(big, to: .pdf, op: PDFEngine.Operation.merge, more: [big, big, big, big]), validate: false) }127 print("BENCH pdf.merge 5 × 2000 pages: \(String(format: "%.0f", 10_000 / mergeSeconds)) pages/s (\(String(format: "%.2f", mergeSeconds)) s)")128 let splitSeconds = try await Benchmarks.time { _ = try await Fx.run(Fx.request(big, to: .folder, op: PDFEngine.Operation.split), validate: false) }129 print("BENCH pdf.split every page, 2000 pages: \(String(format: "%.0f", 2_000 / splitSeconds)) pages/s (\(String(format: "%.2f", splitSeconds)) s)")130 let chunkSeconds = try await Benchmarks.time { _ = try await Fx.run(Fx.request(big, to: .folder, op: PDFEngine.Operation.split, ["split.mode": .string("every-n"), "split.every": .int(100)]), validate: false) }131 print("BENCH pdf.split every 100 pages, 2000 pages: \(String(format: "%.0f", 2_000 / chunkSeconds)) pages/s (\(String(format: "%.2f", chunkSeconds)) s)")132133 for (dpi, quality) in [(150, 0.6), (100, 0.4)] {134 var result: RunResult?135 let seconds = try await Benchmarks.time { result = try await Fx.run(Fx.request(office, to: .pdf, op: PDFEngine.Operation.compress, ["compress.dpi": .int(dpi), "quality": .double(quality)]), validate: false) }136 print("BENCH pdf.compress office.pdf dpi=\(dpi) q=\(quality): \(Fx.size(office)) → \(Fx.size(result!.file)) bytes (\(result!.output.facts["result"] ?? "")), \(String(format: "%.0f", 40 / seconds)) pages/s")137 }138 for (dpi, quality) in [(150, 0.6), (100, 0.4)] {139 var result: RunResult?140 let seconds = try await Benchmarks.time { result = try await Fx.run(Fx.request(scan, to: .pdf, op: PDFEngine.Operation.compress, ["compress.dpi": .int(dpi), "quality": .double(quality)]), validate: false) }141 print("BENCH pdf.compress scan.pdf dpi=\(dpi) q=\(quality): \(Fx.size(scan)) → \(Fx.size(result!.file)) bytes (\(result!.output.facts["result"] ?? "")), \(String(format: "%.1f", 10 / seconds)) pages/s")142 }143 let photos = (0..<20).map { Fx.write([Fx.noise(2000, 1500, seed: UInt64(1000 + $0))], "p\($0).jpg", type: .jpeg) }144 let imagesSeconds = try await Benchmarks.time { _ = try await Fx.run(Fx.request(photos[0], to: .pdf, more: Array(photos.dropFirst()), from: .jpeg), validate: false) }145 print("BENCH images→pdf 20 JPEG 2000 × 1500 (pass-through): \(String(format: "%.0f", 20 / imagesSeconds)) images/s")146 print("BENCH peak RSS of the benchmark process: \(String(format: "%.0f", Fx.peakRSSMegabytes())) MB")147 }148 }149}150