// // EntityDecodingBenchmark.swift // RSParserTests // // Created by Brent Simmons on 4/19/26. // import XCTest @testable import RSParser import RSParserObjC // One-shot comparison benchmark: old ObjC `rsparser_stringByDecodingHTMLEntities` // vs a Swift wrapper around `XMLEntities.decode`. Prints absolute times and the // speedup ratio. Not meant to be run as part of every test suite. final class EntityDecodingBenchmark: XCTestCase { /// A realistic mix: plain text, predefined XML entities, named HTML entities, /// numeric decimal, numeric hex, a worst-case ampersand-heavy input, and an /// entity-free input (exercises the no-op path). private let inputs: [String] = [ "Tom & Jerry — “Episode 5” (™ 2025)", "Café, résumé, naïve. © 2025 – all rights reserved.", "No entities here, just plain text content that's a typical paragraph from a feed article — should exercise the no-work path efficiently.", "“Hello, "world" & everything else” — said the fox.", String(repeating: "&", count: 200), "
Mixed markup & entities: <script> 😀 ❤ — works?
", "&invalidEntity; & &xyz &; & &;" ] private let iterations = 5_000 func testCompareImplementations() { // Warm up: run both implementations a few times so the first iteration doesn't // pay for cold caches / lazy dict init (the ObjC path builds its entity dict // on first call via dispatch_once). for _ in 0..<100 { for s in inputs { _ = (s as NSString).rsparser_stringByDecodingHTMLEntities() _ = Self.swiftDecode(s) } } // Correctness sanity — outputs should be identical for our inputs. for s in inputs { let objc = (s as NSString).rsparser_stringByDecodingHTMLEntities() let swift = Self.swiftDecode(s) XCTAssertEqual(objc, swift, "divergence for input \(s.debugDescription)") } let objcTime = time(label: "ObjC ") { for _ in 0..