From 3f202dbdb40b789e16202f18e20df8374dcdd28b Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 7 Jul 2025 03:05:48 +0800 Subject: [PATCH] Optimize Benchmark summary output --- Example/Example/BenchmarkApp.swift | 37 +++++++++++++++----- Sources/OpenSwiftUICore/Test/Benchmark.swift | 12 +++---- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/Example/Example/BenchmarkApp.swift b/Example/Example/BenchmarkApp.swift index 8be658d61..bc1ae3b7d 100644 --- a/Example/Example/BenchmarkApp.swift +++ b/Example/Example/BenchmarkApp.swift @@ -19,7 +19,8 @@ import UIKit struct BenchmarkApp { static func main() { _TestApp().runBenchmarks([ - Benchmark(), + RedBenchmark(), + BlueBenchmark(), ]) } } @@ -36,10 +37,17 @@ extension UIHostingController: _ViewTest where Content == AnyView { #endif struct PerformanceTest: _PerformanceTest { - var name = "RedColor Test" + var name: String { "PerformanceTest" } + + let view: AnyView + + init(_ view: some View) { + self.view = AnyView(view) + } + func runTest(host: _BenchmarkHost, options: [AnyHashable : Any]) { #if os(iOS) - let test = _makeUIHostingController(AnyView(RedColor())) as! UIHostingController + let test = _makeUIHostingController(view) as! UIHostingController test.setUpTest() test.render() test._forEachIdentifiedView { proxy in @@ -52,7 +60,6 @@ struct PerformanceTest: _PerformanceTest { test.tearDownTest() #endif } - } struct RedColor: View { @@ -63,15 +70,29 @@ struct RedColor: View { } } -struct Benchmark: _Benchmark { - func setUpTest() { - print("DSF") +struct BlueColor: View { + var id: String { "BlueColor" } + + var body: some View { + Color.blue._identified(by: id) + } +} + +struct RedBenchmark: _Benchmark { + func measure(host: _BenchmarkHost) -> [Double] { + return [ + host.measureAction { + PerformanceTest(RedColor()).runTest(host: host, options: [:]) + }, + ] } +} +struct BlueBenchmark: _Benchmark { func measure(host: _BenchmarkHost) -> [Double] { return [ host.measureAction { - PerformanceTest().runTest(host: host, options: [:]) + PerformanceTest(BlueColor()).runTest(host: host, options: [:]) }, ] } diff --git a/Sources/OpenSwiftUICore/Test/Benchmark.swift b/Sources/OpenSwiftUICore/Test/Benchmark.swift index 1475acb17..7b40afad9 100644 --- a/Sources/OpenSwiftUICore/Test/Benchmark.swift +++ b/Sources/OpenSwiftUICore/Test/Benchmark.swift @@ -82,18 +82,14 @@ extension _BenchmarkHost { } package func summarize(_ measurements: [(any _Benchmark, [Double])]) -> String { - let benchmarkData = measurements.map { (String(describing: $0.0), $0.1) } - + let benchmarkData = measurements.map { (String(describing: type(of: $0.0)), $0.1) } let maxNameLength = benchmarkData.map { $0.0.count }.max() ?? 0 - let results: [String] = benchmarkData.map { (name, values) in + let total = values.reduce(0, +) let padding = maxNameLength - name.count + 1 let paddingString = String(repeating: " ", count: padding) - - let average = values.reduce(0, +) / Double(values.count) - let milliseconds = average * 1000.0 - - return "\(name)\(paddingString): \(String(format: "%.3f", milliseconds))ms" + let milliseconds = total * 1000.0 + return "\(name):\(paddingString)\(String(format: "%.3f ms", milliseconds))" } return results.joined(separator: "\n") }