Skip to content

Commit 47ac888

Browse files
authored
Optimize Benchmark summary output (#376)
1 parent c405714 commit 47ac888

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

Example/Example/BenchmarkApp.swift

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import UIKit
1919
struct BenchmarkApp {
2020
static func main() {
2121
_TestApp().runBenchmarks([
22-
Benchmark(),
22+
RedBenchmark(),
23+
BlueBenchmark(),
2324
])
2425
}
2526
}
@@ -36,10 +37,17 @@ extension UIHostingController: _ViewTest where Content == AnyView {
3637
#endif
3738

3839
struct PerformanceTest: _PerformanceTest {
39-
var name = "RedColor Test"
40+
var name: String { "PerformanceTest" }
41+
42+
let view: AnyView
43+
44+
init(_ view: some View) {
45+
self.view = AnyView(view)
46+
}
47+
4048
func runTest(host: _BenchmarkHost, options: [AnyHashable : Any]) {
4149
#if os(iOS)
42-
let test = _makeUIHostingController(AnyView(RedColor())) as! UIHostingController<AnyView>
50+
let test = _makeUIHostingController(view) as! UIHostingController<AnyView>
4351
test.setUpTest()
4452
test.render()
4553
test._forEachIdentifiedView { proxy in
@@ -52,7 +60,6 @@ struct PerformanceTest: _PerformanceTest {
5260
test.tearDownTest()
5361
#endif
5462
}
55-
5663
}
5764

5865
struct RedColor: View {
@@ -63,15 +70,29 @@ struct RedColor: View {
6370
}
6471
}
6572

66-
struct Benchmark: _Benchmark {
67-
func setUpTest() {
68-
print("DSF")
73+
struct BlueColor: View {
74+
var id: String { "BlueColor" }
75+
76+
var body: some View {
77+
Color.blue._identified(by: id)
78+
}
79+
}
80+
81+
struct RedBenchmark: _Benchmark {
82+
func measure(host: _BenchmarkHost) -> [Double] {
83+
return [
84+
host.measureAction {
85+
PerformanceTest(RedColor()).runTest(host: host, options: [:])
86+
},
87+
]
6988
}
89+
}
7090

91+
struct BlueBenchmark: _Benchmark {
7192
func measure(host: _BenchmarkHost) -> [Double] {
7293
return [
7394
host.measureAction {
74-
PerformanceTest().runTest(host: host, options: [:])
95+
PerformanceTest(BlueColor()).runTest(host: host, options: [:])
7596
},
7697
]
7798
}

Sources/OpenSwiftUICore/Test/Benchmark.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,14 @@ extension _BenchmarkHost {
8282
}
8383

8484
package func summarize(_ measurements: [(any _Benchmark, [Double])]) -> String {
85-
let benchmarkData = measurements.map { (String(describing: $0.0), $0.1) }
86-
85+
let benchmarkData = measurements.map { (String(describing: type(of: $0.0)), $0.1) }
8786
let maxNameLength = benchmarkData.map { $0.0.count }.max() ?? 0
88-
8987
let results: [String] = benchmarkData.map { (name, values) in
88+
let total = values.reduce(0, +)
9089
let padding = maxNameLength - name.count + 1
9190
let paddingString = String(repeating: " ", count: padding)
92-
93-
let average = values.reduce(0, +) / Double(values.count)
94-
let milliseconds = average * 1000.0
95-
96-
return "\(name)\(paddingString): \(String(format: "%.3f", milliseconds))ms"
91+
let milliseconds = total * 1000.0
92+
return "\(name):\(paddingString)\(String(format: "%.3f ms", milliseconds))"
9793
}
9894
return results.joined(separator: "\n")
9995
}

0 commit comments

Comments
 (0)