Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions Example/Example/BenchmarkApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import UIKit
struct BenchmarkApp {
static func main() {
_TestApp().runBenchmarks([
Benchmark(),
RedBenchmark(),
BlueBenchmark(),
])
}
}
Expand All @@ -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<AnyView>
let test = _makeUIHostingController(view) as! UIHostingController<AnyView>
test.setUpTest()
test.render()
test._forEachIdentifiedView { proxy in
Expand All @@ -52,7 +60,6 @@ struct PerformanceTest: _PerformanceTest {
test.tearDownTest()
#endif
}

}

struct RedColor: View {
Expand All @@ -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: [:])
},
]
}
Expand Down
12 changes: 4 additions & 8 deletions Sources/OpenSwiftUICore/Test/Benchmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down