1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if FOUNDATION_FRAMEWORK
// TODO: Support AttributedString performance tests in FoundationPreview
// (see https://github.com/apple/swift-foundation/issues/254)
import Foundation
import XCTest
/// Performance tests for `AttributedString` and its associated objects
final class TestAttributedStringPerformance: XCTestCase {
/// Set to true to record a baseline for equivalent operations on `NSAttributedString`
static let runWithNSAttributedString = false
func createLongString() -> AttributedString {
var str = AttributedString(String(repeating: "a", count: 10000), attributes: AttributeContainer().testInt(1))
str += AttributedString(String(repeating: "b", count: 10000), attributes: AttributeContainer().testInt(2))
str += AttributedString(String(repeating: "c", count: 10000), attributes: AttributeContainer().testInt(3))
return str
}
func createManyAttributesString() -> AttributedString {
var str = AttributedString("a")
for i in 0..<10000 {
str += AttributedString("a", attributes: AttributeContainer().testInt(i))
}
return str
}
func createLongNSString() -> NSMutableAttributedString {
let str = NSMutableAttributedString(string: String(repeating: "a", count: 10000), attributes: [.testInt: NSNumber(1)])
str.append(NSMutableAttributedString(string: String(repeating: "b", count: 10000), attributes: [.testInt: NSNumber(2)]))
str.append(NSMutableAttributedString(string: String(repeating: "c", count: 10000), attributes: [.testInt: NSNumber(3)]))
return str
}
func createManyAttributesNSString() -> NSMutableAttributedString {
let str = NSMutableAttributedString(string: "a")
for i in 0..<10000 {
str.append(NSAttributedString(string: "a", attributes: [.testInt: NSNumber(value: i)]))
}
return str
}
// MARK: - String Manipulation
func testInsertIntoLongString() {
var str = createLongString()
let idx = str.characters.index(str.startIndex, offsetBy: str.characters.count / 2)
let toInsert = AttributedString(String(repeating: "c", count: str.characters.count))
let strNS = createLongNSString()
let idxNS = str.characters.count / 2
let toInsertNS = NSAttributedString(string: String(repeating: "c", count: str.characters.count))
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
strNS.insert(toInsertNS, at: idxNS)
} else {
str.insert(toInsert, at: idx)
}
}
}
func testReplaceSubrangeOfLongString() {
var str = createLongString()
let start = str.characters.index(str.startIndex, offsetBy: str.characters.count / 2)
let range = start ... str.characters.index(start, offsetBy: 10)
let toInsert = AttributedString(String(repeating: "d", count: str.characters.count / 2), attributes: AttributeContainer().testDouble(2.5))
let strNS = createLongNSString()
let startNS = strNS.string.count / 2
let rangeNS = NSRange(location: startNS, length: 10)
let toInsertNS = NSAttributedString(string: String(repeating: "d", count: strNS.string.count / 2), attributes: [.testDouble: NSNumber(value: 2.5)])
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
strNS.replaceCharacters(in: rangeNS, with: toInsertNS)
} else {
str.replaceSubrange(range, with: toInsert)
}
}
}
// MARK: - Attribute Manipulation
func testSetAttribute() {
var str = createManyAttributesString()
let strNS = createManyAttributesNSString()
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
strNS.addAttributes([.testDouble: NSNumber(value: 1.5)], range: NSRange(location: 0, length: strNS.string.count))
} else {
str.testDouble = 1.5
}
}
}
func testGetAttribute() {
let str = createManyAttributesString()
let strNS = createManyAttributesNSString()
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
strNS.enumerateAttribute(.testDouble, in: NSRange(location: 0, length: strNS.string.count), options: []) { (attr, range, pointer) in
let _ = attr
}
} else {
let _ = str.testDouble
}
}
}
func testSetAttributeSubrange() {
var str = createManyAttributesString()
let range = str.characters.index(str.startIndex, offsetBy: str.characters.count / 2)...
let strNS = createManyAttributesNSString()
let rangeNS = NSRange(location: 0, length: str.characters.count / 2)
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
strNS.addAttributes([.testDouble: NSNumber(value: 1.5)], range: rangeNS)
} else {
str[range].testDouble = 1.5
}
}
}
func testGetAttributeSubrange() {
let str = createManyAttributesString()
let range = str.characters.index(str.startIndex, offsetBy: str.characters.count / 2)...
let strNS = createManyAttributesNSString()
let rangeNS = NSRange(location: 0, length: str.characters.count / 2)
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
strNS.enumerateAttribute(.testDouble, in: rangeNS, options: []) { (attr, range, pointer) in
let _ = attr
}
} else {
let _ = str[range].testDouble
}
}
}
func testModifyAttributes() {
let str = createManyAttributesString()
let strNS = createManyAttributesNSString()
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
strNS.enumerateAttribute(.testInt, in: NSRange(location: 0, length: strNS.string.count), options: []) { (val, range, pointer) in
if let value = val as? NSNumber {
strNS.addAttributes([.testInt: NSNumber(value: value.intValue + 2)], range: range)
}
}
} else {
let _ = str.transformingAttributes(\.testInt) { transformer in
if let val = transformer.value {
transformer.value = val + 2
}
}
}
}
}
func testReplaceAttributes() {
var str = createManyAttributesString()
let old = AttributeContainer().testInt(100)
let new = AttributeContainer().testDouble(100.5)
let strNS = createManyAttributesNSString()
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
strNS.enumerateAttribute(.testInt, in: NSRange(location: 0, length: strNS.string.count), options: []) { (val, range, pointer) in
if let value = val as? NSNumber, value == 100 {
strNS.removeAttribute(.testInt, range: range)
strNS.addAttribute(.testDouble, value: NSNumber(value: 100.5), range: range)
}
}
} else {
str.replaceAttributes(old, with: new)
}
}
}
func testMergeMultipleAttributes() throws {
var str = createManyAttributesString()
let new = AttributeContainer().testDouble(1.5).testString("test")
let strNS = createManyAttributesNSString()
let newNS: [NSAttributedString.Key: Any] = [.testDouble: NSNumber(value: 1.5), .testString: "test"]
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
strNS.addAttributes(newNS, range: NSRange(location: 0, length: strNS.string.count))
} else {
str.mergeAttributes(new)
}
}
}
func testSetMultipleAttributes() throws {
var str = createManyAttributesString()
let new = AttributeContainer().testDouble(1.5).testString("test")
let strNS = createManyAttributesNSString()
let rangeNS = NSRange(location: 0, length: str.characters.count / 2)
let newNS: [NSAttributedString.Key: Any] = [.testDouble: NSNumber(value: 1.5), .testString: "test"]
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
strNS.setAttributes(newNS, range: rangeNS)
} else {
str.setAttributes(new)
}
}
}
// MARK: - Attribute Enumeration
func testEnumerateAttributes() {
let str = createManyAttributesString()
let strNS = createManyAttributesNSString()
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
strNS.enumerateAttributes(in: NSRange(location: 0, length: strNS.string.count), options: []) { (attrs, range, pointer) in
}
} else {
for _ in str.runs {
}
}
}
}
func testEnumerateAttributesSlice() {
let str = createManyAttributesString()
let strNS = createManyAttributesNSString()
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
strNS.enumerateAttribute(.testInt, in: NSRange(location: 0, length: strNS.string.count), options: []) { (val, range, pointer) in
}
} else {
for (_, _) in str.runs[\.testInt] {
}
}
}
}
// MARK: - NSAS Conversion
func testConvertToNSAS() throws {
guard !TestAttributedStringPerformance.runWithNSAttributedString else {
throw XCTSkip("Test disabled for NSAS")
}
let str = createManyAttributesString()
self.measure(metrics: [XCTClockMetric()]) {
let _ = try! NSAttributedString(str, including: AttributeScopes.TestAttributes.self)
}
}
func testConvertFromNSAS() throws {
guard !TestAttributedStringPerformance.runWithNSAttributedString else {
throw XCTSkip("Test disabled for NSAS")
}
let str = createManyAttributesString()
let ns = try NSAttributedString(str, including: AttributeScopes.TestAttributes.self)
self.measure(metrics: [XCTClockMetric()]) {
let _ = try! AttributedString(ns, including: AttributeScopes.TestAttributes.self)
}
}
// MARK: - Encoding and Decoding
func testEncode() throws {
struct CodableType: Codable {
@CodableConfiguration(from: AttributeScopes.TestAttributes.self)
var str = AttributedString()
}
let str = createManyAttributesString()
let codableType = CodableType(str: str)
let encoder = JSONEncoder()
let ns = createManyAttributesNSString()
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
let _ = try! NSKeyedArchiver.archivedData(withRootObject: ns, requiringSecureCoding: false)
} else {
let _ = try! encoder.encode(codableType)
}
}
}
func testDecode() throws {
struct CodableType: Codable {
@CodableConfiguration(from: AttributeScopes.TestAttributes.self)
var str = AttributedString()
}
let str = createManyAttributesString()
let codableType = CodableType(str: str)
let encoder = JSONEncoder()
let data = try encoder.encode(codableType)
let decoder = JSONDecoder()
let ns = createManyAttributesNSString()
let dataNS = try NSKeyedArchiver.archivedData(withRootObject: ns, requiringSecureCoding: false)
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
let _ = try! NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(dataNS)
} else {
let _ = try! decoder.decode(CodableType.self, from: data)
}
}
}
// MARK: - Other
func testCreateLongString() {
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
let _ = createLongNSString()
} else {
let _ = createLongString()
}
}
}
func testCreateManyAttributesString() {
self.measure(metrics: [XCTClockMetric()]) {
if TestAttributedStringPerformance.runWithNSAttributedString {
let _ = createManyAttributesNSString()
} else {
let _ = createManyAttributesString()
}
}
}
func testEquality() throws {
guard !TestAttributedStringPerformance.runWithNSAttributedString else {
throw XCTSkip("Test disabled for NSAS")
}
let str = createManyAttributesString()
let str2 = createManyAttributesString()
self.measure(metrics: [XCTClockMetric()]) {
_ = str == str2
}
}
func testSubstringEquality() throws {
guard !TestAttributedStringPerformance.runWithNSAttributedString else {
throw XCTSkip("Test disabled for NSAS")
}
let str = createManyAttributesString()
let str2 = createManyAttributesString()
let range = str.characters.index(str.startIndex, offsetBy: str.characters.count / 2)...
let substring = str[range]
let substring2 = str2[range]
self.measure(metrics: [XCTClockMetric()]) {
_ = substring == substring2
}
}
func testHashAttributedString() throws {
guard !TestAttributedStringPerformance.runWithNSAttributedString else {
throw XCTSkip("Test disabled for NSAS")
}
let str = createManyAttributesString()
self.measure(metrics: [XCTClockMetric()]) {
var hasher = Hasher()
str.hash(into: &hasher)
_ = hasher.finalize()
}
}
func testHashAttributeContainer() throws {
guard !TestAttributedStringPerformance.runWithNSAttributedString else {
throw XCTSkip("Test disabled for NSAS")
}
struct TestAttribute : AttributedStringKey {
static var name = "0"
typealias Value = Int
}
var container = AttributeContainer()
for i in 0 ..< 100000 {
TestAttribute.name = "\(i)"
container[TestAttribute.self] = i
}
self.measure(metrics: [XCTClockMetric()]) {
var hasher = Hasher()
container.hash(into: &hasher)
_ = hasher.finalize()
}
}
}
#endif // FOUNDATION_FRAMEWORK
|