SPB Git

Repository Public BSD-3-Clause

Benchmarks.swift 10.5 KB 193 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Maps9// File             : Packages/ORIXAtlas/Tests/ORIXAtlasTests/Benchmarks.swift10// Module           : ORIXAtlasTests11// Repository       : https://git.spboucher.ai/orix-maps12// Created          : 2026-11-2513// Last Modified    : 2026-11-2514//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : Proprietary, all rights reserved (see license file)80// License File     : LICENSE81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import CoreGraphics89import Foundation90import ORIXAtlas91import ORIXGeoCore92import ORIXMapStyle93import ORIXTileStore94import Testing9596/// Measurements quoted in the README. Run with:97///   ORIX_ATLAS_BENCH=1 swift test -c release --filter Benchmarks98@Suite(.enabled(if: TestData.benchmarksEnabled && TestData.hasMontreal), .serialized) struct Benchmarks {99    private func f(_ v: Double, _ digits: Int = 2) -> String { String(format: "%.\(digits)f", v) }100101    @Test func meshBuildPerTile() throws {102        let archive = try TestData.montreal()103        let ids = TestData.benchmarkTiles()104        print("BENCH mesh: \(ids.count) tiles")105        print("BENCH mesh | zoom | tiles | decode p50 ms | decode p95 ms | build p50 ms | build p95 ms | vertices p50 | vertices max | indices p50 | indices max | bytes p50 | bytes max |")106        var allBuild: [Double] = [], allDecode: [Double] = []107        for z in UInt8(12)...15 {108            var decode: [Double] = [], build: [Double] = [], vertices: [Double] = [], indices: [Double] = [], bytes: [Double] = []109            for id in ids where id.z == z {110                guard let data = try archive.tile(id) else { continue }111                var tile: VectorTile?112                // Best of three, to remove scheduling noise from a single run.113                var d = Double.infinity, b = Double.infinity114                var mesh: TileMesh?115                for _ in 0..<3 {116                    d = min(d, try milliseconds { tile = try VectorTile(data: data) })117                    b = min(b, milliseconds { mesh = TileMeshBuilder.build(tile: tile!, id: id, options: .init(isDeepestZoom: z == 15)) })118                }119                decode.append(d); build.append(b)120                vertices.append(Double(mesh!.vertices.count)); indices.append(Double(mesh!.indices.count)); bytes.append(Double(mesh!.byteSize))121            }122            allBuild += build; allDecode += decode123            print("BENCH mesh | \(z) | \(build.count) | \(f(percentile(decode, 50))) | \(f(percentile(decode, 95))) | \(f(percentile(build, 50))) | \(f(percentile(build, 95))) | \(Int(percentile(vertices, 50))) | \(Int(vertices.max()!)) | \(Int(percentile(indices, 50))) | \(Int(indices.max()!)) | \(Int(percentile(bytes, 50))) | \(Int(bytes.max()!)) |")124        }125        print("BENCH mesh | all | \(allBuild.count) | \(f(percentile(allDecode, 50))) | \(f(percentile(allDecode, 95))) | \(f(percentile(allBuild, 50))) | \(f(percentile(allBuild, 95))) |")126        #expect(allBuild.count > 100)127    }128129    @Test func triangulationThroughput() throws {130        let archive = try TestData.montreal()131        for layerName in ["buildings", "landuse"] {132            var polygons: [[[VectorTile.Point]]] = []133            for id in TestData.benchmarkTiles() {134                guard let data = try archive.tile(id), let layer = try VectorTile(data: data).layer(layerName) else { continue }135                for feature in layer.features where feature.type == .polygon { polygons += polygonGroups(feature) }136            }137            let points = polygons.reduce(0) { $0 + $1.reduce(0) { $0 + $1.count } }138            var best = Double.infinity, triangles = 0139            for _ in 0..<5 {140                var indices: [UInt32] = []141                indices.reserveCapacity(points * 3)142                let ms = milliseconds { for p in polygons { Triangulator.triangulate(p, baseIndex: 0, into: &indices) } }143                best = min(best, ms); triangles = indices.count / 3144            }145            let largest = polygons.map { $0.reduce(0) { $0 + $1.count } }.max() ?? 0146            print("BENCH triangulation | \(layerName) | polygons \(polygons.count) | ring points \(points) | largest polygon \(largest) points | triangles \(triangles) | \(f(best)) ms | \(Int(Double(triangles) / best * 1000)) triangles/s |")147            #expect(triangles > 0)148        }149    }150151    @Test func offscreenFrames() async throws {152        let size = CGSize(width: 393, height: 852)153        let downtown = Coordinate(latitude: 45.5017, longitude: -73.5673)154        print("BENCH frame: 1179 x 2556 px (393 x 852 pt at 3x), 4x MSAA")155        print("BENCH frame | camera | tiles | draw calls | triangles | pipeline ms | cold ms (prepare) | warm p50 ms | warm p95 ms | encode p50 | gpu wait p50 | gpu p50 | readback p50 | mesh bytes | mesh bytes/tile |")156        for (name, camera) in [157            ("z10", AtlasCamera(center: downtown, zoom: 10)),158            ("z12", AtlasCamera(center: downtown, zoom: 12)),159            ("z14", AtlasCamera(center: downtown, zoom: 14)),160            ("z16", AtlasCamera(center: downtown, zoom: 16)),161            ("z12.9 (z12 tiles at their largest)", AtlasCamera(center: downtown, zoom: 12.9)),162            ("z15.5 bearing 30 pitch 60", AtlasCamera(center: downtown, zoom: 15.5, bearing: 30, pitch: 60)),163        ] {164            // Cold: new archive mapping, empty decoded-tile cache, empty mesh cache.165            let renderer = try await AtlasRenderer(store: TileStore(archive: try TestData.montreal()), theme: BuiltInThemes.day)166            _ = try await renderer.renderBitmap(camera: camera, size: size, scale: 3)167            let cold = await renderer.lastFrame168            var total: [Double] = [], encode: [Double] = [], wait: [Double] = [], gpu: [Double] = [], read: [Double] = []169            for _ in 0..<60 {170                _ = try await renderer.renderBitmap(camera: camera, size: size, scale: 3)171                let s = await renderer.lastFrame172                #expect(s.tilesBuilt == 0)173                total.append(s.totalMilliseconds); encode.append(s.encodeMilliseconds); wait.append(s.gpuWaitMilliseconds); gpu.append(s.gpuMilliseconds); read.append(s.readbackMilliseconds)174            }175            let bytes = await renderer.cachedMeshBytes, count = await renderer.cachedMeshCount176            print("BENCH frame | \(name) | \(cold.coveredTiles) | \(cold.drawCalls) | \(cold.triangles) | \(f(await renderer.pipelineMilliseconds, 1)) | \(f(cold.totalMilliseconds, 1)) (\(f(cold.prepareMilliseconds, 1))) | \(f(percentile(total, 50))) | \(f(percentile(total, 95))) | \(f(percentile(encode, 50))) | \(f(percentile(wait, 50))) | \(f(percentile(gpu, 50))) | \(f(percentile(read, 50))) | \(bytes) | \(bytes / max(1, count)) |")177        }178    }179180    @Test func meshMemoryForAPanAcrossTheCity() async throws {181        // Every benchmark tile cached at once: what a long pan across the island at four zooms leaves in the mesh cache.182        let archive = try TestData.montreal()183        var bytes = 0, vertices = 0, indices = 0, count = 0184        for id in TestData.benchmarkTiles() {185            guard let data = try archive.tile(id) else { continue }186            let mesh = TileMeshBuilder.build(tile: try VectorTile(data: data), id: id, options: .init(isDeepestZoom: id.z == 15))187            bytes += mesh.byteSize; vertices += mesh.vertices.count; indices += mesh.indices.count; count += 1188        }189        print("BENCH memory | \(count) tiles (z12-z15) | \(vertices) vertices | \(indices) indices | \(bytes) bytes | \(bytes / max(1, count)) bytes/tile |")190        #expect(count > 0)191    }192}193
Files main
Menu

Theme