1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import OSLog
import SwiftUI
@MainActor
enum ViewUpdateMetrics {
private static let enabled = ProcessInfo.processInfo.arguments.contains("-profile-view-updates")
private static let log = OSLog(subsystem: "com.oldfella.app", category: "View updates")
private static var counts: [String: Int] = [:]
static func record(_ viewName: @autoclosure () -> String) {
guard enabled else { return }
let name = viewName()
counts[name, default: 0] += 1
os_signpost(.event, log: log, name: "View body", "%{public}@", name as NSString)
}
static func reset() { counts.removeAll() }
static var snapshot: String {
counts.keys.sorted().map { "\($0)=\(counts[$0, default: 0])" }.joined(separator: ";")
}
}
struct ViewUpdateMetricsPanel: View {
@State private var snapshot = ""
var body: some View {
if ProcessInfo.processInfo.arguments.contains("-ui-testing"),
ProcessInfo.processInfo.arguments.contains("-profile-view-updates") {
VStack {
Button("Reset view update counts") {
ViewUpdateMetrics.reset()
snapshot = ""
}
Button("Read view update counts") { snapshot = ViewUpdateMetrics.snapshot }
Text("View updates")
.accessibilityIdentifier("viewUpdateCounts")
.accessibilityValue(snapshot)
}
.font(.caption2)
.padding(4)
.background(.background)
}
}
}