Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Astra9// File : Tests/AstraLocalTests/BenchmarkSuiteTests.swift10// Module : AstraLocalTests11// Repository : https://git.spboucher.ai/orix-astra12// Created : 2026-11-2213// Last Modified : 2026-11-2214//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 AstraCore89import Foundation90import Testing91@testable import AstraLocal9293@Suite struct BenchmarkSuiteTests {94 @Test func standardSetsAreFixedAndDistinct() {95 #expect(LocalBenchmark.prompts(for: .general) == LocalBenchmark.standardPrompts)96 #expect(LocalBenchmark.reasoningPrompts.count == 4 && LocalBenchmark.reasoningPrompts.allSatisfy { $0.reasoning == .medium && $0.images.isEmpty })97 #expect(LocalBenchmark.codingPrompts.map(\.codeLanguage) == ["swift", "python", "javascript"])98 let vision = LocalBenchmark.visionPrompts99 #expect(vision.map(\.id) == ["colour", "chart", "text", "describe"] && vision.allSatisfy { $0.images.count == 1 && $0.images[0].mimeType == "image/png" })100 for set in LocalBenchmark.PromptSet.allCases {101 let ids = LocalBenchmark.prompts(for: set).map(\.id)102 #expect(Set(ids).count == ids.count && !ids.isEmpty, "\(set)")103 }104 #expect(LocalBenchmark().promptSet == .general && LocalBenchmark(set: .coding).promptSet == .coding)105 #expect(LocalBenchmark(prompts: [.init(id: "x", text: "y")]).promptSet == nil)106 #expect(LocalBenchmark(set: .reasoning).maxOutputTokens == 1_024 && LocalBenchmark(set: .general).maxOutputTokens == 160)107 // Same images every time: runs are comparable.108 #expect(LocalBenchmark.visionPrompts.map(\.images) == vision.map(\.images))109 }110111 @Test func fencedBlockCheckIsFactual() {112 #expect(LocalBenchmark.containsFencedBlock("Here:\n```swift\nfunc f() {}\n```\nDone.", language: "swift"))113 #expect(LocalBenchmark.containsFencedBlock("```js\nlet a = 1\n```", language: "javascript"))114 #expect(LocalBenchmark.containsFencedBlock(" ```Python\nprint(1)\n ```", language: "python"))115 #expect(!LocalBenchmark.containsFencedBlock("```\nfunc f() {}\n```", language: "swift"), "an unlabelled block is not a block in the requested language")116 #expect(!LocalBenchmark.containsFencedBlock("```python\nprint(1)\n```", language: "swift"))117 #expect(!LocalBenchmark.containsFencedBlock("func f() {}", language: "swift"))118 #expect(LocalBenchmark.containsFencedBlock("```swift\nfunc f() {", language: "swift"), "a block cut by the token limit counts")119 #expect(!LocalBenchmark.containsFencedBlock("```text\nx\n```\n```swift", language: "python"))120 #expect(LocalBenchmark.containsAnyFencedBlock("```\nx\n```") && !LocalBenchmark.containsAnyFencedBlock("inline `code` only"))121 }122123 private func result() -> LocalBenchmarkResult {124 func sample(_ id: String, cold: Bool, rate: Double, prefill: Double, ttft: Double) -> LocalBenchmarkResult.Sample {125 .init(promptID: id, cold: cold, timeToFirstToken: ttft, decodeTokensPerSecond: rate, promptTokensPerSecond: prefill, promptTokens: 20, outputTokens: 50,126 peakMemoryBytes: 1 << 30, totalTime: 1, stopReason: .endOfTurn, thermalBefore: .nominal, thermalAfter: .fair, outputPreview: "x",127 reasoningTokens: 30, timeToFirstAnswerToken: ttft + 0.5, effectiveContextLength: 4096, imageCount: nil)128 }129 let device = DeviceFacts(platform: .iOS, modelIdentifier: "iPhone17,2", osVersion: "18.4", physicalMemoryBytes: 8 << 30)130 return LocalBenchmarkResult(modelID: "m", modelRevision: Fixture.revision, device: device, startedAt: Date(timeIntervalSince1970: 1_800_000_000),131 loadTime: 1.5, weightsMemoryBytes: 900 << 20, thermalBefore: .nominal, thermalAfter: .fair,132 samples: [sample("a", cold: true, rate: 10, prefill: 100, ttft: 0.9), sample("b", cold: false, rate: 30, prefill: 300, ttft: 0.2),133 sample("c", cold: false, rate: 50, prefill: 500, ttft: 0.4)],134 measuredOn: "iPhone17,2", promptSet: "reasoning", architecture: "qwen3", quantization: "4-bit", contextLength: 40960,135 totalTime: 3, tokensGenerated: 150)136 }137138 @Test func resultsRoundTripAndCarryTheirProvenance() throws {139 let r = result()140 let e = JSONEncoder(); e.dateEncodingStrategy = .iso8601141 let d = JSONDecoder(); d.dateDecodingStrategy = .iso8601142 let back = try d.decode(LocalBenchmarkResult.self, from: e.encode(r))143 #expect(back == r)144 #expect(back.measuredOn == "iPhone17,2" && back.provenanceLabel == "Measured on iPhone17,2, iOS 18.4")145 #expect(back.warmDecodeTokensPerSecond == 40 && back.warmPromptTokensPerSecond == 400 && back.cold?.promptID == "a")146 #expect(back.modelRevision == Fixture.revision && back.quantization == "4-bit" && back.contextLength == 40960 && back.tokensGenerated == 150)147 #expect(back.samples[1].reasoningTokens == 30 && back.samples[1].timeToFirstAnswerToken == 0.7 && back.thermalAfter == .fair)148 }149150 @Test func resultsStoredBeforeTheNewFieldsStillDecode() throws {151 let e = JSONEncoder(); e.dateEncodingStrategy = .iso8601152 var object = try #require(JSONSerialization.jsonObject(with: e.encode(result())) as? [String: Any])153 for key in ["measuredOn", "promptSet", "architecture", "quantization", "contextLength", "totalTime", "tokensGenerated"] { object[key] = nil }154 var samples = try #require(object["samples"] as? [[String: Any]])155 for i in samples.indices { for key in ["reasoningTokens", "timeToFirstAnswerToken", "effectiveContextLength", "imageCount"] { samples[i][key] = nil } }156 object["samples"] = samples157 let d = JSONDecoder(); d.dateDecodingStrategy = .iso8601158 let old = try d.decode(LocalBenchmarkResult.self, from: JSONSerialization.data(withJSONObject: object))159 #expect(old.measuredOn == nil && old.samples.count == 3 && old.samples[0].reasoningTokens == nil)160 #expect(old.provenanceLabel == "Measured on iPhone17,2, iOS 18.4", "the device record still says where it was measured")161 }162}163