//===--- MirrorTest.swift -------------------------------------------------===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2021 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 // //===----------------------------------------------------------------------===// // This test measures performance of Mirror and related things. import TestsUtils public let benchmarks = [ BenchmarkInfo( name: "TypeName", runFunction: run_TypeName, tags: [.api, .String]), BenchmarkInfo( name: "MirrorDefault", runFunction: run_MirrorDefault, tags: [.api, .String]), ] struct S1 { var s: String; var d: Double } struct S2 { var i: Int; var a: [Range] } class C { var i: Int = 0 } class D: C { var s: String = "" } enum E { case a,b(Int) } struct G { var t: T } class H: C { var t: T; init(_ t: T) { self.t = t }} public func run_MirrorDefault(scale: Int) { let n = 100*scale let s1 = S1(s: "foo", d: 3.14) let s2 = S2(i: 42, a: [0..<4]) let c = C() let d = D() let e = E.a let f = E.b(99) let g = G(t: 12.3) let h = H<[Int]>([1,2,3]) var str = "" for _ in 0..(t: 12.3),MirrorTest.H>") } func typename(of: T.Type) -> String { "\(T.self)" } public func run_TypeName(scale: Int) { let n = 1_000*scale var a: [String] = [] a.reserveCapacity(16) for _ in 0...self)) a.append(typename(of: G.self)) a.append(typename(of: G.self)) a.append(typename(of: H.self)) a.append(typename(of: [S1].self)) a.append(typename(of: [G].self)) a.append(typename(of: [H].self)) a.append(typename(of: S1?.self)) a.append(typename(of: C?.self)) blackHole(a) } let expected = ["S1", "S2", "C", "D", "G", "G", "G", "H", "Array", "Array>", "Array>", "Optional", "Optional", ] check(a == expected) }