// // ArticleStringFormatterPerformanceTests.swift // NetNewsWireTests // // Created by Brent Simmons on 4/21/26. // Copyright © 2026 Ranchero Software, LLC. All rights reserved. // import Articles import Foundation import RSCore import RSParser import XCTest @testable import NetNewsWire /// Each test uses a fresh formatter per pass so every call hits the /// code path (not the cache). /// /// Benchmark: release build, Apple Silicon — mean per-call time /// captured at the point the legacy implementations were deleted: /// /// Function Legacy Current Speedup /// ------------------------- ------------ ------------ ------- /// truncatedFeedName 0.00000048 s 0.00000040 s ~1.2× /// truncatedTitle 0.00000060 s 0.00000024 s ~2.5× /// attributedTruncatedTitle 0.00000580 s 0.00000055 s ~11× /// truncatedSummary 0.00001200 s 0.00000102 s ~12× /// @MainActor final class ArticleStringFormatterPerformanceTests: XCTestCase { private static let articles: [Article] = makePerformanceTestArticles() private static let feedNames: [String] = makePerformanceTestFeedNames() func testTruncatedFeedName() { measure { for _ in 0..<1000 { let formatter = ArticleStringFormatter() for name in Self.feedNames { _ = formatter.truncatedFeedName(name) } } } } func testTruncatedTitle() { measure { for _ in 0..<1000 { let formatter = ArticleStringFormatter() for article in Self.articles { _ = formatter.truncatedTitle(article, forHTML: true) _ = formatter.truncatedTitle(article, forHTML: false) } } } } func testAttributedTruncatedTitle() { measure { for _ in 0..<1000 { let formatter = ArticleStringFormatter() for article in Self.articles { _ = formatter.attributedTruncatedTitle(article) } } } } func testTruncatedSummary() { measure { for _ in 0..<1000 { let formatter = ArticleStringFormatter() for article in Self.articles { _ = formatter.truncatedSummary(article) } } } } } // MARK: - Fixtures @MainActor private func makePerformanceTestArticles() -> [Article] { let count = 100 let sampleTitles = [ "Simple plain title", "Title with bold and italic and code", "Title with entities & ampersand and "quotes" and <tags>", "Long title with lots of words that stretches on and on for quite a while before ending", "Unicode title with emoji 🎉 and accents café résumé", "Title with\nnewlines\tand\ttabs" ] let entityParagraph = "

Tom & Jerry run a test — the body continues without any further entities for a good long while to exercise the post-ampersand slow path scanning across a realistic-scale article body.

" let loremParagraph = "

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

" let sampleBodies = [ "

A short article body with no entities at all.

", "

Paragraph with bold, italic, and links.

Second paragraph with & entities <tags> and more content.

", String(repeating: entityParagraph, count: 100), String(repeating: loremParagraph, count: 100), "

Comments

" ] var result: [Article] = [] result.reserveCapacity(count) for i in 0.. [String] { var names: [String] = [] names.reserveCapacity(300) for i in 0..<150 { names.append("Feed \(i)") } let longPrefix = "An overly descriptive feed title with lots of padding text to ensure it exceeds the maximum limit for feed-name truncation testing purposes" for i in 0..<100 { names.append("\(longPrefix) part \(i)") } for i in 0..<50 { names.append("Émojis 🎉🎊🎈 and accénts résumé café feed #\(i)") } return names }