Repository Public License
1// ORIX Convert — Packages/EngineGeo/Tests/EngineGeoTests/Benchmark.swift2// Description: Measurement run, skipped unless ORIX_GEO_BENCH is set: converts a generated 200 000-feature layer3// along every path family and prints elapsed time, throughput and output size. Run in release mode:4// ORIX_GEO_BENCH=1 swift test -c release -Xswiftc -enable-testing --filter GeoBenchmark5// Author: Simon-Pierre Boucher — contact@spboucher.ai6import Foundation7import Testing8import ConvertCore9@testable import EngineGeo1011@Suite struct GeoBenchmark {12 static func size(_ url: URL) -> Int {13 var isDirectory: ObjCBool = false14 FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory)15 if !isDirectory.boolValue { return (try? FileManager.default.attributesOfItem(atPath: url.path)[.size] as? NSNumber)?.intValue ?? 0 }16 return ((try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)) ?? []).reduce(0) { $0 + size($1) }17 }1819 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORIX_GEO_BENCH"] != nil)) func measure() async throws {20 let temp = TempDir()21 let n = 200_00022 var text = "{\"type\":\"FeatureCollection\",\"features\":[\n"23 for i in 0..<n {24 let x = -79.5 + Double(i % 1000) * 0.01, y = 45 + Double(i / 1000) * 0.0125 if i % 4 == 3 {26 text += "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[\(x),\(y)],[\(x + 0.008),\(y)],[\(x + 0.008),\(y + 0.008)],[\(x),\(y + 0.008)],[\(x),\(y)]]]},\"properties\":{\"id\":\(i),\"name\":\"feature \(i)\",\"value\":\(Double(i) * 0.25)}}"27 } else {28 text += "{\"type\":\"Feature\",\"geometry\":{\"type\":\"Point\",\"coordinates\":[\(x),\(y),\(Double(i % 300))]},\"properties\":{\"id\":\(i),\"name\":\"feature \(i)\",\"value\":\(Double(i) * 0.25)}}"29 }30 text += i == n - 1 ? "\n" : ",\n"31 }32 text += "]}"33 let source = temp.write("bench.geojson", text)34 print("GEO-BENCH source: \(n) features (150 000 points, 50 000 polygons), GeoJSON \(Self.size(source) / 1024) KiB")35 func run(_ from: URL, _ a: FormatID, _ b: FormatID, _ options: [String: OptionValue] = [:], label: String = "") async throws -> URL {36 let engine = GeoEngine()37 let work = temp.sub("w-\(UUID().uuidString.prefix(6))")38 let request = ConversionRequest(source: from, sourceFormat: a, destinationFormat: b, options: ConversionOptions(options), outputBaseName: "bench")39 let start = Date()40 let output = try await engine.convert(request, context: ConversionContext(workDirectory: work))41 let converted = Date().timeIntervalSince(start)42 let checks = try await engine.validate(output, request: request, source: nil)43 let validated = Date().timeIntervalSince(start) - converted44 let allPassed = checks.allSatisfy { $0.passed }45 #expect(allPassed)46 let features = Int(output.facts["geo.features"] ?? "0") ?? 047 print(String(format: "GEO-BENCH %@ → %@%@: %.2f s (%.0f features/s), validate %.2f s, output %d KiB", a.rawValue, b.rawValue, label, converted, Double(features) / converted, validated, output.files.reduce(0) { $0 + Self.size($1) } / 1024))48 return output.files[0]49 }50 let gpkg = try await run(source, .geojson, .geopackage)51 _ = try await run(source, .geojson, .geopackage, ["crs.target": .string("EPSG:32618")], label: " (reprojected to UTM 18N)")52 let shp = try await run(source, .geojson, .shapefile)53 _ = try await run(source, .geojson, .kml)54 _ = try await run(source, .geojson, .csv)55 _ = try await run(source, .geojson, .gpx, ["gpx.polygons-as-tracks": .bool(true)])56 _ = try await run(gpkg, .geopackage, .geojson)57 _ = try await run(gpkg, .geopackage, .shapefile)58 let shpFiles = try FileManager.default.contentsOfDirectory(at: shp, includingPropertiesForKeys: nil).filter { $0.pathExtension == "shp" }.sorted { $0.path < $1.path }59 _ = try await run(shpFiles[0], .shapefile, .geopackage)60 _ = try await run(shpFiles[0], .shapefile, .geojson)61 }62}63