From bb12a0852a3b5837162ca552fbb98dbb9d2899ab Mon Sep 17 00:00:00 2001 From: Kyle Date: Mon, 23 Jun 2025 02:35:04 +0800 Subject: [PATCH] Add BenchmarkApp example for TestApp API --- Example/Example/BenchmarkApp.swift | 78 ++++++++++++++++++++++++++++++ Example/Example/ExampleApp.swift | 11 +++++ 2 files changed, 89 insertions(+) create mode 100644 Example/Example/BenchmarkApp.swift diff --git a/Example/Example/BenchmarkApp.swift b/Example/Example/BenchmarkApp.swift new file mode 100644 index 000000000..8be658d61 --- /dev/null +++ b/Example/Example/BenchmarkApp.swift @@ -0,0 +1,78 @@ +// +// BenchmarkApp.swift +// Example +// +// Created by Kyle on 2025/6/23. +// +// Modified from https://gist.github.com/edudnyk/74ed8d3801004a6dcaab09868b522187 + +#if OPENSWIFTUI +import OpenSwiftUI +#else +import SwiftUI +#endif + +#if os(iOS) +import UIKit +#endif + +struct BenchmarkApp { + static func main() { + _TestApp().runBenchmarks([ + Benchmark(), + ]) + } +} +#if os(iOS) +extension UIHostingController: _Test where Content == AnyView {} +extension UIHostingController: _ViewTest where Content == AnyView { + public func initRootView() -> AnyView { + return rootView + } + public func initSize() -> CGSize { + sizeThatFits(in: UIScreen.main.bounds.size) + } +} +#endif + +struct PerformanceTest: _PerformanceTest { + var name = "RedColor Test" + func runTest(host: _BenchmarkHost, options: [AnyHashable : Any]) { + #if os(iOS) + let test = _makeUIHostingController(AnyView(RedColor())) as! UIHostingController + test.setUpTest() + test.render() + test._forEachIdentifiedView { proxy in + let state = test.stateForIdentifier(proxy.identifier, type: Bool.self, in: type(of: test.rootView)) + let view = test.viewForIdentifier(proxy.identifier, AnyView.self) + print("IDENTIFIER: \(proxy)") + print("STATE: \(state)") + print("VIEW: \(view)") + } + test.tearDownTest() + #endif + } + +} + +struct RedColor: View { + var id: String { "RedColor" } + + var body: some View { + Color.red._identified(by: id) + } +} + +struct Benchmark: _Benchmark { + func setUpTest() { + print("DSF") + } + + func measure(host: _BenchmarkHost) -> [Double] { + return [ + host.measureAction { + PerformanceTest().runTest(host: host, options: [:]) + }, + ] + } +} diff --git a/Example/Example/ExampleApp.swift b/Example/Example/ExampleApp.swift index 85673d5f8..5d1cb3fca 100644 --- a/Example/Example/ExampleApp.swift +++ b/Example/Example/ExampleApp.swift @@ -12,6 +12,17 @@ import SwiftUI #endif @main +struct EntryPoint { + static func main() { + let runBenchmark = CommandLine.arguments.contains("benchmark") + if runBenchmark { + BenchmarkApp.main() + } else { + ExampleApp.main() + } + } +} + struct ExampleApp: App { var body: some Scene { WindowGroup {