SPB Git

Repository Public

PerformanceTests.swift 3.3 KB 61 lines swift

1//2//  VQuant iOS3//  File: VQuantTests/PerformanceTests.swift4//  Description: Measurements (XCTest metrics) of the hot paths: chart rendering of a long series, indicator5//               computation, report parsing, cache encryption. Numbers are read from the test log, never assumed.6//7//  Author: Simon-Pierre Boucher8//  Contact: contact@spboucher.ai9//1011import XCTest12@testable import VQuant1314@MainActor15final class PerformanceTests: XCTestCase {16    private func bars(_ n: Int) -> [Bar] {17        (0..<n).map { i in18            let base = 100 + 10 * sin(Double(i) / 9) + Double(i) * 0.0119            return Bar(date: Date(timeIntervalSince1970: 1_600_000_000 + Double(i) * 86_400), open: base, high: base + 1.5, low: base - 1.2, close: base + (i % 2 == 0 ? 0.8 : -0.6), volume: 1_000_000)20        }21    }2223    /// One full frame of the chart (price pane, overlays, three panels) for 12 000 daily bars (about 47 years).24    func testChartFrame12000Bars() {25        let data = bars(12_000)26        let cfg = ChartConfiguration(kind: .candles, overlays: Set(ChartOverlay.allCases), panels: Set(ChartPanel.allCases))27        let options = XCTMeasureOptions(); options.iterationCount = 528        measure(metrics: [XCTClockMetric()], options: options) {29            XCTAssertNotNil(ChartSnapshot.png(bars: data, configuration: cfg, intraday: false, title: "PERF", subtitle: ""))30        }31    }3233    func testIndicators12000Bars() {34        let closes = bars(12_000).map(\.close)35        let options = XCTMeasureOptions(); options.iterationCount = 536        measure(metrics: [XCTClockMetric()], options: options) {37            _ = Indicators.sma(closes, period: 200); _ = Indicators.ema(closes, period: 21); _ = Indicators.bollinger(closes)38            _ = Indicators.rsi(closes); _ = Indicators.macd(closes); _ = Indicators.rollingVolatility(closes)39        }40    }4142    /// A long report: 60 sections with a table and a list each (about 9 000 words).43    func testMarkdownParseLongReport() {44        let section = "## Section\n\nUn paragraphe avec du **gras**, un [lien](https://example.com) et un marqueur :up: pour la tendance.\n\n| A | B | C |\n|---|---:|---:|\n| x | 1,2 | 3,4 |\n| y | 5,6 | 7,8 |\n\n- point un\n- point deux\n  - sous-point\n\n"45        let text = String(repeating: section + String(repeating: "Phrase de remplissage pour atteindre une longueur réaliste. ", count: 12) + "\n\n", count: 60)46        let options = XCTMeasureOptions(); options.iterationCount = 547        measure(metrics: [XCTClockMetric()], options: options) { XCTAssertGreaterThan(MarkdownParser.parse(text).count, 200) }48    }4950    func testCacheRoundTrip1MB() async {51        let cache = DiskCache(directory: FileManager.default.temporaryDirectory.appendingPathComponent("vq-perf-\(UUID().uuidString)"), keyProvider: FixedCacheKey())52        let payload = Data((0..<1_048_576).map { UInt8(truncatingIfNeeded: $0) })53        let start = ContinuousClock.now54        for i in 0..<10 { await cache.save("k\(i)", payload); let hit = await cache.load("k\(i)"); XCTAssertEqual(hit?.data.count, payload.count) }55        let elapsed = ContinuousClock.now - start56        let ms = Double(elapsed.components.seconds) * 1000 + Double(elapsed.components.attoseconds) / 1e1557        print("PERF cache: 10 × (chiffrer + écrire + lire + déchiffrer 1 Mo) = \(Int(ms)) ms")58        XCTAssertLessThan(ms, 5000)59    }60}61
Files main
Menu

Theme